#![no_std]
#![doc = "Minimal numeric traits for the vector-space/GA ecosystem."]
mod float;
mod int;
use core::ops::{Add, Div, Mul, Neg, Rem, Sub};
#[must_use]
#[inline]
pub fn damp_factor<R: Real>(half_lives: R) -> R {
R::one() - (-half_lives).exp2()
}
#[must_use]
#[inline]
pub fn damp<R: Real>(current: R, target: R, half_lives: R) -> R {
current + (target - current) * damp_factor(half_lives)
}
pub trait Zero {
#[must_use]
fn zero() -> Self;
fn is_zero(&self) -> bool;
}
pub trait One {
#[must_use]
fn one() -> Self;
fn is_one(&self) -> bool;
}
pub trait Inv {
type Output;
#[must_use]
fn inv(self) -> Self::Output;
}
pub trait Sqrt {
#[must_use]
fn sqrt(self) -> Self;
}
pub trait Exp {
type Output;
#[must_use]
fn exp(self) -> Self::Output;
#[must_use]
fn exp2(self) -> Self::Output;
}
pub trait Logarithm {
#[must_use]
fn ln(self) -> Self;
#[must_use]
fn log2(self) -> Self;
#[must_use]
fn powf(self, exponent: Self) -> Self;
}
pub trait Trigonometry: Sized {
#[must_use]
fn sin_cos(self) -> [Self; 2];
#[must_use]
fn sin(self) -> Self {
let [sin, _] = self.sin_cos();
sin
}
#[must_use]
fn cos(self) -> Self {
let [_, cos] = self.sin_cos();
cos
}
#[must_use]
fn tan(self) -> Self;
}
pub trait InverseTrigonometry {
#[must_use]
fn asin(self) -> Self;
#[must_use]
fn acos(self) -> Self;
#[must_use]
fn atan(self) -> Self;
#[must_use]
fn atan2(self, other: Self) -> Self;
}
pub trait Clamp: Sized {
#[must_use]
fn min(self, other: Self) -> Self;
#[must_use]
fn max(self, other: Self) -> Self;
#[must_use]
fn clamp(self, lower: Self, upper: Self) -> Self {
self.max(lower).min(upper)
}
}
#[cfg(feature = "std")]
pub trait FromStrRadix: Sized {
type Error;
fn from_str_radix(source: &str, radix: u32) -> Result<Self, Self::Error>;
}
pub trait Numeric:
Copy + PartialEq + Add<Output = Self> + Sub<Output = Self> + Mul<Output = Self> + Zero + One
{
}
impl<T> Numeric for T where
T: Copy + PartialEq + Add<Output = Self> + Sub<Output = Self> + Mul<Output = Self> + Zero + One
{
}
pub trait Integer: Numeric + Eq + Ord + Div<Output = Self> + Rem<Output = Self> {}
impl<T> Integer for T where T: Numeric + Eq + Ord + Div<Output = Self> + Rem<Output = Self> {}
pub trait Real:
Numeric
+ PartialOrd
+ Div<Output = Self>
+ Neg<Output = Self>
+ Inv<Output = Self>
+ Sqrt
+ Exp<Output = Self>
+ Logarithm
+ Trigonometry
+ InverseTrigonometry
+ Clamp
{
}
impl<T> Real for T where
T: Numeric
+ PartialOrd
+ Div<Output = Self>
+ Neg<Output = Self>
+ Inv<Output = Self>
+ Sqrt
+ Exp<Output = Self>
+ Logarithm
+ Trigonometry
+ InverseTrigonometry
+ Clamp
{
}