http://qs321.pair.com?node_id=293493


in reply to My floating point comparison does not work. Why ?

Computers generally don't handle floating point numbers the way we expect them to. For instance, while you might think a number is 4.3, the computer might store it as 4.2999999999999. For most applications, this is probably fine, but you have to round the results when you display them. To compare two floating point numbers, use the sprintf function, figure out how many significant digits you need and compare with a string equal comparison operator (eq).

sub floats_eq { my ($num1, $num2, $sig) = @_; $_ = sprintf "%.${sig}g" foreach $num1, $num2; return $num1 eq $num2; }

Replies are listed 'Best First'.
Re: Answer: My floating point comparison does not work. Why ?
by thens (Scribe) on Sep 23, 2003 at 15:11 UTC

    But I guess the round off operation is very costly and may not be a good solution when you are dealing with large sets of data to be compared.

    When perfomance is a concern testing for the range is a better option than rounding off the numbers and then doing a string compare ( which is again costly !! )

    -T