Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Parallelization of multiple nested loops

by salva (Abbot)
on Feb 07, 2018 at 11:51 UTC ( #1208623=note: print w/replies, xml ) Need Help??


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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1208623]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (2)
As of 2018-07-21 09:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?















    Results (445 votes). Check out past polls.

    Notices?