help-gplusplus
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: g++ 3.4.2 strange behavior


From: Paul Schneider
Subject: Re: g++ 3.4.2 strange behavior
Date: Mon, 01 Nov 2004 19:53:26 +0100
User-agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.7.3) Gecko/20041009

Ulrich Eckhardt wrote:
Paul Schneider wrote:
[ (1000.0-0.0)/0.001 yields 99999 ]

where it should be 100000 straight in my opinion. What am I doing wrong.


Try representing 0.001 in binary, it can't be done accurately, just as 1/3
can't in decimal. IOW, the computer has to use a value slightly lower or
higher, which is why your result differs from what you expect. The reason it didn't do so with the other number might be that they were
already computed at compile-time or with higher precision or with a
different algorithm.
Uli

I am well aware of the real number representation problem. I was not aware of the fact that ( I just tried it):

double b = 1000.0;
double a = 0.0;
double c = 0.01;

static_cast<size_t>((b-a)/c);

is different from

static_cast<size_t>((1000.0 - 0.0)/0.01);

as you just suggested. Personally I find this very disturbing. Thanks for your answer.

cheers,

Paul



reply via email to

[Prev in Thread] Current Thread [Next in Thread]