Skip to content

Transformation from float to bigint #7089

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
remitbri opened this issue Oct 7, 2024 · 4 comments · Fixed by #7419
Closed

Transformation from float to bigint #7089

remitbri opened this issue Oct 7, 2024 · 4 comments · Fixed by #7419
Milestone

Comments

@remitbri
Copy link

remitbri commented Oct 7, 2024

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
Copy link
Author

remitbri commented Oct 7, 2024

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

@shulhi shulhi added this to the v12 milestone Jan 30, 2025
@bloodyowl
Copy link
Collaborator

@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

@remitbri
Copy link
Author

remitbri commented May 3, 2025

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

bloodyowl added a commit that referenced this issue May 4, 2025
fixes #7089

⚠️ breaking change
bloodyowl added a commit that referenced this issue May 4, 2025
fixes #7089

⚠️ breaking change
@bloodyowl
Copy link
Collaborator

@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 added a commit that referenced this issue May 4, 2025
* make fromFloat error-safe

fixes #7089

⚠️ breaking change

* shadow rather than prefix with underscore
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants