This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Soft Skills and have John Sonmez on-line!
See this thread for details.
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 Soft Skills this week in the Jobs Discussion 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: 14430
    
  23

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 8 API documentation
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: 0.4 * 100