Iterate through spread - find sum of all slices

I know this is simple but I can’t quite see it. In csharp I would have a variable to store the running total, and using a for loop I would iterate through each ‘slice’, adding it’s value to this variable.

How is this done in VL? I wouldn’t have thought you’d need much more than a pad, a +, and a foreach…

+, foreach and an accumulator that stores the sum and passes it on to the next iteration.
there is also the Sum node for sequences (Spread is a Sequence) that does this for you. you can click open to see how the Sum node is patched…

How do you add the accumulator to the foreach?

start a link (for example from the plus) and click in the accumulator bar that should highlight as soon as you start the link.

Great, got it.

Maybe it should be filed as a bug but I noticed that trying to produce the sum of a float in this manner may result in an error.

The results start to diverge from 5794.

yes, that makes sense, Float32 is not capable of representing integer numbers that high:

Floating point numbers have an insane value range, but its important to understand that there is only a certain number of digits of precision.
This also means that the maximum consecutive integer that can be represented is 2^24 = 16,777,216 for Float32 and 2^54 = 9,007,199,254,740,992 for Float64, keep that in mind when connecting integers to float inputs.

read the section “Value Range” here:

1 Like

Thanks, its the limit that could really cause grief on a project.

Also your examples are a good indication of the perfomance difference an operation has over a record.

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.