Any integer + .005 is not going to be exactly representable in floating point. Sometimes you are getting a repesentable value above the true one and sometimes one below. Try this:
for my $num ( 0 .. 101 ) { $num += 0.005; printf "%.2f\t%.20g\n", $num, $num; }
to see the actual values you are rounding and it should make more sense. (The output starts:)
0.01 0.0050000000000000001041 1.00 1.0049999999999998934 2.00 2.0049999999999998934 3.00 3.0049999999999998934 4.00 4.0049999999999998934 5.00 5.0049999999999998934 6.00 6.0049999999999998934 7.00 7.0049999999999998934 8.01 8.0050000000000007816 9.01 9.0050000000000007816 10.01 10.005000000000000782 11.01 11.005000000000000782 12.01 12.005000000000000782 13.01 13.005000000000000782 14.01 14.005000000000000782 15.01 15.005000000000000782 16.00 16.004999999999999005

In reply to Re: (s)printf and rounding woes by ysth
in thread (s)printf and rounding woes by Not_a_Number

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.