1/10, 8/10 and 1/100 are all periodic numbers in binary just like 1/3 is a periodic number in decimal.

____ 0.00011 # 1/10 ____ 0.1100 # 8/10 ____________________ 0.0000001010001111010111 # 1/100

As such, they can't be accurately represented as floating-point numbers.

$ perl -e'printf "%.100g\n", $ARGV[0]' 0.1 0.1000000000000000055511151231257827021181583404541015625 $ perl -e'printf "%.100g\n", $ARGV[0]' 0.8 0.8000000000000000444089209850062616169452667236328125 $ perl -e'printf "%.100g\n", $ARGV[0]' 0.01 0.01000000000000000020816681711721685132943093776702880859375

See What Every Computer Scientist Should Know About Floating-Point Arithmetic.

Solution:

for (0..69) ( my $x = ( 80 - $_ ) / 100; say $x; }

This will prevent error from accumulating, keeping it under the default rounding. You could also perform more forgiving rounding than the default. Or you could avoid floating point numbers entirely (e.g. using rational number libraries).

Seeking work! You can reach me at ikegami@adaelis.com


In reply to Re: what did I just see..? by ikegami
in thread what did I just see..? by ishaybas

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.