Repository navigation
Conversation
…rets the bits The spec says `cast` reinterprets the bits, and the C backend does so, but jsgen converted the value numerically: `cast[uint32](1.5'f32)` gave 1 and `cast[int64](1.5)` threw (BigInt of a non-integer). Casts where one side is a float and the other a float or integer of another kind now store the source in a little-endian DataView and read the destination back from it. This also fixes `std/formatfloat.writeFloatToBufferRoundtrip` on JS, whose dragonbox and schubfach read the float's bits with `cast`. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
fixes #22326
The manual says
castreinterprets the bits, and the C backend does so. jsgen'sgenCastdid not for floats and integers: it either left the Number as it was or converted it numerically.cast[uint32](1.5'f32)1.5(printing it throws a RangeError)1069547520(0x3FC00000)cast[int64](1.5)4609434218613702656cast[float](0x4059000000000000'i64)4636737291354636000.0100.0genCastis where the JS backend decides what acastmeans, so the fix is there: when one side is a float and the other a float or integer of another kind, the source is stored in a little-endianDataViewand the destination is read back from it. 64-bit integers go throughsetBigInt64/getBigInt64and are wrapped withBigInt(...)/Number(...)when they are Numbers (--jsbigint64:off).floatandfloat64stay a no-op.This also fixes
std/formatfloat.writeFloatToBufferRoundtripon JS (reported in the issue), whose dragonbox and schubfach read the float's bits withcast: it printed5e-324for1.5and now prints1.5. The local workaround instd/hashes(toBitson JS) is left as it is.Test:
tests/js/tcastfloatbits.nimruns the same assertions on C and JS, each with--jsbigint64:onandoff(the formatfloat part only where 64-bit integers are exact). Checked on macOS arm64 with node 26: the new test 4/4,testament cat js260 passed and 0 failed,tests/stdlib/thashes.nimandtests/float/tfloats.nimpass on all their targets.🤖 Generated with Claude Code