From: Pete Becker on
Louis Lavery wrote:
>
> So, does floating point < define a strict weak ordering?
>
> If you avoid NANs (and as -0.0 and +0.0 behave as equals when compared)
> then yes.
>

Good point.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of
"The Standard C++ Library Extensions: a Tutorial and Reference"
(www.petebecker.com/tr1book)

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Hyman Rosen on
On 3/3/2010 3:27 PM, Pete Becker wrote:
> It's really not that much different from this:
> int a = 1/3; int b = a * 3; assert(b == 1);
> Gasp! The assert triggers! == isn't doing what it's supposed to do.

But what about this?
double a = 1; double b = 3; assert(a/b == 1.0/3.0);

This isn't just people not understanding floating point,
it's the language definition deliberately conspiring
against making it be understandable.

(For the uninitiated, the problem is 5/10.)

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Andy Johnson on
> So, does floating point < define a strict weak ordering?

> If you avoid NANs (and as -0.0 and +0.0 behave as equals when compared)
> then yes.

This was kinda the answer I was hoping for but the responses are far
from conclusive.

Is this in the standard anywhere?

I agree with the general opinion that without knowledge of what the
floating point number represents it pretty difficult to determine how
they should logically compare (what constitutes representation error
vs actual difference).

I might err on the side of caution and decide on an epilson tailored
to my specific problem.

Thanks for all the responses

AJ

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Louis Lavery on
Andy Johnson wrote:
>> So, does floating point < define a strict weak ordering?
>
>> If you avoid NANs (and as -0.0 and +0.0 behave as equals when compared)
>> then yes.
>
> This was kinda the answer I was hoping for but the responses are far
> from conclusive.
>
> Is this in the standard anywhere?
>
> I agree with the general opinion that without knowledge of what the
> floating point number represents it pretty difficult to determine how
> they should logically compare (what constitutes representation error
> vs actual difference).
>

I don't get what you mean here. To me, a float represents a float.

I think you should decide what type of number you're trying to model
and then ask if floats can model (or be made to model) that type, not
the other way round.

> I might err on the side of caution and decide on an epilson tailored
> to my specific problem.

Using epsilon will almost guarantee you don't have a strict weak order.

That is if floats differing by no more than epsilon[1] compare equal
then you no longer have transitivity of equivalence.

Louis.

[1] Unless epsilon is 0, of course.

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Pete Becker on
Andy Johnson wrote:
>> So, does floating point < define a strict weak ordering?
>
>> If you avoid NANs (and as -0.0 and +0.0 behave as equals when compared)
>> then yes.
>
> This was kinda the answer I was hoping for but the responses are far
> from conclusive.
>
> Is this in the standard anywhere?
>

Sort of. IEEE-754 prescribes this behavior, and numeric_traits<T> has a
flag that tells you whether a particular type supports IEEE-754. C++0x
goes quit a bit further in this regard, but mandating IEEE-754 just
wouldn't fly because it would be prohibitively expensive on systems that
don't support it in hardware.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of
"The Standard C++ Library Extensions: a Tutorial and Reference"
(www.petebecker.com/tr1book)

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]