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
Implement full-range i256::to_f64 to eliminate ±∞ saturation for Decimal256 → Float64 casts (#7986)
# Which issue does this PR close?
Closes#7985
---
# Rationale for this change
The existing Decimal256 → Float64 conversion was changed to **saturate**
out-of-range values to `±INFINITY` (PR #7887) in order to avoid panics.
However, every 256-bit signed integer actually fits within the exponent
range of an IEEE-754 `f64` (±2¹⁰²³), so we can always produce a
**finite** `f64`, only sacrificing mantissa precision.
By overriding `i256::to_f64` to split the full 256-bit magnitude into
high/low 128-bit halves, recombine as
```text
(high as f64) * 2^128 + (low as f64)
```
and reapply the sign (special-casing i256::MIN), we:
- Eliminate both panics and infinite results
- Match Rust’s built-in (i128) as f64 rounding (ties-to-even)
- Simplify casting logic—no saturating helpers or extra flags required
# What changes are included in this PR?
- Added full-range fn to_f64(&self) -> Option<f64> for i256, using
checked_abs() + to_parts() + recombination
- Removed fallback through 64-bit to_i64()/to_u64() and .unwrap()
- Replaced the old decimal256_to_f64 saturating helper with a thin
wrapper around the new i256::to_f64() (always returns Some)
- Updated Decimal256 → Float64 cast sites to call the new helper
## Tests
- Reworked “overflow” tests to assert finite & correctly signed results
for i256::MAX and i256::MIN
- Added typical-value tests; removed expectations of ∞/-∞
# Are there any user-facing changes?
Behavior change:
- Very large or small Decimal256 values no longer become +∞/-∞.
- They now map to very large—but finite—f64 values (rounded to nearest
mantissa).
## API impact:
No public API signatures changed.
Conversion remains lossy by design; users relying on
saturation-to-infinity will observe different (more faithful) behavior.
---------
Co-authored-by: Ryan Johnson <[email protected]>
0 commit comments