Open James103 opened 1 year ago
Woah, I haven't seen this '2n' thing before. Is it a new Javascript feature?
Woah, I haven't seen this '2n' thing before. Is it a new Javascript feature?
The syntax used describes something called BigInt
s, which are supported on the most recent version of all major browsers. BigInt
s effectively allow storing numbers much larger than 21024 with full precision (down to the nearest whole number), with their size effectively only being limited only by available memory.
For more information on BigInts, see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt
Cutting edge! I'll take a look at supporting BigInts if I find the time and mood.
You can convert a BigInt to a String with String(bigint)
or bigint.toString()
and you can construct a Decimal from a string of digits with new Decimal(string)
. However, Decimal.fromString
fails to properly parse strings of digits that are more than 309 digits long, as such strings become Infinity
when coerced to Number
.
Number("1".repeat(309))
1.1111111111111112e+308
Number("1".repeat(310))
Infinity
I guess it'd be sufficient to detect the string is longer than 300 digits, grab the first 18 of them, and turn it into mantissa and exponent.
I guess it'd be sufficient to detect the string is longer than 300 digits, grab the first 18 of them, and turn it into mantissa and exponent.
Couldn't you toString()
the bigint, remove the n
from the string then let toString()
method do it's job as usual?
I guess it'd be sufficient to detect the string is longer than 300 digits, grab the first 18 of them, and turn it into mantissa and exponent.
Couldn't you
toString()
the bigint, remove then
from the string then lettoString()
method do it's job as usual?
but that's what #145 does.
Currently (latest
break_eternity.js
version as of 2023-01-07), attempting to convert a BigInt value to Decimal will return 0. Furthermore, a string that represents a number (generated byBigInt.toString()
) will also return 0 if the number the string represents is at least 21024.Console output with test cases:
Discovered whilst writing migration code for an incremental game to convert various save data from BigInt values to their proper Decimals.