Fix RealVal for floats requiring scientific notation - #117
Merged
Conversation
`RealVal` passed `str(val)` to `mkReal`, but `str` switches to scientific notation for small and large magnitudes (`str(1e-5) == '1e-05'`), which `mkReal` rejects. Convert floats via `format(Decimal(repr(val)), "f")` instead. `repr` yields the shortest decimal that round-trips to the float, and formatting the `Decimal` with `"f"` expands it without an exponent, so the resulting rational is exactly the value Python would print, for every finite float. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
daniel-larraz
force-pushed
the
fix-float-realval
branch
from
August 12, 2026 03:42
a31455b to
127e8c3
Compare
alex-ozdemir
approved these changes
Aug 12, 2026
alex-ozdemir
left a comment
Member
There was a problem hiding this comment.
Yes, this approach is right, thanks @daniel-larraz .
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.
Alternative to #114, which addresses the same bug.
The bug
RealValpassesstr(val)tomkReal. For small and large magnitudesstrswitches to scientific notation, whichmkRealrejects:This fix
reprgives the shortest decimal string that round-trips to the float, and formatting the resultingDecimalwith"f"expands it without an exponent. The result is exactly the value Python itself would print, for every finite float — no rounding, no magnitude cutoff.Why not #114
#114 fixes the crash, but its mechanism has two problems.
1.
f"{val:.10f}"silently rounds every float to 10 fractional digits. This loses precision for values that worked correctly before the patch:RealVal(1.5e-10)1/10000000000(33% error)3/20000000000RealVal(2.5e-10)1/2500000000(20% error)1/4000000000RealVal(1/3)3333333333/100000000003333333333333333/10000000000000000Since
RealValbacks_py2exprandArithSortRef.cast, this also affects ordinary expressions likex + 1/3, not just directRealValcalls.2. The
1e-10threshold gives floats two incompatible semantics. Below the threshold #114 usesas_integer_ratio(), i.e. the exact binary value; above it, a rounded decimal. So which meaning a user gets depends on magnitude:Using the round-tripping decimal everywhere keeps a single semantics —
RealVal(f)is the rational denoted byrepr(f)— and both identities above hold.as_integer_ratio()alone would also be defensible, but it makesRealVal(0.1)be3602879701896397/36028797018963968rather than1/10, which is surprising and changes existing printed forms.Notes
RealVal(1.5),RealVal(1e-5), and the1e-11exactness identity.RealVal(float('nan'))/RealVal(float('inf'))still raiseRuntimeErrorfrommkReal, as before.mainafter Update the multiple_solvers test output #116; the full suite passes andblack --checkis clean.