Hi,

could someone please explain this behaviour of the following snippet to me?

for my $i (1..20) { print "$i: "; print (1-1/2000)**$i; print " - "; print (1-1/2000)**int($i); print " - "; print "" . (1-1/2000)**$i; print "\n"; }
This prints on my 5.18.1 on Debian:
1: 0.9995 - 0.9995 - 0.9995 2: 0.9995 - 0.9995 - 0.99900025 3: 0.9995 - 0.9995 - 0.998500749875 4: 0.9995 - 0.9995 - 0.998001499500063 5: 0.9995 - 0.9995 - 0.997502498750313 6: 0.9995 - 0.9995 - 0.997003747500938 7: 0.9995 - 0.9995 - 0.996505245627187 8: 0.9995 - 0.9995 - 0.996006993004374 9: 0.9995 - 0.9995 - 0.995508989507872 10: 0.9995 - 0.9995 - 0.995011235013118 11: 0.9995 - 0.9995 - 0.994513729395611 12: 0.9995 - 0.9995 - 0.994016472530913 13: 0.9995 - 0.9995 - 0.993519464294648 14: 0.9995 - 0.9995 - 0.993022704562501 15: 0.9995 - 0.9995 - 0.99252619321022 16: 0.9995 - 0.9995 - 0.992029930113615 17: 0.9995 - 0.9995 - 0.991533915148558 18: 0.9995 - 0.9995 - 0.991038148190984 19: 0.9995 - 0.9995 - 0.990542629116888 20: 0.9995 - 0.9995 - 0.99004735780233
So for some reason I get the correct numerical value of the expression (1-1/2000)**$i only when I force stringification...

Why is that?


In reply to strange arithmetic by morgon

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.