[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: While
From: |
Paul Pluzhnikov |
Subject: |
Re: While |
Date: |
Sun, 26 Mar 2006 20:32:33 -0800 |
User-agent: |
Gnus/5.1006 (Gnus v5.10.6) XEmacs/21.4 (Jumbo Shrimp, linux) |
Wilhelm Korrengk <wilhelm@korrengk.de> writes:
> even if I`m not THE Pro coding c++
> I thought I`m already better not to ask the following:
... code expecting double equality ...
> It does not matter whether i is positive or negative while will
> stop when i is 0.
>
> But is doesn`t.
You need to read up on float/double representation in the computer:
http://docs.sun.com/source/806-3568/ncg_goldberg.html
You should note that 0.1 is *not* representable exactly when using
IEEE-standard representation.
You should *never* compare two floats/doubles for equality.
Instead, always to this: if (abs(x - y) < epsilon) ...
Or, in the case of your program:
while (1.e-10 < i) { ... }
Also note that 'i' is ideomatically used as an *integer* index.
Use 'd' or some descriptive name instead.
Cheers,
--
In order to understand recursion you must first understand recursion.
Remove /-nsp/ for email.
- While, Wilhelm Korrengk, 2006/03/26
- Re: While,
Paul Pluzhnikov <=