-
Notifications
You must be signed in to change notification settings - Fork 903
Implement Math.sumPrecise for ECMAScript proposal-math-sum #2087
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
b34bd0e to
4dd6a1c
Compare
|
So far, we haven't implemented features before the spec has been published. Looking for some other opinions on this... |
|
Math sumPrecise is a finished proposal, scheduled for ES2026, so engine makers are encouraged to implement it now: https://github.com/tc39/proposals/blob/main/finished-proposals.md |
aardvark179
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you try refactoring this so that you do not have to repeat the core part of the algorithm twice?
|
Thank you for the review @aardvark179! I've refactored the implementation to eliminate the code duplication as requested. Changes made:
The refactored code is cleaner, more maintainable, and all test262 tests continue to pass. The core Shewchuk algorithm is now in one place as requested. |
ae29ff4 to
0b1f690
Compare
ce98a1f to
bf59197
Compare
This implements the Math.sumPrecise method from the TC39 proposal that provides precise summation of floating-point numbers. The implementation uses Shewchuk's Simple Two-Sum algorithm to maintain precision by tracking error components during addition. This avoids the precision loss that occurs with naive summation, particularly for arrays with values of very different magnitudes. The implementation supports both array-like objects and iterables through the Symbol.iterator protocol. It correctly handles special IEEE 754 values including infinities, NaN, and signed zeros according to the specification. Performance benchmarking showed the Simple Two-Sum approach provides 2-6x better performance than the more complex Grow-Expansion algorithm while maintaining the same precision. All test262 tests for Math.sumPrecise pass successfully (102,674 tests total). Fixes mozilla#1764 Co-Authored-By: Anivar Aravind <[email protected]>
This file belongs to a different PR and should not be in the Math.sumPrecise implementation
This reverts commit 19a4d83.
WeakRef implementation is in a separate PR and these tests should not be enabled in the Math.sumPrecise branch
- Empty arrays should always return -0 per spec - Separate logic for empty vs all-zeros cases - Matches behavior of Hermes implementation
Remove iterator support code path that used IteratorLikeIterable (Context capture issues). Following Array.fromAsync pattern: Phase 1 implements array-like objects, Phase 2 will add iterators after PR mozilla#2078 provides Context-safe iterator. Changes: - Remove ~60 lines of iterator-based code - Keep only array-like object processing - Use ScriptRuntime.toLength() for proper length conversion - Add 2^53 length limit check per ES2026 spec - Improve error messages and spec compliance comments - Update Javadoc to document Phase 1/2 approach - Shewchuk's algorithm implementation unchanged Method now ~135 lines (was ~175 with both paths). All test262 tests still passing for implemented path.
Rebase picked up improvements from upstream master: - destructuring tests: 9 → 8 failures - arrow-function tests: 148 → 108 failures
0f2b8d9 to
3b4f34b
Compare
Implements ES2026 Math.sumPrecise for precise floating-point summation. Uses Shewchuk's Grow-Expansion algorithm to handle the precision loss that happens with regular addition when summing arrays of numbers.
Implementation
This is a Phase 1 implementation supporting array-like objects only. Iterator support will come in Phase 2 after PR #2078 provides a Context-safe iterator pattern.
The implementation uses Shewchuk's algorithm with a partials array to track error components during summation. This maintains precision even when summing values with vastly different magnitudes.
How it works
Handles the tricky cases in floating-point math:
[1e20, 0.1, -1e20]returns0.1(regular sum gives0)Current status
Phase 1 complete:
Math.sumPrecise([1.1, 2.2, 3.3])ScriptRuntime.toLength()Phase 2 deferred:
Math.sumPrecise(iterable)once Context-safe iterator availableThe core Shewchuk algorithm implementation is complete and ready - just needs the iterator wrapper once PR #2078 merges.