use num::Float;
use std::fmt::Debug;
#[derive(Clone, Eq, Hash, PartialEq)]
pub struct FloatingPointComponents(u64, i16, i8);
#[derive(Debug)]
pub struct NanError;
#[doc(hidden)]
pub struct FloatWrap<F: Float>(F);
impl FloatingPointComponents {
#[inline]
pub fn new<F: Float>(num: F) -> Result<Self, NanError> {
Self::try_from(FloatWrap(num))
}
}
impl FloatingPointComponents {
#[inline]
pub fn as_f32(&self) -> f32 {
let sign_f = self.2 as f32;
let mantissa_f = self.0 as f32;
let exponent_f = (2 as f32).powf(self.1 as f32);
sign_f * mantissa_f * exponent_f
}
#[inline]
pub fn as_f64(&self) -> f64 {
let sign_f = self.2 as f64;
let mantissa_f = self.0 as f64;
let exponent_f = (2 as f64).powf(self.1 as f64);
sign_f * mantissa_f * exponent_f
}
}
impl std::fmt::Debug for FloatingPointComponents {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Float")
.field("sign", &self.2)
.field("mantissa", &self.0)
.field("exponent", &(2 as f64).powf(self.1 as f64))
.finish()
.and(write!(f, " ({})", self.as_f64()))
}
}
impl<F: Float> TryFrom<FloatWrap<F>> for FloatingPointComponents {
type Error = NanError;
fn try_from(value: FloatWrap<F>) -> Result<Self, Self::Error> {
let value = value.0;
if value.is_nan() {
return Err(NanError);
}
let (mantissa, exponent, sign) = value.integer_decode();
Ok(Self(mantissa, exponent, sign))
}
}
impl Into<f64> for &FloatingPointComponents {
fn into(self) -> f64 {
let sign_f = self.2 as f64;
let mantissa_f = self.0 as f64;
let exponent_f = (2 as f64).powf(self.1 as f64);
sign_f * mantissa_f * exponent_f
}
}
impl Into<f32> for &FloatingPointComponents {
fn into(self) -> f32 {
let sign_f = self.2 as f32;
let mantissa_f = self.0 as f32;
let exponent_f = (2 as f32).powf(self.1 as f32);
sign_f * mantissa_f * exponent_f
}
}
impl Into<f64> for FloatingPointComponents {
fn into(self) -> f64 {
let sign_f = self.2 as f64;
let mantissa_f = self.0 as f64;
let exponent_f = (2 as f64).powf(self.1 as f64);
sign_f * mantissa_f * exponent_f
}
}
impl Into<f32> for FloatingPointComponents {
fn into(self) -> f32 {
let sign_f = self.2 as f32;
let mantissa_f = self.0 as f32;
let exponent_f = (2 as f32).powf(self.1 as f32);
sign_f * mantissa_f * exponent_f
}
}
#[macro_export]
macro_rules! lifering {
($float:expr) => {
FloatingPointComponents::new($float).unwrap()
};
}