in reply to Parallelization of multiple nested loops

I would like to add that usually you should avoid using non-integer numbers as control variables in loops. The float representation used by the computer may introduce rounding errors and break your logic.

For instance, on my computer:

for ($i = 0; $i < 1; $i += 0.2) {}; say $i; #==> 1.0 for ($i = 0; $i < 2; $i += 0.2) {}; say $i; #==> 2.2

So, the first loops works right, but the second one runs an extra iteration unexpectedly!

The right approach in this cases is to use an intermediate integer variable:

for ($ix = 0; $ix < 10; $ix++) { my $i = $ix * 0.2; ... }

Replies are listed 'Best First'.
Re^2: Parallelization of multiple nested loops
by roboticus (Chancellor) on Feb 07, 2018 at 16:33 UTC

    salva, biosub:

    I'd suggest something like:

    # customize to suit my @param_space = (0.0, 0.2, 0.4, 0.6, 0.8, 1.0); for my $i (@param_space) { for my $j (@param_space) { ... } }

    This clarifies the code a little and might remove a few operations from the optree. It makes it simple to change the distribution (if desired), and you can use more arrays if you want to treat some parameters slightly differently.

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.