Obviously, it's possible to use floats as increments in for loops: you're doing it. It's just a bad idea, at least partly because accumulation of errors means that the number of loop iterations cannot be guaranteed to be identical across platforms (or even across two versions of Perl built with different compiler options on the same machine).

As mentioned by prior posters, 0.1 cannot be exactly represented in floating point, as 0.1 cannot be exactly represented as a finite sum of the powers of 2, in the same way that 1/3 cannot be exactly represented as a finite sum of powers of 10. For this reason, x = 5 + 0.1 will look like 5.0999999, because 5.1 can't be exactly represented. Floating point numbers are not real numbers; they are only approximations to real numbers, and that only within a finite range. This article will make this point much clearly than I.


Information about American English usage here and here. Floating point issues? Please read this before posting. — emc


In reply to Re: for loop with float by swampyankee
in thread for loop with float by RobertCraven

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.