Re: Weirdness: (-0.666667 + 0.333333)

From: Richard Herring (junk_at_[127.0.0.1)
Date: 05/07/04


Date: Fri, 7 May 2004 11:07:08 +0100

In message <ZK2dnZEXiNV0ewTd4p2dnA@is.co.za>, Corne' Cornelius
<cornec@nospam-for-noreason.uy> writes
>i should've made it more clear what i actually want to do.
>
>I'm not really trying to compare 2 double which could be in accurate
>becuase of rounding errors.
>
>I'm writing a program that draws graphs on a cartesian plane, so in
>order to go on the x-axis from say -2, to +2, in 20 intervals, i need
>one of these point to be zero or very close to it.
>
>how could i make sure i don't get rounding problems ?
>

You can often improve matters by using an integer loop counter:

instead of: for (double x=xstart; x<xend; x+=step) {...}

use something like: for (int i=istart; i<iend; ++i) { double x=i*xstep;
...}

It doesn't get rid of the rounding errors, but it does ensure that they
don't accumulate as you progress through the loop.

-- 
Richard Herring


Relevant Pages

  • Re: Weirdness: (-0.666667 + 0.333333)
    ... > becuase of rounding errors. ... > I'm writing a program that draws graphs on a cartesian plane, ... The only thing you can do is to compare your value of x to zero ...
    (comp.lang.cpp)
  • Re: addition of 0.7 and 0.1 is not 0.8 CDbl / CStr vbs problem
    ... values are stored there will be very small rounding errors. ... Comparing floating point values should be done using a tolerance:- ... Most newbies find this situation shocking yet it's pratically as old as ... the situation using CDbl) than to compare with "almost near ...
    (microsoft.public.scripting.vbscript)
  • Re: Weirdness: (-0.666667 + 0.333333)
    ... I'm not really trying to compare 2 double which could be in accurate ... becuase of rounding errors. ... > I'm experiencing some weirdness in a program, ...
    (comp.lang.cpp)