File tree Expand file tree Collapse file tree
tests/cases/conformance/esnext Expand file tree Collapse file tree Original file line number Diff line number Diff line change 22/// <reference lib="es2025.collection" />
33/// <reference lib="es2025.float16" />
44/// <reference lib="es2025.intl" />
5- /// <reference lib="es2025.iterator" />
5+ /// <reference lib="es2025.iterator" />
66/// <reference lib="es2025.promise" />
77/// <reference lib="es2025.regexp" />
Original file line number Diff line number Diff line change 99/// <reference lib="esnext.typedarrays" />
1010/// <reference lib="esnext.temporal" />
1111/// <reference lib="esnext.date" />
12+ /// <reference lib="esnext.math" />
Original file line number Diff line number Diff line change 1+ /// <reference lib="es2015.iterable" />
2+
3+ interface Math {
4+ /**
5+ * Returns the sum of the values in the iterable using a more precise
6+ * summation algorithm than naive floating-point addition.
7+ * Returns `-0` if the iterable is empty.
8+ * @param numbers An iterable (such as an Array) of numbers.
9+ * @throws {TypeError } If `numbers` is not iterable, or if any value in the iterable is not of type `number`.
10+ * @throws {RangeError } If the iterable yields 2^53 or more values.
11+ * @see https://tc39.es/proposal-math-sum/
12+ */
13+ sumPrecise ( numbers : Iterable < number > ) : number ;
14+ }
Original file line number Diff line number Diff line change 8383 " es2025.collection" ,
8484 " es2025.float16" ,
8585 " es2025.intl" ,
86- " es2025.iterator" ,
86+ " es2025.iterator" ,
8787 " es2025.promise" ,
8888 " es2025.regexp" ,
89- " esnext.array" ,
89+ " esnext.array" ,
90+ " esnext.math" ,
9091 " esnext.collection" ,
9192 " esnext.date" ,
9293 " esnext.decorators" ,
Original file line number Diff line number Diff line change 1+ // @target : esnext
2+ // @lib : esnext
3+ // @noemit : true
4+ // @strict : true
5+
6+ // Basic usage with array
7+ const sum1 = Math . sumPrecise ( [ 1 , 2 , 3 ] ) ;
8+
9+ // Floating point precision
10+ const sum2 = Math . sumPrecise ( [ 1e20 , 0.1 , - 1e20 ] ) ;
11+
12+ // Empty iterable returns -0
13+ const sum3 = Math . sumPrecise ( [ ] ) ;
14+
15+ // Works with any Iterable<number>
16+ const sum4 = Math . sumPrecise ( new Set ( [ 1 , 2 , 3 ] ) ) ;
17+
18+ function * gen ( ) : Iterable < number > {
19+ yield 0.1 ;
20+ yield 0.2 ;
21+ }
22+ const sum5 = Math . sumPrecise ( gen ( ) ) ;
23+
24+ // Return type is number
25+ const result : number = Math . sumPrecise ( [ 1 , 2 , 3 ] ) ;
26+
27+ // @ts -expect-error - BigInt is not a number
28+ Math . sumPrecise ( [ 1n , 2n ] ) ;
29+
30+ // @ts -expect-error - string is not a number
31+ Math . sumPrecise ( [ "a" , "b" ] ) ;
You can’t perform that action at this time.
0 commit comments