Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 55 additions & 63 deletions spec.emu
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ location: https://github.com/tc39/proposal-amount/
<emu-clause id="sec-amount-roundtofractiondigits" type="abstract operation">
<h1>RoundAmountValueToFractionDigits(
_v_: a mathematical value,
_n_: a non-negative integer,
_n_: an integer,
optional _roundingMode_: a rounding mode
): a mathematical value
</h1>
Expand All @@ -262,32 +262,6 @@ location: https://github.com/tc39/proposal-amount/
</emu-alg>
</emu-clause>

<emu-clause id="sec-amount-roundtosignificantdigits" type="abstract operation">
<h1>RoundToSignificantDigits (
_v_: a mathematical value,
_n_: a non-negative integer,
optional _roundingMode_: a rounding mode
): a mathematical value
</h1>
<dl class="header">
<dt>description</dt>
<dd>It computes the closest approximation to a given mathematical value that has at most the given number of significant digits, rounding (if necessary) according to the given rounding mode.</dd>
</dl>
<emu-alg>
1. If _roundingMode_ is *undefined*, set _roundingMode_ to *"halfEven"*.
1. If _v_ = 0, return 0.
1. If v &lt; 0, then
1. Let _reverseRoundingMode_ be ReverseRoundingMode(_roundingMode_).
1. Let _d_ be RoundToSignificantDigits(–_v_, _n_, _reverseRoundingMode_).
1. Return –_d_.
1. Let _e_ be the unique integer such that 10<sup>_e_</sup> ≤ _v_ < 10<sup>_e_+1</sup>.
1. Let _pow_ be _e_ - _n_.
1. Let _m_ be _v_ × 10<sup>–_pow_</sup>.
1. Let _rounded_ be ApplyRoundingModeToPositive(_m_, _roundingMode_).
1. Return _rounded_ × 10<sup>_n_ + _e_</sup>.
</emu-alg>
</emu-clause>

<emu-clause id="sec-amount-rendermvwithfractiondigits" type="abstract operation">
<h1>RenderAmountValueWithFractionDigits (
_v_: an Intl mathematical value,
Expand Down Expand Up @@ -340,6 +314,45 @@ location: https://github.com/tc39/proposal-amount/
</emu-alg>
</emu-clause>

<emu-clause id="sec-amount-FractionToSignificantDigits" type="abstract operation">
<h1>FractionToSignificantDigits (
_value_: an Intl mathematical value,
_fractionDigits_: an integer
): a non-negative integer
</h1>
<dl class="header"></dl>
<emu-alg>
1. If _value_ is one of ~not-a-number~, ~positive-infinity~, or ~negative-infinity~, then
1. Return 0.
1. If _value_ is one of 0 or ~negative-zero~, then
1. If _fractionDigits_ < 0, return 1.
1. Let _integerDigits_ be 1.
1. Else,
1. Let _integerDigits_ be the smallest integer such that 10<sup>_integerDigits_</sup> > abs(_value_).
1. Assert: _integerDigits_ + _fractionDigits_ > 0.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This assert can fail for zeroes. That's one of the reasons why I don't think we should compute significantDigits from fractionDigits.

FractionToSignificantDigits is not used anywhere. Just delete it.

1. Return _integerDigits_ + _fractionDigits_.
</emu-alg>
</emu-clause>

<emu-clause id="sec-amount-SignificantToFractionDigits" type="abstract operation">
<h1>SignificantToFractionDigits (
_value_: an Intl mathematical value,
_significantDigits_: a positive integer
): an integer
</h1>
<dl class="header"></dl>
<emu-alg>
1. If _value_ is one of ~not-a-number~, ~positive-infinity~, or ~negative-infinity~, then
1. Return 0.
1. If _value_ is one of 0 or ~negative-zero~, then
1. Let _integerDigits_ be 1.
1. Else,
1. Let _integerDigits_ be the smallest integer such that 10<sup>_integerDigits_</sup> > abs(_value_).
1. Assert: _significantDigits_ > 0.
1. Return _significantDigits_ - _integerDigits_.
</emu-alg>
</emu-clause>

<emu-clause id="sec-amount-getamountoptions" type="abstract operation">
<h1>GetAmountOptions (
_opts_: an Object
Expand All @@ -357,7 +370,7 @@ location: https://github.com/tc39/proposal-amount/
1. Let _unit_ be ? GetOption(_opts_, *"unit"*, ~string~, ~empty~, *undefined*).
1. If _fractionDigits_ is not *undefined*, then
1. If _significantDigits_ is not *undefined*, throw a *RangeError* exception.
1. If _fractionDigits_ is not a non-negative integral number, throw a *RangeError* exception.
1. If _fractionDigits_ is not an integral number, throw a *RangeError* exception.
1. Else if _significantDigits_ is not *undefined*, then
1. If _significantDigits_ is not an integral number, throw a *RangeError* exception.
1. If ℝ(_significantDigits_) < 1, throw a *RangeError* exception.
Expand Down Expand Up @@ -389,47 +402,27 @@ location: https://github.com/tc39/proposal-amount/
1. Let _parsed_ be ParseText(_toParse_, |StringNumericLiteral|).
1. If _parsed_ is a List of errors, then
1. Let _amountValue_ be ~not-a-number~.
1. Let _numDigits_ be 0.
1. Else,
1. Let _intlMV_ be the StringIntlMV of _parsed_.
1. Let _amountValue_ be _intlMV_[0].
1. Let _numDigits_ be _intlMV_[1].
1. Let _validatedOpts_ be ? GetAmountOptions(_opts_).
1. Let _fractionDigits_ be _validatedOpts_.[[FractionDigits]].
1. Let _roundingMode_ be _validatedOpts_.[[RoundingMode]].
1. Let _fractionDigits_ be _validatedOpts_.[[FractionDigits]].
1. Let _significantDigits_ be _validatedOpts_.[[SignificantDigits]].
1. Let _unit_ be _validatedOpts_.[[Unit]].
1. Let _O_ be OrdinaryObjectCreate(%Amount.prototype%, « [[FractionDigits]], [[SignificantDigits]], [[Unit]], [[Value]] »).
1. If _amountValue_ is a mathematical value, then
1. Let _roundedValue_ be _amountValue_.
1. If both _significantDigits_ and _fractionDigits_ are *undefined*, then
1. Set _significantDigits_ to _numDigits_.
1. Let _O_ be OrdinaryObjectCreate(%Amount.prototype%, « [[FractionDigits]], [[Unit]], [[Value]] »).
1. If _fractionDigits_ is *undefined*, then
1. If _significantDigits_ is not *undefined*, then
1. Set _fractionDigits_ to SignificantToFractionDigits(_amountValue_, _significantDigits_).
1. Else if _amountValue_ is a mathematical value or ~negative-zero~, then
1. Set _fractionDigits_ to the CountFractionDigits of _toParse_.
1. Else if _significantDigits_ is *undefined*, then
1. Set _roundedValue_ be RoundAmountValueToFractionDigits(_amountValue_, _fractionDigits_, _roundingMode_).
1. Let _e_ be the smallest non-negative integer such that _roundedValue_ × 10<sup>-_e_</sup> is an integer.
1. Let _scaledRoundedValue_ be _roundedValue_ × 10<sup>-_e_</sup>.
1. If _scaledRoundedValue_ = 0, then
1. Set _significantDigits_ to 1.
1. Else,
1. Let _l_ be the log-10 of abs(_scaledRoundedValue_).
1. Set _significantDigits_ to floor(_l_) + 1.
1. Otherwise:
1. Set _roundedValue_ be RoundToSignificantDigits(_amountValue_, _significantDigits_, _roundingMode_).
1. Let _digitStr_ be the unique decimal string representation of _roundedValue_ without duplicate leading zeroes.
1. Set _fractionDigits_ to the CountFractionDigits of _digitStr_.
1. Set _O_.[[Value]] to _roundedValue_.
1. Set _O_.[[SignificantDigits]] to _significantDigits_.
1. Set _O_.[[FractionDigits]] to _fractionDigits_.
1. Else if _amountValue_ is ~minus-zero~, then
1. Set _O_.[[Value]] to ~minus-zero~.
1. Set _O_.[[SignificantDigits]] to _numDigits_.
1. Assert: _numDigits_ ≥ 1.
1. Set _O_.[[FractionDigits]] to _numDigits_ - 1.
1. Otherwise:
1. Else,
1. Set _fractionDigits_ to 0.
1. Set _O_.[[FractionDigits]] to _fractionDigits_.
1. If _amountValue_ is a mathematical value, then
1. Set _O_.[[Value]] to RoundAmountValueToFractionDigits(_amountValue_, _fractionDigits_, _roundingMode_).
1. Else,
1. Set _O_.[[Value]] to _amountValue_.
1. Set _O_.[[SignificantDigits]] to _numDigits_..
1. Set _O_.[[FractionDigits]] to 0.
1. If _unit_ is not *undefined* and _amountValue_ is not ~not-a-number~, set _O_.[[Unit]] to _unit_.
1. Return _O_.
</emu-alg>
Expand Down Expand Up @@ -486,15 +479,14 @@ location: https://github.com/tc39/proposal-amount/
1. Let _fractionDigits_ be _processedOptions_.[[FractionDigits]].
1. Let _significantDigits_ be _processedOptions_.[[SignificantDigits]].
1. Let _value_ be _O_.[[Value]].
1. If _significantDigits_ is not *undefined*, set _fractionDigits_ to SignificantToFractionDigits(_value_, _significantDigits_).
1. If _fractionDigits_ is not *undefined*, set _value_ to RoundAmountValueToFractionDigits(_value_, _fractionDigits_).
1. Else if _significantDigits_ is not *undefined*, set _value_ to RoundToSignificantDigits(_value_, _significantDigits_).
1. Let _N_ be OrdinaryObjectCreate(*"%Amount.prototype%"*, « [[FractionDigits]], [[SignificantDigits]], [[Unit]], [[Value]] »).
1. Let _N_ be OrdinaryObjectCreate(*"%Amount.prototype%"*, « [[FractionDigits]], [[Unit]], [[Value]] »).
1. If _unit_ is not *undefined* and _O_.[[Unit]] is not *undefined*, then
1. If SameValueNonNumber(_unit_, _O_.[[Unit]]) is *false*, throw a *TypeError* exception.
1. Set _N_.[[Value]] to _value_.
1. Set _N_.[[Unit]] to _unit_.
1. Set _N_.[[FractionDigits]] to ℝ(_fractionDigits_).
1. Set _N_.[[SignificantDigits]] to ℝ(_significantDigits_).
1. Return _N_.
</emu-alg>
</emu-clause>
Expand Down