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

In reply to RE: RE: Round a Number to Any Place by tye
in thread Round a Number to Any Place by reptile

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.