Compile ints to big-ints on the Javascript target #5087
abs0luty
started this conversation in
Ideas & suggestions
Replies: 1 comment
-
|
This is by design. The limits of the number type is target dependant.
We did previously try this, but it performs too poorly to be a viable option. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Current design
On the BEAM target, Int values are arbitrary-precision integers (limited only by memory). Small integers are optimized; larger ones transparently become bignums. Float is the runtime's 64-bit IEEE-754 double, but BEAM has no NaN/Infinity values exposed in the same way as JavaScript.
On the JavaScript target, Float is a 64-bit IEEE-754 double, matching JS Number. Int operations obey the host's numeric semantics; keep integers within JS's safe integer range (±(2^53−1)) to avoid precision loss, or use library types/BigInt when you need larger exact integers.
Output when running the code using default erlang target (
gleam run):Output when running the code using javascript target (
gleam run --target javascript):As of recently, Gleam compiler started to output a warning when dealing with big numbers in JS target:
I think the current cross-target inconsistency is a real foot-gun and we should prefer strict, portable semantics.
Proposal: be strict and make
IntportableMake
Intcompile toBigInton the JS target (just like it's currently implemented in bigi), soIntmeans "mathematically exact integer" on both BEAM and JS. That gives us one mental model and identical results across backends.Alongside strict
Int, introduce a dedicated small-integer type that is guaranteed to fit in JS's safe range - call it something likeSmallInt. On JS it maps toNumber; on BEAM it maps toIntand enforces the same range at construction.This gives users an explicit, performance-oriented choice for counters, indices, interop with JS APIs, etc., while keeping default
Intexact and cross-target consistent. (On BEAM, small integers are already an optimized representation; this mirrors that idea semantically)Do you think it's worth it?
Beta Was this translation helpful? Give feedback.
All reactions