• 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

The trouble with rounding floating point numbers

 
Ranch Hand
Posts: 820
IntelliJ IDE VI Editor Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I read Trouble with rounding floating point numbers. In the comments section, a couple of people are complaining about "newer, fadish languages like java" that don't have decimal types.

The article supplies some C code that highlights floating point precision errors and I tried those with floats in Java. Seems like you can't add 1/3f and 2/3f and get 1.

I also tried them using BigDecimal and they all seemed to work out OK. So, is the answer to that article "Use BigDecimal" ??
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Tim McGuire:
...is the answer to that article "Use BigDecimal" ??


This article is just an introduction. Note the last paragraph...

More important, perhaps, how do we avoid this problem? In a follow-up article I'm going to look at a different way of doing arithmetic (decimal arithmetic), used by lots of real world software, that avoids the approximation error in floats.


Yes, one "answer" in Java is BigDecimal.

But as far as some of the comments posted in response to this article... :roll:
 
Tim McGuire
Ranch Hand
Posts: 820
IntelliJ IDE VI Editor Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok. thank you Marc.

Java seems to handle the following example from the article OK:


I wonder why java does not suffer from precision problems in this case? i.e. it returns 66.375 like expected.
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Java does suffer from floating-point precision problems. Oh yes it does. The double type is much less prone to that problem than the float type, but both can show imprecision. And we aren't really discussing C here, but remember that C has different compilers so a C app can produce different results on different compilers. I tried it recently because of this beginners' forum discussion.

Try repeating the exercise with "strictfp" in your method heading, see whether that makes any difference. Again this is a topic which came up recently on the JIG beginners' forum.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The JavaBeginnersFaq has links to some further reading on the subject, under #21.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Tim McGuire:

I wonder why java does not suffer from precision problems in this case? i.e. it returns 66.375 like expected.



Java might suffer from the problem, depending on the platform you are running on. Only when you use strictfp it will be guaranteed to run exactly identically on every platform.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic