Skip to content

Fail instead of returning inf when an int is too large to convert to float - #254

Closed
Dilshad-Tanvi wants to merge 1 commit into
facebook:mainfrom
Dilshad-Tanvi:int-to-float-too-large
Closed

Dilshad-Tanvi wants to merge 1 commit into
facebook:mainfrom
Dilshad-Tanvi:int-to-float-too-large

Conversation

@Dilshad-Tanvi

Copy link
Copy Markdown
Contributor

Converting an int to a float goes through NumRef::as_float, which uses BigInt::to_f64 and gives +inf/-inf once the int is outside the f64 range. So float(1 << 1024) returns +inf, (1 << 1024) + 0.5, * 1.0, // and % all quietly come out as inf, (1 << 1100) / (1 << 1000) is +inf instead of 1.2676506002282294e+30, and "%f" % (1 << 1024) prints +inf. The spec says float(x) "fails if x is too large to represent as a finite float" and that the implicit conversion in mixed arithmetic fails the same way. starlark-go reports int too large to convert to float, and float() of the same number written as a string already errors here.

The fix adds NumRef::as_finite_float, which errors for an int whose conversion is infinite, and uses it in float(), the mixed + - * / // % paths and %e/%f/%g. Int/int arithmetic, comparisons and hashing are unchanged. testcases/eval/go/float.star already has the starlark-go assertions for these cases, but tests/go.rs filtered them out. Those lines now run and pass, and they fail without the fix. cargo test and cargo clippy are clean.

…float

NumRef::as_float converts a big int with BigInt::to_f64, which yields
+/-inf outside the f64 range, so float(1 << 1024), mixed int/float
arithmetic and %f formatting silently returned inf. The spec says these
conversions fail, and float() of the same number as a string already
does. Convert through a checked NumRef::as_finite_float instead, and
stop filtering the Go conformance assertions that cover it.
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Oct 21, 2026
@meta-codesync

meta-codesync Bot commented Oct 21, 2026

Copy link
Copy Markdown
Contributor

This pull request has been imported. If you are a Meta employee, you can view this in D126649355. (Because this pull request was imported automatically, there will not be any future comments.)

@JakobDegen JakobDegen left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review automatically exported from Phabricator review in Meta.

meta-codesync Bot pushed a commit to facebook/buck2 that referenced this pull request Oct 21, 2026
…float

Summary:
Converting an int to a float goes through `NumRef::as_float`, which uses `BigInt::to_f64` and gives `+inf`/`-inf` once the int is outside the f64 range. So `float(1 << 1024)` returns `+inf`, `(1 << 1024) + 0.5`, `* 1.0`, `//` and `%` all quietly come out as `inf`, `(1 << 1100) / (1 << 1000)` is `+inf` instead of `1.2676506002282294e+30`, and `"%f" % (1 << 1024)` prints `+inf`. The spec says `float(x)` "fails if x is too large to represent as a finite float" and that the implicit conversion in mixed arithmetic fails the same way. starlark-go reports `int too large to convert to float`, and `float()` of the same number written as a string already errors here.

The fix adds `NumRef::as_finite_float`, which errors for an int whose conversion is infinite, and uses it in `float()`, the mixed `+ - * / // %` paths and `%e/%f/%g`. Int/int arithmetic, comparisons and hashing are unchanged. `testcases/eval/go/float.star` already has the starlark-go assertions for these cases, but `tests/go.rs` filtered them out. Those lines now run and pass, and they fail without the fix. `cargo test` and `cargo clippy` are clean.

X-link: facebook/starlark-rust#254

Reviewed By: JakobDegen

Differential Revision: D126649355

fbshipit-source-id: 52ab66d4d0c365f3254dd8b0f1481f949d049d23
@meta-codesync meta-codesync Bot closed this in 89d4a04 Oct 21, 2026
@meta-codesync meta-codesync Bot added the Merged label Oct 21, 2026
@meta-codesync

meta-codesync Bot commented Oct 21, 2026

Copy link
Copy Markdown
Contributor

This pull request has been merged in 89d4a04.

@Dilshad-Tanvi

Copy link
Copy Markdown
Contributor Author

Thanks for landing this, glad the float conversion is strict now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Merged

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants