Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

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

by Ovid (Cardinal)
on Sep 23, 2003 at 13:41 UTC ( [id://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 !! )

    -T

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (5)
As of 2024-04-24 12:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found