Boost logo

Boost :

From: Fernando Cacciola (fcacciola_at_[hidden])
Date: 2001-09-28 12:35:45


----- Original Message -----
From: Paul Beardsley <pab_at_[hidden]>
To: <boost_at_[hidden]>
Sent: Friday, September 28, 2001 12:25 PM
Subject: [boost] testing equality of floating point - any libraries?

>
>
> About two years ago, I saw an article in some C++/programming
> journal (whose name I have forgotten) about testing equality
> of floating point numbers. The author had used templates,
> gave a thorough discussion of the all the issues, and
> his solution with source code. Unfortunately I was never
> able to find it when I came to look for it, and all I remember
> is that the author had a maybe Indian name.
>
> Can anyone guide me to the original article, or a good
> C++ implementation for this task? I didn't notice
> anything relevant on the Boost libraries page.
>
I don't think your problem is general enought to have a general answer.
It depends on the numerical properites of the problem domain.
It depends on the kind of numerical tools you are using.

For instance, do you need to work independetly of a particular number type?
If that's the case then you can use a template based implementation of the
required comparison method. Otherwise, templates are unnecesary.

Which is the best comparison method depends on the problem domain.

As a minimun, you need to measure the distance between the two values and
compare that against a certain threshold value:

inline bool equal_e ( double a, double b, double e ) { return fabs(a-b) <= e
; }

However, determining which threshold 'e' to use is too domain specific.
In some applications, it can be a fixed number, such as 1e-5.

In other applications, it can be a number obtained by scaling the machine
epsilon with the arguments:

inline double scaled_epsilon ( double a,
                                             double b,
                                             double e =
numeric_limits<double>::epsilon
                                          )
 { return e * max(fabs(a),fabs(b))) ; }

inline bool equal_e ( double a, double b ) { return equal_e(a,b,
scaled_epsilon(a,b)) ;}

Actually, there are various scaling techniques, depending on the numerical
behavior of your system. For instance, you can use min() instead if max().
You might need to play a little until you find a technique that fits your
needs.

There is a rather involved method described in Knuth's "Seminumerical
Algorithms"; but that method requires you to access the mantisa and exponent
of the arguments, something that can't be done portably; and which might be
imposible for other than built-in types.

You can turn the above methods into template implementations if you need. In
that case, you will need a way to use the apropriate abs() function.

Fernando Cacciola
Sierra s.r.l.
fcacciola_at_[hidden]
www.gosierra.com

> Thanks,
> Paul.
>
> p.s. I know about the C solution at http://fcmp.sourceforge.net/.
>
>
> Info: http://www.boost.org Unsubscribe:
<mailto:boost-unsubscribe_at_[hidden]>
>
> Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
>
>
>


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk