Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

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

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

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


Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://293493]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (2)
As of 2021-09-21 18:28 GMT
Find Nodes?
    Voting Booth?

    No recent polls found