aspose file tools*
The moose likes Java in General and the fly likes discrepancy between double and float Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "discrepancy between double and float " Watch "discrepancy between double and float " New topic
Author

discrepancy between double and float

Sigmund Underg
Greenhorn

Joined: Sep 13, 2013
Posts: 3
Hi,

kann mir bitte jemand die Ausgabe erklären, warum ist der nach float gecastete Wert anders?

Edit: sorry, I should have written everything in english .
Please someone explain me, why the value that is casted to float is different than the double value.

public class Test2
{
public static void main(String[] args)
{
double d = 0.3 / 3;
System.out.println(d);
System.out.println((float) d);
}
}

Ausgabe:
0.09999999999999999
0.1

Mark Beardsley
Ranch Hand

Joined: Jun 07, 2013
Posts: 32
    
    1
More than likely, this discrepancy can be explained as one of the artefacts that occur when a computer tries to store a floating point number. For a far better explanation than I can offer, take a look here - http://www.lahey.com/float.htm

Hope this helps.
Sigmund Underg
Greenhorn

Joined: Sep 13, 2013
Posts: 3
will read it carefully, thanks
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40


Also, take a look at item 20 in the Java Beginner's FAQ

https://www.coderanch.com/how-to/java/JavaBeginnersFaq


Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Sigmund Underg
Greenhorn

Joined: Sep 13, 2013
Posts: 3
ok, that gave me something to read for the weekend. marking the question as solved.
 
Don't get me started about those stupid light bulbs.
 
subject: discrepancy between double and float