Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix ldexp to handle denormalized inputs correctly
Detect denormalized input values below f64::MIN_POSITIVE
Scale subnormal inputs by 2^54 to bring them into normalized range

Signed-off-by: Yash Suthar <[email protected]>
  • Loading branch information
YashSuthar983 committed Oct 25, 2025
commit 728b7b420ac3fc430b7f66a8f2c815f9bc903c95
10 changes: 9 additions & 1 deletion stdlib/src/math.rs
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,15 @@ mod math {
// Direct multiplication would overflow for large i values, especially when computing
// the largest finite float (i=1024, x<1.0). By directly modifying the exponent bits,
// we avoid intermediate overflow to infinity.
let (mant, exp0) = float_ops::decompose_float(value);

// Scale subnormals to normal range first, then adjust exponent.
let (mant, exp0) = if value.abs() < f64::MIN_POSITIVE {
let scaled = value * (1u64 << 54) as f64; // multiply by 2^54
let (mant_scaled, exp_scaled) = float_ops::decompose_float(scaled);
(mant_scaled, exp_scaled - 54) // adjust exponent back
} else {
float_ops::decompose_float(value)
};

let i_big = i.as_bigint();
let overflow_bound = BigInt::from(1024_i32 - exp0); // i > 1024 - exp0 => overflow
Expand Down