• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

discrepancy between double and float

 
Sigmund Underg
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 32
1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
will read it carefully, thanks
 
Henry Wong
author
Marshal
Pie
Posts: 20823
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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

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


Henry
 
Sigmund Underg
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok, that gave me something to read for the weekend. marking the question as solved.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic