Skip to content

Implement full-range i256::to_f64 to replace current ±∞ saturation for Decimal256 → Float64 #7985

@kosiew

Description

@kosiew

Background

Proposal

Override ToPrimitive::to_f64 for i256 so that:

  1. No panics (we’ll never call .unwrap() or fall through to None).
  2. No infinities—every value maps to a finite, rounded f64.
  3. Behavior matches Rust’s built-in (i128) as f64 logic: ties-to-even rounding, full exponent coverage.

Suggested implementation in arrow-buffer/src/bigint/mod.rs

impl ToPrimitive for i256 {
    fn to_f64(&self) -> Option<f64> {
        // Handle the one case where abs() would overflow: i256::MIN
        let mag = if let Some(abs) = self.checked_abs() {
            // Break the magnitude into (low: u128, high: i128)
            let (low, high) = abs.to_parts();
            // Recombine: high * 2^128 + low
            (high as f64) * 2_f64.powi(128) + (low as f64)
        } else {
            // self == i256::MIN → exactly 2^255
            2_f64.powi(255)
        };

        // Reapply the sign bit
        Some(if *self < i256::ZERO { -mag } else { mag })
    }

    // ... other methods unchanged ...
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions