rescript-compiler icon indicating copy to clipboard operation
rescript-compiler copied to clipboard

Transformation from float to bigint

Open remitbri opened this issue 1 year ago • 1 comments

At the moment

let i = 1
let f = 1.3

let bigI = BigInt.fromInt(i)
let bigF = BigInt.fromFloat(f)

is compiled to

var bigI = BigInt(1);

var bigF = BigInt(1.3);

var i = 1;

var f = 1.3;

Problem: in JS, BigInt(x) is valid only if x is an integer, not a float, so running var bigF = BigInt(1.3) throws an error

remitbri avatar Oct 07 '24 14:10 remitbri

Potential fix: redefining in Core__BigInt.res to

let fromFloat = t => t->Core__Int.fromFloat->fromInt

with some documentation in the interface file, maybe adapting what's already available for Core__Int.fromFloat

remitbri avatar Oct 07 '24 14:10 remitbri

@remitbri issue there is that it accepts values above the maximum safe int32 provided they don't have any decimals, not much we can do on the type level

bloodyowl avatar May 03 '25 11:05 bloodyowl

Er, not sure I'm following you about the type level. This is a compiling-to-js issue

remitbri avatar May 03 '25 13:05 remitbri

@remitbri BigInt(Number.MAX_VALUE) is valid even though MAX_VALUE is a float. the issue is passing a float with decimals. I propose changing the signature to #7419 to make the function return an option rather than throwing.

@cometkim does that sound good to you?

bloodyowl avatar May 04 '25 15:05 bloodyowl