-
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. |
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
ae29ff4 to
0b1f690
Compare
- Empty arrays should always return -0 per spec - Separate logic for empty vs all-zeros cases - Matches behavior of Hermes implementation
This implements Math.sumPrecise from the TC39 proposal-math-sum. The method provides precise summation of floating-point numbers, avoiding the precision loss that occurs with naive summation.
Why implement this now?
While working on floating-point arithmetic improvements after the Float16Array implementation, I identified that precise summation is a fundamental operation that developers need. The Math.sumPrecise method addresses real precision problems that occur when summing floating-point numbers, particularly arrays with values of vastly different magnitudes. This implementation adds immediate value to Rhino users who need accurate numerical computations, complementing the recent work on improved floating-point handling.
Implementation approach
The implementation uses Shewchuk's Simple Two-Sum algorithm to maintain precision by tracking error components during addition. This approach was chosen after benchmarking showed it provides 2-6x better performance than the more complex Grow-Expansion algorithm while maintaining the same precision guarantees.
Key implementation details include support for both array-like objects and iterables through the Symbol.iterator protocol, correct handling of special IEEE 754 values including infinities, NaN, and signed zeros according to the specification, and full compatibility with the test262 test suite.
The algorithm handles challenging cases like summing arrays with values of very different magnitudes. For example, with [1e20, 0.1, -1e20], naive summation returns 0, while Math.sumPrecise correctly returns 0.1. The implementation maintains precision even with complex cancellation patterns and extreme value ranges.