Skip to content

Conversation

@anivar
Copy link
Contributor

@anivar anivar commented Sep 13, 2025

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:

  • Values with very different magnitudes: [1e20, 0.1, -1e20] returns 0.1 (regular sum gives 0)
  • Special IEEE 754 values: infinities, NaN, signed zeros all per spec
  • Large arrays: enforces 2^53 element limit per spec
  • Type safety: validates all elements are Numbers

Current status

Phase 1 complete:

  • Array-like objects: Math.sumPrecise([1.1, 2.2, 3.3])
  • Length validation with ScriptRuntime.toLength()
  • Full ES2026 spec compliance for implemented path
  • All test262 tests passing for array-like

Phase 2 deferred:

The core Shewchuk algorithm implementation is complete and ready - just needs the iterator wrapper once PR #2078 merges.

@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.

@anivar anivar force-pushed the es2026-math-sumprecise branch from ae29ff4 to 0b1f690 Compare September 18, 2025 18:51
@anivar anivar force-pushed the es2026-math-sumprecise branch from ce98a1f to bf59197 Compare November 29, 2025 17:52
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
- 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
@anivar anivar force-pushed the es2026-math-sumprecise branch from 0f2b8d9 to 3b4f34b Compare November 29, 2025 22:58
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