Fix floating-point precision issues in Random.num() for integer formats #954
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.
Problem
The CBOR encoding/decoding test for
Configurationwas flaky, occasionally failing with:The
portfield is defined ast.Number({format: 'u16', gte: 1, lte: 65535}), which should generate clean integers. However, floating-point precision artifacts like11.000000000000002were occasionally being generated, which CBOR would encode as floats. Upon decoding, these became integers, causing the equality check to fail.Root Cause
In
Random.num(), an epsilon value (0.000000000000001) was being added tominwhengtewas specified and subtracted frommaxwhenltewas specified:This epsilon handling was intended to distinguish between inclusive (
gte/lte) and exclusive (gt/lt) bounds for floating-point numbers. However, when combined with theMath.round()calculation for integer formats, it introduced floating-point precision errors:Solution
The fix detects integer formats (i8, i16, i32, i64, i, u8, u16, u32, u64, u) and avoids adding/subtracting epsilon for these types:
For integer formats, this ensures:
min = 1instead ofmin = 1.000000000000001)For floating-point formats (f32, f64) and untyped numbers, the epsilon behavior is preserved to maintain precise boundary handling.
Testing
Added comprehensive test coverage:
The fix was validated with standalone tests showing:
9.000000000000002Impact
Warning
Firewall rules blocked me from connecting to one or more addresses (expand for details)
I tried to connect to the following addresses, but was blocked by firewall rules:
classic.yarnpkg.com/usr/local/bin/node /home/REDACTED/.cache/node/corepack/v1/yarn/4.10.3/yarn.js set version classic --only-if-needed --yarn-path(dns block)If you need me to access, download, or install something from one of these locations, you can either:
Original prompt
Fixes #953
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.