I agree that it does seem odd that perl apparently uses a different rounding methodology for the int() function versus doing the calculation in print. But, there is no cut and dry way to handle this. sprintf and printf use round half to even. If you want a explanation of all of the different rounding strategies then search wikipedia for round half to even.
foreach my $i (0.5, 1.5, 1.7, 2.3 ,2.5, 2.7){ printf "%2.0f", $i; }
Output: 0 2 2 2 2 3 As you can see, it does round toward even. Here is another example with int() that doesn't work the way you think it would:
print int(0.6/0.2);
You would think the above would print 3, but it prints 2. The following works like you expect and prints 3.
print 0.6/0.2;
The short is that there are several methods for rounding, and you really need to know when and where each method is used or you don't use floating point maths to calculate money until displayed for output. Also, you should read "What Every Computer Scientist Should Know About Floating-Point Arithmetic". You can find it by googling. People have different uses for different rounding schemes, again read about rounding on wikipedia. These different needs means that not all functions will round the same. By the way, this is not just a perl problem/difference.

In reply to Re: Floating point problems by shevek
in thread Floating point problems by bluescreen

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.