You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Fix panic on lossy decimal to float casting: round to saturation for overflows (#7887)
# Which issue does this PR close?
Closes#7886.
# Rationale for this change
Casting large `Decimal256` values to `Float64` can exceed the
representable range of floating point numbers. Previously, this could
result in a panic due to unwrapping a failed conversion.
This PR introduces a safe conversion that saturates overflowing values
to `INFINITY` or `-INFINITY`, following standard floating point
semantics. This ensures stable, predictable behavior without runtime
crashes.
# What changes are included in this PR?
- Introduced a helper function `decimal256_to_f64` that converts `i256`
to `f64`, returning `INFINITY` or `-INFINITY` when the value is out of
range.
- Updated the casting logic for `Decimal256` → `Float64` to use the new
safe conversion.
- Improved inline and module-level documentation to reflect that this
conversion is lossy and saturating.
- Added a unit test `test_cast_decimal256_to_f64_overflow` to validate
overflow behavior.
# Are there any user-facing changes?
Yes.
- **Behavior Change:** When casting `Decimal256` values that exceed the
`f64` range, users now receive `INFINITY` or `-INFINITY` instead of a
panic.
- **Improved Docs:** Updated documentation clarifies the lossy and
saturating behavior of decimal-to-float casting.
- **Not a Breaking Change:** There are no API changes, but users relying
on panics for overflow detection may observe different behavior.
0 commit comments