use core::ops::Sub;
#[cfg(not(feature = "std"))]
use peniko::kurbo::common::FloatFuncs as _;
pub fn compute_erf7(x: f32) -> f32 {
let x = x.clamp(-10., 10.);
let x = x * core::f32::consts::FRAC_2_SQRT_PI;
let xx = x * x;
let x = x + (0.24295 + (0.03395 + 0.0104 * xx) * xx) * (x * xx);
x / (1.0 + x * x).sqrt()
}
const SCALAR_NEARLY_ZERO: f32 = 1.0 / (1 << 12) as f32;
pub trait FloatExt: Sized + Sub<Self, Output = Self> {
fn is_nearly_zero(&self) -> bool;
fn is_nearly_zero_within_tolerance(&self, tolerance: f32) -> bool;
}
impl FloatExt for f32 {
fn is_nearly_zero(&self) -> bool {
self.is_nearly_zero_within_tolerance(SCALAR_NEARLY_ZERO)
}
fn is_nearly_zero_within_tolerance(&self, tolerance: f32) -> bool {
debug_assert!(tolerance >= 0.0, "tolerance must be positive");
self.abs() <= tolerance
}
}
impl FloatExt for f64 {
fn is_nearly_zero(&self) -> bool {
self.is_nearly_zero_within_tolerance(SCALAR_NEARLY_ZERO)
}
fn is_nearly_zero_within_tolerance(&self, tolerance: f32) -> bool {
debug_assert!(tolerance >= 0.0, "tolerance must be positive");
self.abs() <= tolerance as Self
}
}