Skip to content

Commit

Permalink
Editorial: Address too-large Duration values resulting from FP rounding
Browse files Browse the repository at this point in the history
There was an edge case, spotted by Anba, where floating-point rounding at
the point of conversion from normalized form to Duration storage form, in
BalanceTimeDuration, can result in a Duration storage form that fails
assertions.

For example, when a normalized time duration is the maximum possible value
{ [[TotalNanoseconds]]: 9007199254740991999999999 }, and
BalanceTimeDuration is called with largestUnit nanoseconds.

This is already covered by test262 tests.

Closes: #2785
  • Loading branch information
ptomato committed Mar 12, 2024
1 parent d20ddf8 commit 812fe52
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion spec/duration.html
Original file line number Diff line number Diff line change
Expand Up @@ -1197,7 +1197,7 @@ <h1>
1. If abs(_years_) &ge; 2<sup>32</sup>, return *false*.
1. If abs(_months_) &ge; 2<sup>32</sup>, return *false*.
1. If abs(_weeks_) &ge; 2<sup>32</sup>, return *false*.
1. Let _normalizedSeconds_ be _days_ &times; 86,400 + _hours_ &times; 3600 + _minutes_ &times; 60 + _seconds_ + _milliseconds_ &times; 10<sup>-3</sup> + _microseconds_ &times; 10<sup>-6</sup> + _nanoseconds_ &times; 10<sup>-9</sup>.
1. Let _normalizedSeconds_ be _days_ &times; 86,400 + _hours_ &times; 3600 + _minutes_ &times; 60 + _seconds_ + ℝ(𝔽(_milliseconds_)) &times; 10<sup>-3</sup> + ℝ(𝔽(_microseconds_)) &times; 10<sup>-6</sup> + ℝ(𝔽(_nanoseconds_)) &times; 10<sup>-9</sup>.
1. NOTE: The above step cannot be implemented directly using floating-point arithmetic. Multiplying by 10<sup>-3</sup>, 10<sup>-6</sup>, and 10<sup>-9</sup> respectively may be imprecise when _milliseconds_, _microseconds_, or _nanoseconds_ is an unsafe integer. This multiplication can be implemented in C++ with an implementation of `std::remquo()` with sufficient bits in the quotient. String manipulation will also give an exact result, since the multiplication is by a power of 10.
1. If abs(_normalizedSeconds_) &ge; 2<sup>53</sup>, return *false*.
1. Return *true*.
Expand Down

0 comments on commit 812fe52

Please sign in to comment.