This week's giveaway is in the Spring forum.
We're giving away four copies of REST with Spring (video course) and have Eugen Paraschiv 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 REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "0.4 * 100" Watch "0.4 * 100" New topic

0.4 * 100

Michael Imhof

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

Correct statements are:

Mohit Jain
Ranch Hand

Joined: Jun 04, 2007
Posts: 74

I believe the code -

double test = 0.4 * 100;

gives -

test = 40.0

SCJP 5.0, SCWCD in progress
Michael Imhof

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: 15045

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:
subject: 0.4 * 100
It's not a secret anymore!