Actually, round to even would be correct for numbers ending in .5

This is new to me, up to now I always rounded .5 up. But you seem to be right, e.g. Mathematica rounds in that way. The reason for this is that the rounding should not give a statistical bias - as far as I understand it.

But in this case, 1.025 really is closer to 1.02 printf "%.20f => %.2f\n", 1.025, 1.025;
# this prints 1.02499999999999990000 => 1.02
D'oh, well, been tripped up again by floating point number representations (tye explains that very nicely here in this thread) ...

What is interesting is the following example where the rounding towards the nearest even number (0) does not take place - and the 0.5 can be represented exactly as a float (as merlyn pointed out somewhere else in this node):

printf "%.20f => %.0f", 0.5, 0.5; # prints 0.50000000000000000000 => 1

-- Hofmator


In reply to Re4: Filthy Floats by Hofmator
in thread Filthy Floats by THuG

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.