wood burning stoves 2.0*
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 Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "discrepancy between double and float " Watch "discrepancy between double and float " New topic

discrepancy between double and float

Sigmund Underg

Joined: Sep 13, 2013
Posts: 3

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((float) d);


Mark Beardsley
Ranch Hand

Joined: Jun 07, 2013
Posts: 32
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

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

Joined: Sep 28, 2004
Posts: 18545

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



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

Joined: Sep 13, 2013
Posts: 3
ok, that gave me something to read for the weekend. marking the question as solved.
I agree. Here's the link: http://aspose.com/file-tools
subject: discrepancy between double and float
Similar Threads
converting float to double
Float and ==
Confusing outputs
What is the difference between a double and a float?