Skip to content

make BigInt.fromFloat error-safe #7419

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

Merged
merged 2 commits into from
May 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#### :boom: Breaking Change

- Rename `JsError` to `JsExn` and error modules cleanup. https://github.com/rescript-lang/rescript/pull/7408
- Make `BigInt.fromFloat` return an option rather than throwing an error in case it's passed a value with a decimal value. https://github.com/rescript-lang/rescript/pull/7419

#### :rocket: New Feature

Expand Down
9 changes: 9 additions & 0 deletions lib/es6/Stdlib_BigInt.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@



function fromFloat(value) {
try {
return BigInt(value);
} catch (exn) {
return;
}
}

function toInt(t) {
return Number(t) | 0;
}
Expand All @@ -10,6 +18,7 @@ function bitwiseNot(x) {
}

export {
fromFloat,
toInt,
bitwiseNot,
}
Expand Down
9 changes: 9 additions & 0 deletions lib/js/Stdlib_BigInt.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
'use strict';


function fromFloat(value) {
try {
return BigInt(value);
} catch (exn) {
return;
}
}

function toInt(t) {
return Number(t) | 0;
}
Expand All @@ -9,6 +17,7 @@ function bitwiseNot(x) {
return x ^ -1n;
}

exports.fromFloat = fromFloat;
exports.toInt = toInt;
exports.bitwiseNot = bitwiseNot;
/* No side effect */
6 changes: 6 additions & 0 deletions runtime/Stdlib_BigInt.res
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ external fromStringExn: string => bigint = "BigInt"
@val external fromInt: int => bigint = "BigInt"
@val external fromFloat: float => bigint = "BigInt"

let fromFloat = (value: float) => {
try Some(fromFloat(value)) catch {
| _ => None
}
}

@send
/**
Formats a `bigint` as a string. Return a `string` representing the given value.
Expand Down
2 changes: 1 addition & 1 deletion tests/tests/src/core/Core_TempTests.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ console.info("BigInt");

console.info("---");

console.log(Primitive_bigint.div(BigInt(1), BigInt(12.0)));
console.log(Primitive_bigint.div(BigInt(1), Stdlib_Option.getOr(Stdlib_BigInt.fromFloat(12.0), 0n)));

console.info("");

Expand Down
2 changes: 1 addition & 1 deletion tests/tests/src/core/Core_TempTests.res
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ Console.info("---")
@warning("-44")
Console.log({
open BigInt
fromInt(1) / fromFloat(12.0)
fromInt(1) / fromFloat(12.0)->Option.getOr(0n)
})

Console.info("")
Expand Down
4 changes: 3 additions & 1 deletion tests/tests/src/core/Core_TestTests.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

import * as Test from "./Test.mjs";
import * as Pervasives from "rescript/lib/es6/Pervasives.js";
import * as Stdlib_BigInt from "rescript/lib/es6/Stdlib_BigInt.js";
import * as Stdlib_Option from "rescript/lib/es6/Stdlib_Option.js";
import * as Primitive_object from "rescript/lib/es6/Primitive_object.js";

let eq = Primitive_object.equal;

let bign = BigInt(Number.MAX_VALUE);
let bign = Stdlib_Option.getOr(Stdlib_BigInt.fromFloat(Number.MAX_VALUE), 0n);

let bign$1 = bign + bign;

Expand Down
2 changes: 1 addition & 1 deletion tests/tests/src/core/Core_TestTests.res
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
let eq = (a, b) => a == b

let bign = BigInt.fromFloat(Float.Constants.maxValue)
let bign = BigInt.fromFloat(Float.Constants.maxValue)->Option.getOr(0n)
let bign = BigInt.add(bign, bign)

Test.run(__POS_OF__("print null"), Test.print(null), eq, "null")
Expand Down