From: Andy Johnson on
Hi All

I've searched for a definitive solution but couldn't find one. So I
came here...

Can I use a floating point number (either single or double precision)
as the key for a map (and it work correctly)?

I'm aware of the issues with floating point equality but as the map is
doing equivalence (strict weak ordering) wondered whether it would
just work or will I have to write my own compare functor.

Thanks

AJ


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

From: restor on
> Can I use a floating point number (either single or double precision)
> as the key for a map (and it work correctly)?
>
> I'm aware of the issues with floating point equality but as the map is
> doing equivalence (strict weak ordering) wondered whether it would
> just work or will I have to write my own compare functor.

I do not know all the tricks of floating point numbers, but the ones I
know from other people in that forum, that
the comparison of two close values may give different result at
different times due to fairly "random" reasons (like whether the
numbers are stored in registers or RAM), I am pretty sure that map of
floats with default comparison function is a bad idea. A custom
functor may be a solution, but it surely cannot be a function like:

bool less( double x, double y ) {
return x + EPSILON < y;
}

This is discussed in http://cpp-next.com/archive/2010/02/order-i-say/
Look for Sean Parent's comments.

Regards,
&rzej

--
[ 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:
>
> I've searched for a definitive solution but couldn't find one. So I
> came here...
>
> Can I use a floating point number (either single or double precision)
> as the key for a map (and it work correctly)?
>
> I'm aware of the issues with floating point equality but as the map is
> doing equivalence (strict weak ordering) wondered whether it would
> just work or will I have to write my own compare functor.
>

Despite the drivel that's written in newsgroups, equality of
floating-point values is well defined and useful. Don't be afraid of it.

--
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: Ulrich Eckhardt on
Andy Johnson wrote:
> Can I use a floating point number (either single or double precision)
> as the key for a map (and it work correctly)?

Define "correctly". You will have numbers that are almost indistinguishable
from each other but compare unequal.

> I'm aware of the issues with floating point equality but as the map is
> doing equivalence (strict weak ordering) wondered whether it would
> just work or will I have to write my own compare functor.

The only values that would not work correctly would be NaNs. IIRC, the
following holds:

NaN < x == false
x < NaN == false
(x==Nan) == false

Uli

--
Sator Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932


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

From: Bart van Ingen Schenau on
On Mar 2, 8:30 pm, Andy Johnson <ajcpp...(a)gmail.com> wrote:
> Hi All
>
> I've searched for a definitive solution but couldn't find one. So I
> came here...
>
> Can I use a floating point number (either single or double precision)
> as the key for a map (and it work correctly)?
>
> I'm aware of the issues with floating point equality but as the map is
> doing equivalence (strict weak ordering) wondered whether it would
> just work or will I have to write my own compare functor.

It will work, in the sense that the compiler will be completely happy
with the code and you are not invoking the demons of UB when you use
float or double as your key.

Just be aware that finding a particular key might not work as you
expect, due to differences in precision/rount-off error between the
stored key and the value you are trying to match it with.

>
> Thanks
>
> AJ
>
Bart v Ingen Schenau


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