← Back to context

Comment by astrange

11 years ago

reduce isn't a safe way to add floating-point numbers unless your accumulator is much more precise than your array values. You'll end up with what they call "catastrophic cancellation".

And they won't let you have fixed-point in JavaScript!

Doesn't a for loop have the same problem?

  • If you write the loop yourself, you can add everything to a double instead of a float, or a long double instead of a double, if you have those.

    Oh, if all your numbers are positive you can also sort the array and it will minimize the error accumulated, but I'm not sure what to do with mixed signs.