Skip to content

Conversation

@anivar
Copy link
Contributor

@anivar anivar commented Sep 13, 2025

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.

@anivar anivar force-pushed the es2026-math-sumprecise branch from b34bd0e to 4dd6a1c Compare September 13, 2025 18:26
@gbrail
Copy link
Collaborator

gbrail commented Sep 16, 2025

So far, we haven't implemented features before the spec has been published. Looking for some other opinions on this...

@p-bakker
Copy link
Collaborator

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

Copy link
Contributor

@aardvark179 aardvark179 left a 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?

@anivar
Copy link
Contributor Author

anivar commented Sep 18, 2025

Thank you for the review @aardvark179! I've refactored the implementation to eliminate the code duplication as requested.

Changes made:

  1. Unified iterator approach: Both array-like objects and iterables now use the same algorithm through a shared helper method, eliminating the duplicate loops.

  2. Single algorithm path: The refactoring ensures all input types (arrays, array-likes, and iterables) flow through a single iterator-based path, removing the duplication you identified.

  3. Enhanced algorithm: While addressing the duplication, I also improved:

    • Overflow handling with biased partial tracking (handles extreme values better)
    • Special value handling for -0, infinities, and NaN
    • Edge case handling for MAX_DOUBLE boundaries
    • Extracted TwoSum algorithm into a cleaner inner class

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
WeakRef implementation is in a separate PR and these tests should not be enabled in the Math.sumPrecise branch
@anivar anivar force-pushed the es2026-math-sumprecise branch from ae29ff4 to 0b1f690 Compare September 18, 2025 18:51
- Empty arrays should always return -0 per spec
- Separate logic for empty vs all-zeros cases
- Matches behavior of Hermes implementation
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants