wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes BigDecimal and the lack of information about rounding Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "BigDecimal and the lack of information about rounding" Watch "BigDecimal and the lack of information about rounding" New topic
Author

BigDecimal and the lack of information about rounding

Janet Wilson
Ranch Hand

Joined: Jul 16, 2002
Posts: 98
We ran up against a rounding "problem" in Java which we were unaware of. We have since discovered that if we want exact results we need to use the BigDecimal API or we could use int or long if we want to keep track of the decimal (look at Joshua Bloch's item #31 in "Effective Java").
However, my post isn't technical, per se. We (there's 2 of us at my job who are really curious about this) are surprised at the dearth of literature concerning this topic and was wondering if we were somehow snoozing during the beginner's coverage on the Sun site or if they only talk about this problem in formal classes or ??? I would think this topic would be covered extensively in a beginner's guide under their "gotcha's section"? Are there any other gotcha's (besides what Joshua Bloch uncovers in his book) one should be aware of?
Could someone explain why there is little written about this topic?
Thanks in advance for enlightening me.
Janet
P.S.-I thought it was funny that at the JavaOne 2003 conference they are offering a session called: BigInteger, BigDecimal, and a Billion of digits of Pi (ts-1991)
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
This isn't a Java specific issue, so perhaps that's why you haven't stumbled across much coverage on the topic. The loss of precision with floating-point arithmetic is a bit of a techincal matter concerning bit patterns and the size of a given data type. A good computer science book on computer architecture should describe well the topic of floating-point data types and the IEEE 754 standard (which Java follows).
Some place in the cattle drive forum (and in this forum) is a reference to a decent article or two on the subject. I've it book marked at home and will post it later if somebody else doesn't beat me to it.


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Janet Wilson
Ranch Hand

Joined: Jul 16, 2002
Posts: 98
Dirk,
As usual - thanks!
I might even beat you to it because we've found all sorts of information concerning this:
IBM's Alphaworks BigDecimal Overview
IBM's General Decimal Arithmetic (contains many links)
IEEE 754-1985: Standard for Binary Floating-Point Arithmetic
IEEE 854-1987: Standard for Radix-Independent Floating-Point Arithmetic
What Every Computer Scientist Should Know About Floating-Point Arithmetic
It still just struck us as being odd that coverage isn't at least "strongly" mentioned. Please add your links if they are not amongst these.
Thanks! Janet
P.S.-Credit to "JJ" at my work for the links.
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Not everybody has neglected to include this topic in their development of introductory Java curriculum. The purpose of the Sum assignment of The JavaRanch Cattle Drive is to become familiar with this very topic.
You've found some good resources. You may also want to take a look at the Cattle Drive Forum conversation I previously mentioned and from which I now quote:
Originally posted by Richard Boren:
Peter van der Linden, author of Just Java 2, maintains a Java FAQ list at Java Software, FAQs, Books. The fourth question, under Java Programming Language Issues of the FAQ, explains floating point errors plus it has two additional links that give in-depth explanation of floating point.
Janet Wilson
Ranch Hand

Joined: Jul 16, 2002
Posts: 98
Thanks for the additional info Dirk!
Janet
 
Don't get me started about those stupid light bulbs.
 
subject: BigDecimal and the lack of information about rounding