Re: Rounding error when doing float-to-integer

From: Richard Maine (nospam_at_see.signature)
Date: 10/16/03


Date: 15 Oct 2003 16:12:53 -0700

José Rui Faustino de Sousa <jrfsousaKXVAMC@esoterica.pt> writes:

> "Richard Maine" <nospam@see.signature> wrote in message
> > For example, if you have x+y+z in two different places in the
> > code, I don't think there is any basis for believing that the
> > two results will necessarily be the same. In one case, it
> > might be evaluated as (x+y)+z, while in another it might be
> > x+(y+z). Rounding can make those results different.
> >
>
> OK that I knew. But is it still not assured to get the same result if
> one explicitly writes ((x+y)+z)?

Now you are getting into the subtle part. I argue that it is not
assured, but I think there might be people who argue otherwise. I
have too much real work (well, anyway, work other than posting here)
to feel like resurecting that debate, or even checking the fine points
to see whether they apply to exactly this case or not, so I restrict
myself to the following advice.

Regardless of whether or not a strict interpretation of the standard
requires the two evaluations of (x+y)+z to get the exact same result,
the cautious programmer is well advised not to count on it. Some real
compilers in some situations are likely not to get the same result.
You can spend your time debating whether your code should have worked
or you can make sure that your code actually does work. While
admitting the philosophical merits of the abstract debate, I still
advise the cautious programmer to write code that works anyway.

There are very few things that you can in practice count on being
exact in floating point world. I'd say that some of the things that
you *CAN* actually count on in real life are things that the Fortran
standard doesn't guarantee. (Mostly things like that small values
with no fractional part are going to be exactly representable).

-- 
Richard Maine                       |  Good judgment comes from experience;
email: my first.last at org.domain  |  experience comes from bad judgment.
org: nasa, domain: gov              |        -- Mark Twain


Relevant Pages

  • Re: Interface standards (was Re: Dual Port RAM)
    ... to do it anyway) does not qualify it as a 'standard'. ... Although the fifo functionality is pretty low level, ... Both have generics for specifying width of the fifo...but can't agree on the ... implements the EXACT SAME FUNCTION. ...
    (comp.arch.fpga)
  • Re: Float comparison
    ... In addition doubles *are* used to store exact integral values ... that taken in isolation you do NOT know that it is a range. ... The standard actually talks about values that "can be represented exactly" in section 6.3.1.5. ...
    (comp.lang.c)
  • Re: Float comparison
    ... trying to get an exact definition of them. ... If the result is that x contains the exact floating point value ... standard means) MUST allow foe unnormalised numbers. ... I have already explained that I know about floating point representations and have implemented some floating point arithmetic in assembler. ...
    (comp.lang.c)
  • RE: Use of others material - was: Re: Administrator Password Lost
    ... > is an exact copy of one of my standard posts. ... > and attribution. ... > Regards, Jim Byrd, MS-MVP ...
    (microsoft.public.win2000.general)