Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #34310 > unrolled thread
| Started by | Andrew Poulos <ap_prog@hotmail.com> |
|---|---|
| First post | 2017-11-12 11:09 +1100 |
| Last post | 2018-02-15 12:12 -0800 |
| Articles | 4 — 4 participants |
Back to article view | Back to comp.lang.javascript
sum array Andrew Poulos <ap_prog@hotmail.com> - 2017-11-12 11:09 +1100
Re: sum array Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-11-12 01:20 +0000
Re: sum array Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2018-02-14 09:57 +0100
Re: sum array "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2018-02-15 12:12 -0800
| From | Andrew Poulos <ap_prog@hotmail.com> |
|---|---|
| Date | 2017-11-12 11:09 +1100 |
| Subject | sum array |
| Message-ID | <HM6dnRdhLcy-DJrHnZ2dnUU7-a3NnZ2d@westnet.com.au> |
To sum the integer elements in an array I'm using
var sum = arr.reduce(function(a, b) {
return a + b;
});
which is fine. When I pass it
var arr = [,0, 1, -1, 3,, null];
I get the correct answer though I'm expecting it to error. Why doesn't
it throw an error or at least not the right answer?
Andrew Poulos
[toc] | [next] | [standalone]
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Date | 2017-11-12 01:20 +0000 |
| Message-ID | <878tfcwaw9.fsf@bsb.me.uk> |
| In reply to | #34310 |
Andrew Poulos <ap_prog@hotmail.com> writes:
> To sum the integer elements in an array I'm using
>
> var sum = arr.reduce(function(a, b) {
> return a + b;
> });
>
> which is fine. When I pass it
> var arr = [,0, 1, -1, 3,, null];
> I get the correct answer though I'm expecting it to error. Why doesn't
> it throw an error or at least not the right answer?
First, reduce only considers the elements that are present in the array,
and second, null is converted to +0 before the addition (non-string operands
of + are converted to number using the the ToNumber abstract operation).
--
Ben.
[toc] | [prev] | [next] | [standalone]
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Date | 2018-02-14 09:57 +0100 |
| Message-ID | <11978900.uLZWGnKmhe@PointedEars.de> |
| In reply to | #34310 |
Andrew Poulos wrote:
> To sum the integer elements in an array I'm using
>
> var sum = arr.reduce(function(a, b) {
> return a + b;
> });
>
> which is fine.
Actually, it is NOT.
> […]
> var arr = [,0, 1, -1, 3,, …];
This approach is (more or less) fine with *those* numbers and in general
with numbers that require little precision.
But as I learned recently, it is prone to “catastrophic cancellation”
in ECMAScript implementations, too, because they use IEEE-754 “double
precision” *floating-point* values for numbers by default.
Therefore, I have added implementations of the Kahan–Babuška, Kahan–Babuška–
Neumaier and pairwise summation algorithms to JSX:math/float.js yesterday,
and I am going to refactor the corresponding methods (e.g.
JSX:math/algebra.js#jsx.math.Vector.prototype.dot()) to use Kahan–Babuška–
Neumaier summation.
<https://en.wikipedia.org/wiki/Loss_of_significance>
--
PointedEars
FAQ: <http://PointedEars.de/faq> | <http://PointedEars.de/es-matrix>
<https://github.com/PointedEars> | <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | Please do not cc me./Bitte keine Kopien per E-Mail.
[toc] | [prev] | [next] | [standalone]
| From | "Michael Haufe (TNO)" <tno@thenewobjective.com> |
|---|---|
| Date | 2018-02-15 12:12 -0800 |
| Message-ID | <561c71b7-2e6d-44fe-a702-757c2581ba25@googlegroups.com> |
| In reply to | #34734 |
Thomas 'PointedEars' Lahn wrote: > But as I learned recently, it is prone to “catastrophic cancellation” > in ECMAScript implementations, too, because they use IEEE-754 “double > precision” *floating-point* values for numbers by default. > > Therefore, I have added implementations of the Kahan–Babuška, Kahan–Babuška– > Neumaier and pairwise summation algorithms to JSX:math/float.js yesterday, > and I am going to refactor the corresponding methods (e.g. > JSX:math/algebra.js#jsx.math.Vector.prototype.dot()) to use Kahan–Babuška– > Neumaier summation. > > <https://en.wikipedia.org/wiki/Loss_of_significance> +1 Aside: "Nothing brings fear to my heart more than a floating point number. Numerical analysis is the blackest art I know." — Gerald Jay Sussman Ref: <https://www.youtube.com/watch?v=O3tVctB_VSU>
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.javascript
csiph-web