Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

RE: RE: Round a Number to Any Place

by tye (Sage)
on Aug 30, 2000 at 09:31 UTC ( [id://30262]=note: print w/replies, xml ) Need Help??


in reply to RE: Round a Number to Any Place
in thread Round a Number to Any Place

In chatterbox, I (belatedly) noticed Adam wonder why his code failed for round(-4.35,0.1). I think the reason helps to illustrate why I didn't worry about rounding to the nearest even. I can sum up the situation pretty easily with this:

% perl -le "print 4.35*100-435" -5.6843418860808e-014

A floating point number can never be exactly 0.05. Floating point numbers are represented in binary and have a limited number of bits. 0.05 in binary requires an infinite number of bits (forming a repeating pattern, similar to how 1/7 is 0.142857142857... in decimal). So the extra code to round to the nearest even will probably never trigger when rounding to 1 or more places after the decimal point (I say "probably" because I don't claim to be able to predict the interactions of limited floating point precision on the calculations being done and it is possible that things would cancel out and trigger the extra code, perhaps in cases where it really shouldn't have).

Okay, here is my version of Adam's code:

sub round { my( $num, $prec )= @_; $num= int( my $frac= $num/$prec + 0.5 - ($num<0) ); $num -= ($num<=>0) if $num == $frac && $num % 2; return $num*$prec; }
        - tye (but my friends call me "Tye")

Replies are listed 'Best First'.
RE: RE: RE: Round a Number to Any Place
by Adam (Vicar) on Aug 30, 2000 at 20:29 UTC
    That makes sense, I don't know why I didn't think of that. Thanks!

      I didn't think of it either. But I did immediately jump into the Perl debugger and found that sufficient so I didn't have to "resort to" adding print statments. ;)

              - tye (but my friends call me "Tye")
      (Sneaky "persistent preview" for Coming soon: Algorithm::Loops finally removed from this out-of-the-way node after all of this time)
        Ouch... That stings! <grin>.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2024-04-25 23:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found