From: Hyman Rosen on
On 3/4/2010 3:07 PM, Andy Johnson wrote:
> What I mean is, say I have a sensor producing a continous value to
> within some error/tolerance then I can set Epsilon to a value that
> appropriately distinguishes between what is error due to floating
> point representation and what represents an actual change in input.

You cannot use the standard library methods that
require strict-weak comparisons with a comparator
that says |α - β| ≤ ε → α = β, because equality
must be transitive, while the formula above will
give α - ε = α and α + ε = α but α - ε � α + ε.
This is true no matter what positive value ε is.

If your sensor is producing values accurate to a
certain tolerance, round the values to that tolerance
before comparing. Or just divide the result by the
tolerance and use integers. Or switch to Ada, which
has fixed-point reals with specifiable tolerance.


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

From: Geert-Jan Giezeman on
Andy Johnson wrote:
> On 4 Mar, 10:22, Louis Lavery <Lo...(a)LaverREMOVE.demon.co.uk> wrote:
>> 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.
>
> What I mean is, say I have a sensor producing a continous value to
> within some error/tolerance then I can set Epsilon to a value that
> appropriately distinguishes between what is error due to floating
> point representation and what represents an actual change in input.
>
>> That is if floats differing by no more than epsilon[1] compare equal
>> then you no longer have transitivity of equivalence.
>
> I wasn't really referring to numeric_limits::epsilon, more to a custom
> value (much larger than epsilon<float>) tailored to my specific
> problem.

I think you should use float with standard comparison for the map (to
have a valid order), but should use lower_bound or upper_bound and not
find for querying the map. This will give an iterator, which will allow
for checking close by values.

For instance:

bool has_approximate_value(set<float> const &s, float x, float eps)
{
set<float>::const_iterator it = s.lower_bound(x-eps);
if (it==s.end() || *it > x+eps)
return false;
return true;
}


Geert-Jan Giezeman


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