Skip to content

Commit 545f9ec

Browse files
committed
Handle infinity and NaN separately
1 parent 92bf655 commit 545f9ec

1 file changed

Lines changed: 17 additions & 11 deletions

File tree

spec.emu

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ location: https://github.com/tc39/proposal-amount/
3232
<emu-intro id="sec-amount-intro">
3333
<h1>Introduction</h1>
3434
<p>An Amount is an object that wraps a numeric value—as a Number, BigInt, or String—together with an optional unit (e.g., mile, kilogram, EUR, JPY, USD-per-mile). One can intuitively understand an Amount as a value that, so to speak, knows what it is measuring.</p>
35-
<p>When precision options (such as fractionDigits or significantDigits) are applied, or when unit conversion is performed, the numeric value is stored as a <dfn id="dfn-decimal-digit-string">decimal digit string</dfn>, which is a String in |StrDecimalLiteral| form or *"NaN"*. Otherwise, the original JavaScript value type (Number, BigInt, or String) is retained.</p>
35+
<p>When precision options (such as fractionDigits or significantDigits) are applied, or when unit conversion is performed, finite numeric values are stored as a <dfn id="dfn-decimal-digit-string">decimal digit string</dfn>: a String in |StrDecimalLiteral| form. Non-finite values are stored as the Number values *NaN*, *+&infin;*<sub>𝔽</sub>, or *-&infin;*<sub>𝔽</sub>. Otherwise, the original JavaScript value type (Number, BigInt, or String) is retained.</p>
3636
<p>Rounding a mathematical value is an important part of this spec. When we say <dfn id="dfn-amount-rounding-mode">rounding mode</dfn> in this specification we simply refer to <emu-xref href="#table-intl-rounding-modes">ECMA-402's definition</emu-xref>.</p>
3737
</emu-intro>
3838

@@ -215,14 +215,11 @@ location: https://github.com/tc39/proposal-amount/
215215
</h1>
216216
<dl class="header">
217217
<dt>description</dt>
218-
<dd>It returns _v_ rendered in canonical exponential notation: an optional minus sign, a mantissa with one non-zero integer digit (or *"0"*), an optional fractional part, *"e"*, an explicit sign, and an unpadded base-10 exponent. The number of significant digits in the result equals the number of significant digits in _v_. If _v_ is *"NaN"*, *"Infinity"*, or *"-Infinity"*, it is returned unchanged.</dd>
218+
<dd>It returns _v_ rendered in canonical exponential notation: an optional minus sign, a mantissa with one non-zero integer digit (or *"0"*), an optional fractional part, *"e"*, an explicit sign, and an unpadded base-10 exponent. The number of significant digits in the result equals the number of significant digits in _v_.</dd>
219219
</dl>
220220
<emu-alg>
221221
1. Let _intlMV_ be ! ToIntlMathematicalValue(_v_).
222222
1. Let _value_ be _intlMV_.[[Value]].
223-
1. If _value_ is ~not-a-number~, return *"NaN"*.
224-
1. If _value_ is ~positive-infinity~, return *"Infinity"*.
225-
1. If _value_ is ~negative-infinity~, return *"-Infinity"*.
226223
1. Let _digitCount_ be _intlMV_.[[StringDigitCount]].
227224
1. Let _sign_ be the empty String.
228225
1. If _value_ is ~negative-zero~, then
@@ -363,10 +360,16 @@ location: https://github.com/tc39/proposal-amount/
363360
1. Let _value_ be _x_.
364361
1. Else,
365362
1. Assert: _x_ is a String.
366-
1. Let _formatter_ be CreateFormatterObject(_roundingMode_, _fractionDigits_, _fractionDigits_, _significantDigits_, _significantDigits_, *undefined*).
367363
1. Let _intlMV_ be ! ToIntlMathematicalValue(_x_).
368-
1. Let _formatted_ be FormatNumericToString(_formatter_, _intlMV_.[[Value]], _intlMV_.[[StringDigitCount]]).
369-
1. Let _value_ be RenderDigitStringInExponentialNotation(_formatted_.[[FormattedString]]).
364+
1. Let _mv_ be _intlMV_.[[Value]].
365+
1. If _mv_ is ~positive-infinity~, then
366+
1. Let _value_ be *+&infin;*<sub>𝔽</sub>.
367+
1. Else if _mv_ is ~negative-infinity~, then
368+
1. Let _value_ be *-&infin;*<sub>𝔽</sub>.
369+
1. Else,
370+
1. Let _formatter_ be CreateFormatterObject(_roundingMode_, _fractionDigits_, _fractionDigits_, _significantDigits_, _significantDigits_, *undefined*).
371+
1. Let _formatted_ be FormatNumericToString(_formatter_, _mv_, _intlMV_.[[StringDigitCount]]).
372+
1. Let _value_ be RenderDigitStringInExponentialNotation(_formatted_.[[FormattedString]]).
370373
1. Let _O_ be OrdinaryObjectCreate(%Amount.prototype%, « [[AmountValue]], [[Unit]] »).
371374
1. Set _O_.[[AmountValue]] to _value_.
372375
1. Set _O_.[[Unit]] to _unit_.
@@ -470,10 +473,13 @@ location: https://github.com/tc39/proposal-amount/
470473
1. Assert: _v_ is a String.
471474
1. Let _sourceValue_ be StringToNumber(_v_).
472475
1. Let _convertedValue_ be ? ConvertUnitValue(_sourceValue_, _sourceUnit_, _targetUnit_).
473-
1. Let _formatter_ be CreateFormatterObject(_roundingMode_, _minFractionDigits_, _maxFractionDigits_, _minSignificantDigits_, _maxSignificantDigits_, _roundingPriority_).
474-
1. Let _formatted_ be FormatNumericToString(_formatter_, ℝ(_convertedValue_), 0).
475476
1. Let _result_ be OrdinaryObjectCreate(%Amount.prototype%, « [[AmountValue]], [[Unit]] »).
476-
1. Set _result_.[[AmountValue]] to RenderDigitStringInExponentialNotation(_formatted_.[[FormattedString]]).
477+
1. If _convertedValue_ is not finite, then
478+
1. Set _result_.[[AmountValue]] to _convertedValue_.
479+
1. Else,
480+
1. Let _formatter_ be CreateFormatterObject(_roundingMode_, _minFractionDigits_, _maxFractionDigits_, _minSignificantDigits_, _maxSignificantDigits_, _roundingPriority_).
481+
1. Let _formatted_ be FormatNumericToString(_formatter_, ℝ(_convertedValue_), 0).
482+
1. Set _result_.[[AmountValue]] to RenderDigitStringInExponentialNotation(_formatted_.[[FormattedString]]).
477483
1. Set _result_.[[Unit]] to _targetUnit_.
478484
1. Return _result_.
479485
</emu-alg>

0 commit comments

Comments
 (0)