File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes 0.4 * 100 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "0.4 * 100" Watch "0.4 * 100" New topic
Author

0.4 * 100

Michael Imhof
Greenhorn

Joined: Nov 07, 2004
Posts: 24
Can somebody explain why this result is wrong?


Correct statements are:


Regards
Michael
Mohit Jain
Ranch Hand

Joined: Jun 04, 2007
Posts: 74
Hi

I believe the code -

double test = 0.4 * 100;

gives -

test = 40.0


SCJP 5.0, SCWCD in progress
Michael Imhof
Greenhorn

Joined: Nov 07, 2004
Posts: 24
Sorry, I was simplyfing the source code and all the sudden
the result was right...
Try this:

-> Result = 39.999999990686774
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13883
    
  10

The float and double data types do not have infinite precision, so you can expect to see some rounding errors when you do calculations.

Have a look at this: Some things you should know about floating-point arithmetic

And here's a more detailed but very technical explanation: What Every Computer Scientist Should Know About Floating-Point Arithmetic


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: 0.4 * 100
 
Similar Threads
Java 7 - Sum
Array trouble
floating point and double accuracy
Valadation problem!!!!!!
Noob with a question about Java code