Re: Strange basic problem with comparision of two varialbes defined as double

From: Duane Bozarth (dp_bozarth_at_swko.dot.net)
Date: 05/17/04


Date: Mon, 17 May 2004 08:35:20 -0500

RAJ wrote:
>
> Thanks for your reply.
> What alternative is there to compare two values of this sort? I have also
> seen this kind of problem were in I am storing a value defined as double in
> the access database field which is again defined as double with 2 decimal
> places. Even though the value is, for example, 10.00, it at times (not
> always) stores it as 9.99999999999997 in the database.
>
> My application does need to compare a lot of values for accounting purposes.
> Your help is much appriciated.

First of all, did you read the paper at the link? If so, do you
understand the cause of the problems you're having? If not, you'll
continue to make the same (wrong) assumptions forever. Unfortunately,
the tone of your description makes me think you still aren't quite
"getting it" wrt floating point...

If you're doing accounting-type computations, I would recommend you
<not> store data in floating point format to simplify the problem. It
is inevitable in floating point that <most> fractional values cannot be
stored exactly--the source of your consternation resulting in your
original post. Any integer up to the limit of precision of a Double, of
course (roughly 15 decimal digits) can be stored exactly. But, only
those fractions representable by binary fractions are <exactly>
representable in floating point. Thus, any time you make almost any
computation including the cents, there will be a rounding error and your
nice, clean pennies aren't any longer.

Your alternatives are to either use a representation that deals with the
cents cleanly (Currency is a start) or use appropriate rounding
algorithms to control the results of floating point. Being an engineer
rather than a business type, I don't have a particular place to point
you for a tutorial, but I'm sure there are many a search could find or
someone else here may know of a good place...



Relevant Pages

  • Re: Type Question
    ... this is true of all programming languages that use floating point ... Common Lisp comes pretty close to providing that as ... Change the floating point representation. ... computationally efficient binary representation, the fractions should be ...
    (comp.lang.lisp)
  • Re: fractions and floating point
    ... > floating point representation just have the opposite order? ... > The idea is that the two fractions are almost identical and that the error ... > introduced by going to floating point representation is bigger than the ...
    (comp.lang.c)
  • fractions and floating point
    ... is it possible to have two fractions, which have the order a/b < c/d, but when converted into floating point representation just have the opposite order? ... The idea is that the two fractions are almost identical and that the error introduced by going to floating point representation is bigger than the exact difference, but different for the two fractions such that it somehow turns them around. ...
    (comp.lang.c)
  • Re: fractions and floating point
    ... > is it possible to have two fractions, ... talking about values that are exactly representable in the floating point ... > error introduced by going to floating point representation is bigger ... values tend to be calcuated and held at a higher precision in registers ...
    (comp.lang.c)
  • Re: comparison on non-integer types
    ... Dangerous equality comparison involving double types: ... 18 Integer and floating types are collectively called arithmetic types. ... Thus, it should be fully legal to compare two double values with, e.g., ==. ... When you subtract the internal representation of 2.1 from the representation of 2.2, ...
    (comp.lang.c)

Loading