in reply to Re^4: Why is Zero not 0?
in thread Why is Zero not 0?

$x could come in as -0.00 or -9.049848394839e-14.
But you don't WANT -9.049848394839e-14 to be exactly equal to 0.

And that goes for any comparison between floats, which is why you need to specify your tolerance.

sub comp { my ($x,$y,$delta) = @_; $x -$delta >= $y && $x + $delta <= $y; }

Replies are listed 'Best First'.
Re^6: Why is Zero not 0?
by lodin (Hermit) on Feb 07, 2008 at 02:18 UTC

    Did you get the + and - swapped? Now comp(5, 4.5, 1) is false and comp(5, 4.5, -1) is true.

    Personally I prefer to use the good old standard mathematical way of expressing it:

    if (abs($x - $y) <= $tolerance) { print "$x and $y are 'equal'"; }

    lodin