3 Replies Latest reply on Oct 11, 2007 10:03 PM by DrClap

    Floating point arithmetic

      Hi everybody,

      This line:
      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 ?


        • 1. Re: Floating point arithmetic
          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
            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 ?


            • 3. Re: Floating point arithmetic
              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.


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