java - Does the sum of numbers depend on the sequence in which they are added? -
it intuition far sum set of numbers independent of order in added. in following, set of random numbers determined seed=0, sequence determined order of execution in threads.
i use sum of large number doubles multi-threaded computation checksum. there way find rounding scheme sum maximally sensitive constituent numbers in sum, insensitive particular random sequence of additions?
import java.io.ioexception; import java.util.arraylist; import java.util.random; import java.util.concurrent.callable; import java.util.concurrent.executionexception; import java.util.concurrent.executorservice; import java.util.concurrent.executors; import java.util.concurrent.future; public class test implements callable<double> { public static class sum { double sum = 0; public synchronized void add(double val) { sum += val; } public double getsum() { return sum; } }; sum sum; public test(sum sum) { this.sum = sum; } @override public double call() { random rand = new random(0); (long = 0; < 1000000l; i++) { sum.add(rand.nextdouble()); } return 0d; } static double mean() { sum sum = new sum(); int cores = runtime.getruntime().availableprocessors(); executorservice pool = executors.newfixedthreadpool(cores); arraylist<future<double>> results = new arraylist<>(); double x = 0; (int = 0; < cores; i++) { test test = new test(sum); results.add(pool.submit(test)); } (future<double> entry : results) { try { x += entry.get(); } catch (interruptedexception ex) { throw new runtimeexception("thread interrupted.", ex); } catch (executionexception ex) { throw new runtimeexception("excecution exception:"); } } pool.shutdown(); return sum.getsum(); } public static void main(string[] args) throws ioexception { (int = 0; < 10; i++) { system.out.format("avg:%22.20f\n", mean()); } } }
assuming data structures synchronised, order should not affect final sum, provided operations commutative.
in other words, provided a + b
identical b + a
.
that's not case floating point numbers since are, after all, approximation of number want.
adding two numbers (a
, b
above) commutative becomes more complex when quantity of numbers becomes bigger.
for example, if add smallest possible number (relatively) large number, fact have precision means you'll end larger number, example:
-20 1 + 10 => 1
so, if add 10-20
1
lot of times (1020 exact), you'll still end 1
:
-20 -20 -20 -20 -20 -20 1 + 10 + 10 + 10 ... + 10 + 10 + 10 => 1 \__________________________________________/ 20 10 of these
however, if first add 10-20
values, you'll end 1
(a), adding 1
give 2
:
-20 -20 -20 -20 -20 -20 10 + 10 + 10 ... + 10 + 10 + 10 + 1 => 2 \__________________________________________/ 20 10 of these
(a) isn't quite true since accumulated amount stop increasing becomes large enough relative 10-20
value have 0 effect on it.
however, won't @ point accumulated amount 0 should see difference in final sums.
Comments
Post a Comment