in reply to Re: Defining Arrays
in thread Defining Arrays

Going one step further to prevent floating point math problems:
my @ary = map { int ($_ / 10) . '.' . ($_ % 10) } (15..94);

Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain

Replies are listed 'Best First'.
(tye)Re: Defining Arrays
by tye (Sage) on May 17, 2001 at 19:19 UTC

    FYI, I see no advantage to this method whatsoever.

            - tye (but my friends call me "Tye")
      I don't see it happening in this specific case, but in the general case, I would expect that I'll start seeing floating point errors when you do the division, particulary if you got away from multiples of 10, eg, increments of 0.2, 0.5 or so on. Mind you, I don't think this map solution works as cleanly as in this case (thus resorting to a basic for loop)....
      Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain

        If you were proposing this as an alternative to doing, for example: for( $i= 1.5; $i < 9.5; $i += 0.1 ) { then, yes, you are correct that errors could start to appear.

        But the node that you replied to doesn't suffer from such problems.

        As another data point, if you wanted to go from 1.2 to 3.6 in steps of 0.12, you could do this:

        for( $i= 120; $i <= 360; $i += 12 ) { push @n, $i/100; }
        without risk of errors sneaking in.

        The problem results from repeatedly adding something like 0.1 to a number since, in binary floating point, isn't exactly 0.1:

        printf "%.30f", 0.1 # prints 0.100000000000000010000000000000

                - tye (but my friends call me "Tye")