This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach 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 Murach's Java Servlets and JSP this week in the Servlets 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: 14074

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
I agree. Here's the link:
subject: 0.4 * 100
Similar Threads
Array trouble
floating point and double accuracy
Noob with a question about Java code
Valadation problem!!!!!!
Java 7 - Sum