Velocity Reviews > C++ > Weirdness: (-0.666667 + 0.333333)

# Weirdness: (-0.666667 + 0.333333)

Gert Van den Eynde
Guest
Posts: n/a

 05-06-2004
Corne' Cornelius wrote:

> 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't. The only thing you can do is to compare your value of x to zero
(using fcmp) and if it's close enough force it to be zero. Btw, Is the
difference between 1e-16 and 0.0 so significant that it will show up on a
graph in the range [-1,1] ?

bye,
gert

Corne' Cornelius
Guest
Posts: n/a

 05-06-2004
heh, i also thaught of not worrying about it being 1e-16, but then
there's a pixel missing where x is 0.

Looks like i will have to make 2 loops and 1 statement.

for when:
x - epsilon() < 0
x = 0
x + epsilon() > 0

Gert Van den Eynde wrote:
> Corne' Cornelius wrote:
>
>
>>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't. The only thing you can do is to compare your value of x to zero
> (using fcmp) and if it's close enough force it to be zero. Btw, Is the
> difference between 1e-16 and 0.0 so significant that it will show up on a
> graph in the range [-1,1] ?
>
> bye,
> gert

Karl Heinz Buchegger
Guest
Posts: n/a

 05-06-2004
Corne' Cornelius wrote:
>
> heh, i also thaught of not worrying about it being 1e-16, but then
> there's a pixel missing where x is 0.

Then your transformation from world coordinates to
pixel coordinates is in error, if such a small deviation
from 0 makes that effect.

--
Karl Heinz Buchegger
http://www.velocityreviews.com/forums/(E-Mail Removed)

Karl Heinz Buchegger
Guest
Posts: n/a

 05-06-2004
Karl Heinz Buchegger wrote:
>
> Corne' Cornelius wrote:
> >
> > heh, i also thaught of not worrying about it being 1e-16, but then
> > there's a pixel missing where x is 0.

>
> Then your transformation from world coordinates to
> pixel coordinates is in error, if such a small deviation
> from 0 makes that effect.

The usual error is to truncate instead of round when chopping
of the fractional part in pixel space.

--
Karl Heinz Buchegger
(E-Mail Removed)

Chris Dams
Guest
Posts: n/a

 05-06-2004
Hi!

Corne' Cornelius <(E-Mail Removed)> writes:

>heh, i also thaught of not worrying about it being 1e-16, but then
>there's a pixel missing where x is 0.

If you want to draw a point for every pixels you should loop over pixels
and not over doubles. On the other hand, making small steps and drawing
a straight line between these, is likely to be good enough. Furthermore,
if you draw points for every pixel in the x-direction you should also
worry about what happens if the slope of your graph is bigger than 1.

Bye,
Chris Dams

Richard Herring
Guest
Posts: n/a

 05-07-2004
In message <(E-Mail Removed)>, Corne' Cornelius
<(E-Mail Removed)> 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