This discussion is archived
3 Replies Latest reply: Oct 11, 2007 3:03 PM by DrClap RSS

Floating point arithmetic

807605 Newbie
Currently Being Moderated
Hi everybody,

This line:
System.out.println((0.1+0.7)*10);
outputs 7.999999999999999

This is due to how floating point numbers are stored. When writing
a code, sometimes it behaves in an intended way, sometimes it doesn't
(like the one above). Is there a way to "predict" when the code is ok and
when isn't ? Are there any tips to be aware of to get around that kind
of problems ?

Cheers,

Adrian
  • 1. Re: Floating point arithmetic
    DrClap Expert
    Currently Being Moderated
    The name of the mathematical field you're asking about is "Numerical Analysis". What you're asking for doesn't fit into this little box here. And "get around" is the wrong attitude. It's a feature, not a bug.
  • 2. Re: Floating point arithmetic
    807605 Newbie
    Currently Being Moderated
    Ok. Let me ask another question then: How am I supposed to know
    whether the code I have just written is going to work the way I intended ?
    Is the only way to be sure is to use BigDecimal ?

    Cheers,

    Adrian
  • 3. Re: Floating point arithmetic
    DrClap Expert
    Currently Being Moderated
    No. Using BigDecimal just because you don't understand how floating-point numbers work would be... um... short-sighted. And it wouldn't help, either. As soon as you divide 1 by 3 then you have to know how decimal numbers work, which is essentially the same problem.

    Edit: I forgot the forum hasn't been automated to provide the mandatory link for people who ask this question. We still have to do it by hand.

    http://docs.sun.com/source/806-3568/ncg_goldberg.html

    Edited by: DrClap on Oct 11, 2007 3:02 PM