Skip to content

Instantly share code, notes, and snippets.

@js-choi
Last active September 23, 2021 15:09
Show Gist options
  • Save js-choi/f0f9a1548c791d1390b7be64f328068e to your computer and use it in GitHub Desktop.
Save js-choi/f0f9a1548c791d1390b7be64f328068e to your computer and use it in GitHub Desktop.
BigInt Math research (2021)

BigInt Math research (2021)

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.

Should BigInt sign/abs/pow live in the Math object or in the BigInt object?

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.

What should Math.min and max do with equivalent but differently typed values?

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 sorting their numbers, but JavaScript’s sort does not do this.)

Are there use cases for BigInt sqrt, cbrt, hypot, and log10?

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.

What do developers use BigInts for? What kind of mathematics do they need to perform on them?

This is related to the previous question.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment