• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Double Calculations?

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
 
Rancher
Posts: 600
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@John : Didn't knew about BigDecimal.

Thanks guys
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Deepak Borania
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
All of the world's problems can be solved in a garden - Geoff Lawton. Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic