You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Editorial: Use the rounding abstract operations from Intl.NumberFormat V3
We intend to standardize on the same rounding modes as Intl.NumberFormat
(see #1038), so it makes sense to use the same abstract operations, namely
GetUnsignedRoundingMode and ApplyUnsignedRoundingMode.
Note that this does not yet add user-visible support for the full set of
rounding modes that Intl.NumberFormat does; that (#1038) is a normative
change that we'll apply once Intl.NumberFormat goes to Stage 4.
However, this way of expressing rounding makes it easier to make that
change in the future, and makes it easier to address #2191 in the short
term.
RoundTowardsZero stays the same, since that is used for several other
things as well as rounding according to a rounding mode.
): ~zero~, ~infinity~, ~half-zero~, ~half-infinity~, or ~half-even~
660
+
</h1>
661
+
<dlclass="header">
662
+
<dt>description</dt>
663
+
<dd>
664
+
The return value is the rounding mode that should be applied to the absolute value of a number to produce the same result as if _roundingMode_ were applied to the signed value of the number (negative if _isNegative_ is *true*, or positive otherwise).
665
+
</dd>
666
+
</dl>
656
667
<emu-alg>
657
-
1.Assert:_x_ is a mathematical value.
658
-
1. Return the mathematical value that is closest to _x_ and is an integer.
659
-
If two integers are equally close to _x_, then the result is the integer that is farther away from 0. If _x_ is already an integer, then the result is _x_.
668
+
1. If _isNegative_ is *true*, return the specification type in the third column of <emu-xrefhref="#table-temporal-unsigned-rounding-modes"></emu-xref> where the first column is _roundingMode_ and the second column is "negative".
669
+
1. Else, return the specification type in the third column of <emu-xrefhref="#table-temporal-unsigned-rounding-modes"></emu-xref> where the first column is _roundingMode_ and the second column is "positive".
660
670
</emu-alg>
671
+
<emu-notetype="editor">
672
+
<p>This operation is intended to be the same one as in the <ahref="https://tc39.es/proposal-intl-numberformat-v3/out/numberformat/diff.html#sec-getunsignedroundingmode">Intl.NumberFormat v3</a> proposal.</p>
_unsignedRoundingMode_: ~zero~, ~infinity~, ~half-zero~, ~half-infinity~, ~half-even~, or *undefined*,
776
+
): a mathematical value
777
+
</h1>
778
+
<dlclass="header">
779
+
<dt>description</dt>
780
+
<dd>
781
+
It considers _x_, bracketed below by _r1_ and above by _r2_, and returns either _r1_ or _r2_ according to _unsignedRoundingMode_.
782
+
</dd>
783
+
</dl>
784
+
<emu-alg>
785
+
1. If _x_ is equal to _r1_, return _r1_.
786
+
1.Assert:_r1_<_x_<_r2_.
787
+
1.Assert:_unsignedRoundingMode_ is not *undefined*.
788
+
1. If _unsignedRoundingMode_ is ~zero~, return _r1_.
789
+
1. If _unsignedRoundingMode_ is ~infinity~, return _r2_.
790
+
1. Let _d1_ be <emu-eqn>_x_ – _r1_</emu-eqn>.
791
+
1. Let _d2_ be <emu-eqn>_r2_ – _x_</emu-eqn>.
792
+
1. If _d1_<_d2_, return _r1_.
793
+
1. If _d2_<_d1_, return _r2_.
794
+
1.Assert:_d1_ is equal to _d2_.
795
+
1. If _unsignedRoundingMode_ is ~half-zero~, return _r1_.
796
+
1. If _unsignedRoundingMode_ is ~half-infinity~, return _r2_.
797
+
1.Assert:_unsignedRoundingMode_ is ~half-even~.
798
+
1. Let _cardinality_ be <emu-eqn>(_r1_ / (_r2_ – _r1_)) modulo 2</emu-eqn>.
799
+
1. If _cardinality_ is 0, return _r1_.
800
+
1. Return _r2_.
801
+
</emu-alg>
669
802
<emu-notetype="editor">
670
-
<p>
671
-
The rounding modes accepted by this abstract operation are intended to be the same as whatever is eventually standardized in the <ahref="https://github.com/tc39/proposal-intl-numberformat-v3">Intl.NumberFormat V3</a> proposal.
672
-
</p>
803
+
<p>This operation is intended to be the same one as in the <ahref="https://tc39.es/proposal-intl-numberformat-v3/out/numberformat/diff.html#sec-applyunsignedroundingmode">Intl.NumberFormat v3</a> proposal.</p>
_roundingMode_: *"ceil"*, *"floor"*, *"trunc"*, or *"halfExpand"*,
813
+
): an integer
814
+
</h1>
815
+
<dlclass="header">
816
+
<dt>description</dt>
817
+
<dd>It rounds _x_ to the nearest multiple of _increment_, up or down according to _roundingMode_.</dd>
818
+
</dl>
674
819
<emu-alg>
675
-
1.Assert:_x_ and _increment_ are mathematical values.
676
-
1.Assert:_roundingMode_ is *"ceil"*, *"floor"*, *"trunc"*, or *"halfExpand"*.
677
820
1. Let _quotient_ be _x_ / _increment_.
678
-
1. If _roundingMode_ is *"ceil"*, then
679
-
1. Let _rounded_ be -floor(-_quotient_).
680
-
1. Else if _roundingMode_ is *"floor"*, then
681
-
1. Let _rounded_ be floor(_quotient_).
682
-
1. Else if _roundingMode_ is *"trunc"*, then
683
-
1. Let _rounded_ be RoundTowardsZero(_quotient_).
821
+
1. If _quotient_< 0, then
822
+
1. Let _isNegative_ be *true*.
823
+
1. Set _quotient_ to -_quotient_.
684
824
1. Else,
685
-
1. Let _rounded_ be ! RoundHalfAwayFromZero(_quotient_).
825
+
1. Let _isNegative_ be *false*.
826
+
1. Let _unsignedRoundingMode_ be GetUnsignedRoundingMode(_roundingMode_, _isNegative_).
827
+
1. Let _r1_ be the largest integer such that _r1_ ≤ _quotient_.
828
+
1. Let _r2_ be the smallest integer such that _r2_>_quotient_.
829
+
1. Let _rounded_ be ApplyUnsignedRoundingMode(_quotient_, _r1_, _r2_, _unsignedRoundingMode_).
830
+
1. If _isNegative_ is *true*, set _rounded_ to -_rounded_.
686
831
1. Return _rounded_×_increment_.
687
832
</emu-alg>
833
+
<emu-notetype="editor">
834
+
<p>
835
+
The rounding modes accepted by this abstract operation are intended to be the same as whatever is eventually standardized in the <ahref="https://github.com/tc39/proposal-intl-numberformat-v3">Intl.NumberFormat V3</a> proposal.
0 commit comments