diff --git a/experiments/2025-03-18/src/math/scalar.rs b/experiments/2025-03-18/src/math/scalar.rs index 4d4a1b031..71f65b78c 100644 --- a/experiments/2025-03-18/src/math/scalar.rs +++ b/experiments/2025-03-18/src/math/scalar.rs @@ -1,114 +1 @@ -use std::{cmp::Ordering, ops}; - -#[derive(Clone, Copy, Debug, PartialEq)] -pub struct Scalar { - value: f64, -} - -impl Scalar { - pub const ZERO: Self = Self { value: 0. }; - - pub fn from_f64(value: f64) -> Self { - if value.is_nan() { - panic!("`Scalar` value must not be NaN"); - } - if value.is_infinite() { - panic!("`Scalar` value must not be infinite. Value: `{value}`"); - } - - Self { value } - } - - pub fn into_f64(self) -> f64 { - self.value - } - - pub fn sqrt(self) -> Self { - let value = self.into_f64().sqrt(); - Self::from_f64(value) - } -} - -impl Eq for Scalar {} - -impl Ord for Scalar { - fn cmp(&self, other: &Self) -> Ordering { - let Some(ordering) = self.value.partial_cmp(&other.value) else { - unreachable!( - "Failed to compare `Scalar` values `{}` and `{}`", - self.value, other.value - ); - }; - - ordering - } -} - -impl PartialOrd for Scalar { - fn partial_cmp(&self, other: &Self) -> Option { - Some(self.cmp(other)) - } -} - -impl From for Scalar { - fn from(value: f64) -> Self { - Self::from_f64(value) - } -} - -impl ops::Add for Scalar -where - S: Into, -{ - type Output = Self; - - fn add(self, other: S) -> Self::Output { - let value = self.into_f64() + other.into().into_f64(); - Self::from_f64(value) - } -} - -impl ops::Div for Scalar -where - S: Into, -{ - type Output = Self; - - fn div(self, other: S) -> Self::Output { - let value = self.into_f64() / other.into().into_f64(); - Self::from_f64(value) - } -} - -impl ops::Mul for Scalar -where - S: Into, -{ - type Output = Self; - - fn mul(self, other: S) -> Self::Output { - let value = self.into_f64() * other.into().into_f64(); - Self::from_f64(value) - } -} - -impl ops::Neg for Scalar { - type Output = Self; - - fn neg(self) -> Self::Output { - let value = -self.into_f64(); - Self::from_f64(value) - } -} - -impl ops::Sub for Scalar -where - S: Into, -{ - type Output = Self; - - fn sub(self, other: S) -> Self::Output { - let value = self.into_f64() - other.into().into_f64(); - Self::from_f64(value) - } -} +pub use fj_math::Scalar;