-
Notifications
You must be signed in to change notification settings - Fork 57
How should rounding between Integer and Number be specified? #14
Comments
According to Waldemar Horwat,
TODO(littledan): Write this up in spec text. |
For reference, in Python,
Hence, Python rounds toward In Julia,
Julia requires you to be explicit in how you want to round. In terms of which approach is better, I see pros and cons for each. Julia has no default behavior, which means that no one has to guess what the default behavior is. However, this runs contrary to current precedent in JavaScript where the default behavior when converting a
Of course, having a default behavior is not mutually exclusive with explicitly specifying rounding. In this case, I side with being consistent with current For the reverse conversion, not sure how a rounding mode is applicable here. IEEE 754 rounding rules, unless I am mistaken, are applied when rounding an IEEE 754 float to an integer, not the other way around. |
For the Integer to Number rounding, there's actually already a section in the spec that defines this, in #sec-ecmascript-language-types-number-type . I'll reference that for this direction, as a start. For Number to Integer, the place where this comes up is in the Number constructor, which this specification makes into a cast operator. Unfortunately, this API doesn't give a clean place for a parameter to specify direction, but we could throw an exception on Numbers which are not already exactly expressible in integers, and expect people to call Math.ceil, Math.floor. What do you think? |
My stance is similar Waldemar Horwat: round toward zero (i.e., mirror Python and current JS behavior). |
I'll start the spec with deliberately vague text since I don't know the answer. Should it always round towards 0? If that were done, then are there cases where there would be issues with round-tripping? If there are such issues, do we care?
When rounding from a Number to a Integer, should lower, insignificant digits be "accurate" or round towards 0-digits, as we see coming up in the existing specification in tc39/ecma402#128 ?
The text was updated successfully, but these errors were encountered: