Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Re: Decimal Array range

by ikegami (Pope)
on Dec 21, 2010 at 18:14 UTC ( #878331=note: print w/replies, xml ) Need Help??

in reply to Decimal Array range

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.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://878331]
and the fire pops...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (1)
As of 2016-12-09 06:27 GMT
Find Nodes?
    Voting Booth?
    On a regular basis, I'm most likely to spy upon:

    Results (148 votes). Check out past polls.