mpmfnum 0.2.0

A numbers library in Rust
Documentation
use crate::rfloat::RFloat;
use crate::{Real, RoundingContext};

/// Rounding contexts for exact arithmetic.
///
/// The rounding operation is just the identity function.
#[derive(Clone, Debug, Default)]
pub struct RealContext {}

impl RealContext {
    pub fn new() -> Self {
        Self::default()
    }
}

impl RoundingContext for RealContext {
    type Format = RFloat;

    fn round<T: Real>(&self, val: &T) -> Self::Format {
        if val.is_zero() {
            RFloat::zero()
        } else if val.is_finite() {
            let sign = val.sign().unwrap_or(false);
            RFloat::Real(sign, val.exp().unwrap(), val.c().unwrap())
        } else if val.is_infinite() {
            match val.sign() {
                Some(true) => RFloat::NegInfinity,
                _ => RFloat::PosInfinity,
            }
        } else {
            RFloat::Nan
        }
    }
}