This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates 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 OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP 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: 14279
    
  21

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: 0.4 * 100