Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Answer: My floating point comparison does not work. Why ?

by Ovid (Cardinal)
on Sep 23, 2003 at 13:41 UTC ( #293493=categorized answer: print w/replies, xml ) Need Help??

Q&A > math > My floating point comparison does not work. Why ? - Answer contributed by Ovid

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 !! )


Log In?

What's my password?
Create A New User
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2021-01-28 03:19 GMT
Find Nodes?
    Voting Booth?