Round to even doesn't work so well on floating point, unless you are rounding to an integer. This is because it is supposed to do round to closest unless the amount rounded off is 1/2th, 1/20th, 1/200th, 1/2000th, etc. (depending on what decimal place you are rounding to), and of those, only 1/2 is representable in floating point. None of the others will necessarily exactly occur, so the special round-to-even rule is not always actually invoked.

What you really ought to do is pick an epsilon that represents how much off your floating point number may be from what the actual infinitely precise value would have been - taking into account both the inaccuracy of floating point representation and any round-off errors involved in calculations that led to the number in question. Given the latter factor, there is no one epsilon that will suit any problem. Then you apply the round-to-even rule if your number is within epsilon of 1/20th, 1/200th, etc.

I have the suspicion that the ffround is working because at some point the number is converted to string form, and then to a bigfloat internal form, and the conversion to string does a slight amount of rounding for you. If so, it won't necessarily work 100% of the time.


In reply to Re: Proper monetary rounding by ysth
in thread Proper monetary rounding by blogical

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.