The problem: Math.sign
, Math.abs
, Math.pow
, Math.min
, and Math.max
currently only accept Numbers; there is no similar functionality for BigInts.
See the main proposal repository.
Yaffle has also made a forum poll about some of these questions.
The first option: Math.sign
, Math.abs
, and Math.pow
are extended to also accept BigInts.
The second option: create new BigInt.sign
, BigInt.abs
, and BigInt.pow
methods. (Math.sign
, Math.abs
, and Math.pow
would continue to accept only Numbers).
Option | Memorization |
---|---|
Extend Math.sign etc. |
The developer has to memorize which Math methods also accept BigInts. |
Create BigInt.sign etc. |
The developer has to memorize what methods does the BigInt object have. |
We can already compare Numbers and BigInts with <
, <=
, and ==
. We should be able to do the same with min
and max
. For example, Math.min(0, 1n)
is uncontroversially 0
, and Math.max(0, 1n)
is uncontroversially 1n
.
However, what should Math.min(0, 0n)
and Math.max(0, 0n)
return—and why?
Option | Notes |
---|---|
min prefers the first value and max prefers the last value |
This would match the result of [ 0, 0n ].sort((a, b) => a < b ? -1 : a == b ? 0 : +1) . |
Always prefer the first value | This would match the result of [ 0, 0n ].reduce((a, b) => a < b ? a : b) . |
Always prefer the last value | This would match the result of [ 0, 0n ].reduce((a, b) => a <= b ? a : b) . |
Always prefer BigInt | This would create a standard “numeric type tower”: an intrinsic ordering in the language. Most languages with numeric type towers would use this tower ordering when sort ing their numbers, but JavaScript’s sort does not do this.) |
It would be a requirement that these functions would return BigInts.
Right now, we don’t see any use cases for these. But several people responded to Yaffle’s forum poll affirming that they would use BigInt sqrt
, cbrt
, hypot
, and log10
.
This is related to the previous question.