Re: problem with 'double'

From: Tom McGlynn (tam_at_lheapop.gsfc.nasa.gov)
Date: 07/09/04


Date: Fri, 09 Jul 2004 12:38:15 -0400

Iris wrote:

> I have this problem:
> I would like to add two doubles: -0.07 + 0.0175 and te result, instead of
> be -0.0525, is -0.052500000000000005. I do not know what I have this error
> and I would like avoid it or truncate the double. How can I do it?
> Thank you
>
>

This subject comes up every week or two on the Java newsgroups and
equivalently frequently in every other language newsgroup that I've
followed. While I understand that binary representations of
floating point numbers are more efficient, I wonder if this continual
misunderstanding of floating point suggests that the
default floating point representation for data should be decimal rather
than binary.

This isn't going to happen any time soon, but in a world where only a tiny
fraction of applications really need to worry about the efficiency of
floating point computations, maybe it would be better to have a
a default numeric model in the computer that is closer to what we use outside
of it. Of course it still won't be perfect. There will still be the
"How come 10 - 10./3 * 3 != 0?" messages, but my sense is that
that is less confusing to users than the fact that most of the short decimals
they have been using all of their lives are not representable (exactly)
by computers.

PL/1 catered to this as I recall. One could ask for float binary,
or float decimal numbers -- though I'm not sure if that wasn't just
different ways to describing the precision of the underlying numbers. I doubt that
we want to ever preclude binary floating point numbers, but maybe future
languages should consider adopting decimal floating point numbers as
a first-class data type. (Rather than, e.g., the fairly clumsy classes
that Java provides.)

                Regards,
                Tom McGlynn



Relevant Pages

  • Re: wprintf() and large exact floats?
    ... It is almost always a mistake to think of floating point representations to a fixed number of decimals as "exact". ... The point is that you have no right to assume that arithmetic involving floating point representations can ever be exact. ...
    (microsoft.public.vc.language)
  • Re: Precision
    ... There is nothing wrong with the D3 floating point algorithm or anyone else's ... It's the same if we use Precision, floating point, or ... positions it is IMPOSIBLE to have a result of 4 decimals with the 2 ... remember what the associated actuarial calculations were. ...
    (comp.databases.pick)
  • Re: Precision
    ... There is nothing wrong with the D3 floating point algorithm or anyone else's ... It's the same if we use Precision, floating point, or ... positions it is IMPOSIBLE to have a result of 4 decimals with the 2 ... remember what the associated actuarial calculations were. ...
    (comp.databases.pick)
  • RE: Packed decimal arithmetic in C
    ... >many decimals to ... >> I once used double precision floating point in exactly that manner. ... >applications that track currency and need to do so exactly. ... >A scaled integer meets the requirement of exactness. ...
    (comp.os.vms)
  • Re: Precision
    ... Whether one does the subsequent calculations in internal ie integer format ... I have always used external format with Precision 4 since my ... floating point calcs which are done in binary have around 3% error albeit at ... positions it is IMPOSIBLE to have a result of 4 decimals with the 2 ...
    (comp.databases.pick)