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 Beginning Java and the fly likes Double Calculations? 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 » Java » Beginning Java
Bookmark "Double Calculations?" Watch "Double Calculations?" New topic
Author

Double Calculations?

Deepak Borania
Ranch Hand

Joined: Jul 28, 2009
Posts: 45


Can you please explain to me why does the above two statements output different results?
I think it's something to do with java's representation of floats, not sure though.

Thanks
John de Michele
Rancher

Joined: Mar 09, 2009
Posts: 600
Deepak:

You get different results because doubles and floats are inherently imprecise. Floating point numbers on computers are approximations, and so you get the results that you did. You shouldn't use floating point numbers for applications where you need real accuracy, such as when dealing with real money. In such a case, you'd probably want to look at the BigDecimal class.

John.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19060
    
  40

I think it's something to do with java's representation of floats, not sure though.


Java respresentation of floating point is the IEEE standard -- which is the standard used by practically every modern day processor.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40034
    
  28
Deepak Borania wrote:I think it's something to do with java's representation of floats, not sure though.
It's because (as Henry said, like most languages) it is represented in binary. It is not possible to represent 0.1 precisely, so you get that sort of result. Try representing 1 / 3 precisely in decimal, and you will see the problem.
Deepak Borania
Ranch Hand

Joined: Jul 28, 2009
Posts: 45
@John : Didn't knew about BigDecimal.

Thanks guys
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19783
    
  20

Computers can only count in binary. That means multiples of 2, but also multiples of 1/2: 1/4, 1/8, etc. 0.1, or 1/10, is represented as follows: 1/16 + 1/32 + 1/256 + 1/512 + 1/4096 + 1/8192 + ...
If you represent 1/2 as .1, 1/4 as .01 etc, you get the following binary representation: .0001100110011001100...
As you can see, this repeats, just like 1/3 does when represented in decimals: 0.33333333...
So you see, just as we humans just quit after a number of threes, processors quit after a while as well. That's where numbers are rounded, and you get these imperfections.

BigDecimal is designed to handle these imperfections, so if you are bothered by it use that class.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Deepak Borania
Ranch Hand

Joined: Jul 28, 2009
Posts: 45
yeah I'm familiar with binary representations.....i had advanced digital electronics design course in college

The accurate representation mattered in the class i was trying to create(monetary related). I got around it counting the money in cents instead.
 
jQuery in Action, 2nd edition
 
subject: Double Calculations?