jrsonnet icon indicating copy to clipboard operation
jrsonnet copied to clipboard

More math operations for bigint

Open vklachkov opened this issue 4 months ago • 2 comments

Today I was surprised to learn that in jrsonnet you cannot divide bigints, you cannot multiply them with regular numbers, and changing the order of the operands can break everything. This PR brings order to the basic operations (except for bitwise operations) and makes the following changes:

  1. BigInt can now be compared with regular numbers.
  2. BigInt can now be added to regular numbers.
  3. BigInt can now be subtracted from regular numbers.
  4. BigInt can now be divided, and got the remainder.
  5. Strings can now be multiplied by BigInt. Also, the bug with number * str (instead of str * number) has been fixed.

All operations are now in separate functions. The changes were tested using the tests.zip scripts.

Ideally, we should also have binary operations for bigint and strict comparison between regular numbers and bigint. But this is not a big problem right now, and I will leave it for other PRs.

vklachkov avatar Oct 18 '25 04:10 vklachkov

r? @CertainLach

vklachkov avatar Oct 18 '25 04:10 vklachkov

BigInt can now be compared with regular numbers. BigInt can now be added to regular numbers. BigInt can now be subtracted from regular numbers.

This was done on purpose, such conversions should be explicit (via std.bigint), otherwise it may lead to silent precision loss. It is implemented the same way in JS (implicit conversions are disallowed), for the exception of bigint literal support (12n === BigInt(12))

Strings can now be multiplied by BigInt

String by number multiplication does not exist in upstream jsonnet, and I would prefer to remove it completely, instead of adding another out-of-spec feature

CertainLach avatar Oct 18 '25 05:10 CertainLach