A couple of comments on dealing with floats. - Use FLT_EPSILON/DBL_EPSILON from float.h
- You method is total overkill, return (fabs(x - y) < DBL_EPSILON ? 1 : 0);
should be sufficent (note: check with a numerical expert).
- What about NANs and INFs? I typical test for one of the values being
NAN but allow INFs to be compared.
-J
--
On Tue, Jul 31, 2007 at 07:17:15PM +0200, Paul Cochrane wrote:
> Hi all,
>
> I'm wanting to remove all the floating point comparison warnings which
> appear when compiling parrot and wanted some advice as to how best to
> achieve this.
>
> Floating point comparisons appear in code such as (taken from src/string.c):
>
> if (*p == '-' && f == 0.0)
>
> I'd like to replace this with
>
> if (*p == '-' && is_float_equal(f, 0.0))
>
> where
>
> #define EPSILON 0.0000005
>
> INTVAL is_float_equal(FLOATVAL x, FLOATVAL y)
> {
> return (fabs(x - y) <= fabs(x + y)*EPSILON) ? 1 : 0;
> }
>
> (this code was adapted from equivalent fortran code given here:
> http://www.lahey.com/float.htm courtesy of particle++).
>
> Is this a good way to achieve the goal of safe floating point
> comparisons? The #define and function need to go somewhere. Where
> exactly? And which header gets the declaration? parrot.h? Is there
> a better way to do this? Could/should I be using a macro instead?
>
> Thanks heaps in advance!
>
> Paul
pgp2T0yj1dqL1.pgp
Description: PGP signature
