use crate::Df64;
use libm::expm1;
use simba::scalar::ComplexField;
use std::fmt::Debug;
pub trait CustomNumeric:
Copy
+ Debug
+ PartialOrd
+ std::fmt::Display
+ std::ops::Add<Output = Self>
+ std::ops::Sub<Output = Self>
+ std::ops::Mul<Output = Self>
+ std::ops::Div<Output = Self>
+ std::ops::Neg<Output = Self>
+ num_traits::Zero + simba::scalar::ComplexField {
fn from_f64_unchecked(x: f64) -> Self;
fn convert_from<U: CustomNumeric + 'static>(value: U) -> Self;
fn to_f64(self) -> f64;
fn epsilon() -> Self;
fn pi() -> Self;
fn max(self, other: Self) -> Self;
fn min(self, other: Self) -> Self;
fn is_valid(&self) -> bool;
fn abs_as_same_type(self) -> Self;
fn exp_m1(self) -> Self;
}
impl CustomNumeric for f64 {
fn from_f64_unchecked(x: f64) -> Self {
x
}
fn convert_from<U: CustomNumeric + 'static>(value: U) -> Self {
match std::any::TypeId::of::<U>() {
id if id == std::any::TypeId::of::<f64>() => {
value.to_f64()
}
id if id == std::any::TypeId::of::<Df64>() => {
value.to_f64()
}
_ => value.to_f64(),
}
}
fn to_f64(self) -> f64 {
self
}
fn epsilon() -> Self {
f64::EPSILON
}
fn pi() -> Self {
std::f64::consts::PI
}
fn max(self, other: Self) -> Self {
self.max(other)
}
fn min(self, other: Self) -> Self {
self.min(other)
}
fn is_valid(&self) -> bool {
num_traits::Float::is_finite(*self)
}
fn abs_as_same_type(self) -> Self {
Self::convert_from(self.abs())
}
fn exp_m1(self) -> Self {
expm1(self)
}
}
impl CustomNumeric for Df64 {
fn from_f64_unchecked(x: f64) -> Self {
Df64::from(x)
}
fn convert_from<U: CustomNumeric + 'static>(value: U) -> Self {
match std::any::TypeId::of::<U>() {
id if id == std::any::TypeId::of::<f64>() => {
let f64_value = value.to_f64();
Self::from_f64_unchecked(f64_value)
}
id if id == std::any::TypeId::of::<Df64>() => {
unsafe {
std::mem::transmute_copy(&value)
}
}
_ => Self::from_f64_unchecked(value.to_f64()),
}
}
fn to_f64(self) -> f64 {
let hi = self.hi();
let lo = self.lo();
hi + lo
}
fn epsilon() -> Self {
let epsilon_value = f64::EPSILON * f64::EPSILON / 2.0;
Df64::from(epsilon_value)
}
fn pi() -> Self {
Df64::from(std::f64::consts::PI)
}
fn max(self, other: Self) -> Self {
if self > other { self } else { other }
}
fn min(self, other: Self) -> Self {
if self < other { self } else { other }
}
fn is_valid(&self) -> bool {
let value = *self;
f64::from(value).is_finite() && !f64::from(value).is_nan()
}
fn abs_as_same_type(self) -> Self {
Self::convert_from(self.abs())
}
fn exp_m1(self) -> Self {
ComplexField::exp_m1(self)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_f64_custom_numeric() {
let x = 1.5_f64;
let y = -2.0_f64;
assert_eq!(x.abs(), 1.5);
assert_eq!(y.abs(), 2.0);
let cos_x = x.cos();
assert!(f64::from(cos_x).is_finite());
let sqrt_x = x.sqrt();
assert!(f64::from(sqrt_x).is_finite());
assert_eq!(x.to_f64(), 1.5);
assert_eq!(<f64 as CustomNumeric>::epsilon(), f64::EPSILON);
}
#[test]
fn test_twofloat_custom_numeric() {
let x = Df64::from_f64_unchecked(1.5);
let y = Df64::from_f64_unchecked(-2.0);
assert_eq!(x.abs(), Df64::from_f64_unchecked(1.5));
assert_eq!(y.abs(), Df64::from_f64_unchecked(2.0));
let cos_x = x.cos();
assert!(f64::from(cos_x).is_finite());
let sqrt_x = x.sqrt();
assert!(f64::from(sqrt_x).is_finite());
let x_f64 = x.to_f64();
assert!((x_f64 - 1.5).abs() < 1e-15);
let eps = Df64::epsilon();
assert!(eps > Df64::from_f64_unchecked(0.0));
assert!(eps < Df64::from_f64_unchecked(1.0));
}
#[test]
fn test_precision_comparison() {
let pi_f64 = std::f64::consts::PI;
let pi_tf = Df64::from_f64_unchecked(pi_f64);
assert!(pi_f64.is_finite());
assert!(f64::from(pi_tf).is_finite());
let pi_back = pi_tf.to_f64();
assert!((pi_back - pi_f64).abs() < f64::EPSILON);
}
}