Mark McIntyre <(E-Mail Removed)> writes:

> On Wed, 05 Sep 2007 20:43:45 +0200, in comp.lang.c , Pietro Cerutti

> <(E-Mail Removed)> wrote:

>

>>> ./test

>>0.900000 and 0.900000 are unequal

>>0.900000 and 0.900000 are equal

>>

>>Given the above, is the standard to be considered right in saying that

>>== is defined for arithmetic (including double) values?

>

> Defined and guaranteed are different.

>

> The result is defined - its zero if the arguments are identical,

> nonzero otherwise.
The result is 1 if the arguments are identical, 0 otherwise.

> The result is however not guaranteed to be what you expect, based on

> the laws of maths, because computers have finite precision.
Right.

Suppose you have two numbers that would be mathematically equal if the

computer operated on infintely precise real numbers, but that are

unequal becase of rounding errors. The real question is not so much

whether they're equal; it's *how should your program behave*? That's

something that only you can decide.

Note that comparing the difference to FLT_EPSILON or DBL_EPSILON, as

splint suggests, For one thing, FLT_EPSILON and DBL_EPSILON are

relevant only for numbers close to 1.0 or -1.0. For another,

accumulated roundoff errors can become arbitrarily large, depending on

how you implement your algorithm.

To summarize: "Math is hard".

See also David Goldberg's classic paper "What Every Computer Scientist

Should Know About Floating-Point Arithmetic" (Google it).

--

Keith Thompson (The_Other_Keith)

(E-Mail Removed) <http://www.ghoti.net/~kst>

San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>

"We must do something. This is something. Therefore, we must do this."

-- Antony Jay and Jonathan Lynn, "Yes Minister"