Internally, all floating points are represented as a binary fraction (effectively), and there is no binary fraction exactly equal to 8.95. For a double-precision floating-point (which is what NV often are in modern perl binaries; see perl -V:nvsize ), 8.95 is internally closer to 8.9499999999999992894572642399, per this calculator. 100 times that will be slightly less than 895.0, which means that int and floor both truncate it down, whereas printing only 6 digits after the decimal will round up to 895.

What Every Computer Scientist Should Know About Floating-Point Arithmetic

Also, if your perl is new enough, the %a or %A sprintf conversion will show the hex version of the internal floating-point binary representation used:

linux shell: perl -f -e 'printf qq(%A\n), $_ for 8.95, 8.95*100, 895' windows cmd.exe: perl -f -e "printf qq(%A\n), $_ for 8.95, 8.95*100, 895"

Mine shows:

0X1.1E66666666666P+3 0X1.BF7FFFFFFFFFFP+9 0X1.BF8P+9

update: the anonymous post slightly beat me to the punch, though showing a single-precision float value, rather than the double-precision float that is more likely for your perl to be using. In this instance, they both are slightly less than 8.95 originally, so both round to 894 after your multiplication; but in some cases, the single and double-precision values fall on different sides of the rouding boundary, so rounding a float and rounding a double give different values. Everyone who is ever going to program a floating-point application should watch Superman III. (Okay, that's showing my age; slightly more recent is Office Space.)


In reply to Re: Behaviour of int() unexpected by pryrt
in thread Behaviour of int() unexpected by ceade1000

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.