Pathologically Eclectic Rubbish Lister PerlMonks

### Comment on

 Need Help??

You're thinking of something like BASIC's STEP clause, in effect. You can achieve this using a C-style loop:

```for (my \$i = 0; \$i < 100; \$i += 0.1) {
say \$i;
}

but then you can end up with an accumulation of floating point error.*

```...
99.7999999999986
99.8999999999986
99.9999999999986

The solution is to use an integer for the loop counter, and calculate the decimal number from it.

```for (my \$_ = 0*10; \$_ < 100*10; ++\$_) {
my \$i = \$_/10;
say \$i;
}
```...
99.7
99.8
99.9

Now that you're using integers, you can use a cleaner syntax.

```for (0*10..100*10) {
my \$i = \$_/10;
say \$i;
}

You already been shown how this get the numbers as a list.

```map \$_/10, 0*10..100*10

* — A lot of numbers are periodic in binary. For example, 1/10 is perodic is binary, just like 1/3 is periodic in decimal. It would take infinite storage to store 1/10 as a floating point number.

In reply to Re: Decimal Array range by ikegami
in thread Decimal Array range by packetstormer

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.
• Posts may use any of the Perl Monks Approved HTML tags:
a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, 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, 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: & & < < > > [ [ ] ]
• Link using PerlMonks shortcuts! What shortcuts can I use for linking?

Create A New User
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (2)
As of 2017-06-27 22:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
How many monitors do you use while coding?

Results (617 votes). Check out past polls.