#![doc = include_str!("../README.md")]
#![cfg_attr(not(feature = "std"), no_std)]
#![forbid(unsafe_code)]
#![allow(clippy::excessive_precision)]
#![allow(clippy::approx_constant)]
#![allow(internal_features)]
#![cfg_attr(feature = "nightly", feature(core_intrinsics, const_eval_select))]
#[macro_use]
pub(crate) mod macros;
mod acos;
mod acosh;
mod asin;
mod asinh;
mod atan;
mod atan2;
mod atanh;
mod cos;
mod exp;
mod floor;
mod k_cos;
mod k_sin;
pub(crate) mod k_tan;
mod ln;
pub(crate) mod log1p;
mod pow;
mod rem_pio2;
mod rem_pio2_large;
pub(crate) mod scalbn;
mod sin;
mod tan;
pub use acos::acos;
pub use acosh::acosh;
pub use asin::asin;
pub use asinh::asinh;
pub use atan::atan;
pub use atan2::atan2;
pub use atanh::atanh;
pub use cos::cos;
pub use exp::exp;
pub use floor::floor;
pub use ln::ln;
pub use pow::pow;
pub use sin::sin;
pub use tan::tan;
const TAYLOR_SERIES_SUMS: usize = 16;
pub const fn cot(x: f64) -> f64 {
let sin_calc = sin(x);
if sin_calc == 0.0 {
f64::INFINITY
} else {
cos(x) / sin_calc
}
}
pub const fn sec(x: f64) -> f64 {
let cos_calc = cos(x);
if cos_calc == 0.0 {
f64::INFINITY
} else {
1.0 / cos_calc
}
}
pub const fn csc(x: f64) -> f64 {
let sin_calc = sin(x);
if sin_calc == 0.0 {
f64::INFINITY
} else {
1.0 / sin_calc
}
}
pub const fn sinh(x: f64) -> f64 {
nightly_exp!(sinh, sinh_inner, x)
}
const fn sinh_inner(x: f64) -> f64 {
(exp(x) - exp(-x)) / 2.0
}
pub const fn cosh(x: f64) -> f64 {
nightly_exp!(cosh, cosh_inner, x)
}
const fn cosh_inner(x: f64) -> f64 {
(exp(x) + exp(-x)) / 2.0
}
pub const fn expi(x: f64, mut pow: isize) -> f64 {
let mut o = 1.0;
while pow > 0 {
o *= x;
pow -= 1;
}
while pow < 0 {
o /= x;
pow += 1;
}
o
}
pub const fn factorial(mut x: f64) -> f64 {
if x == 0.0 || x == 1.0 {
1.0
} else {
let mut s = 1.0;
while x > 1.0 {
s *= x;
x -= 1.0;
}
s
}
}
pub const fn sqrt(x: f64) -> f64 {
nightly_exp!(sqrt, sqrt_inner, x)
}
const fn sqrt_inner(x: f64) -> f64 {
if x.is_nan() || x < 0.0 {
return f64::NAN;
} else if x.is_infinite() || x == 0.0 {
return x;
}
let mut current_guess = 1.0;
let mut i = 0;
while i < TAYLOR_SERIES_SUMS {
current_guess = 0.5 * (current_guess + x / current_guess);
i += 1;
}
current_guess
}
pub const fn fabs(x: f64) -> f64 {
x.abs()
}
const fn with_set_high_word(f: f64, hi: u32) -> f64 {
let mut tmp = f.to_bits();
tmp &= 0x00000000_ffffffff;
tmp |= (hi as u64) << 32;
f64::from_bits(tmp)
}
const fn with_set_low_word(f: f64, lo: u32) -> f64 {
let mut tmp = f.to_bits();
tmp &= 0xffffffff_00000000;
tmp |= lo as u64;
f64::from_bits(tmp)
}
const fn get_high_word(x: f64) -> u32 {
(x.to_bits() >> 32) as u32
}
const fn get_low_word(x: f64) -> u32 {
x.to_bits() as u32
}
#[cfg(test)]
mod tests {
use core::f64::consts::{E, PI};
use crate::{cos, cosh, exp, expi, factorial, ln, sin, sinh, sqrt};
macro_rules! float_eq {
($lhs:expr, $rhs:expr) => {
assert!(($lhs - $rhs).abs() < 0.0001, "lhs: {}, rhs: {}", $lhs, $rhs);
};
}
#[test]
fn test_factorial() {
assert_eq!(factorial(0.0), 1.0);
assert_eq!(factorial(1.0), 1.0);
assert_eq!(factorial(2.0), 2.0);
assert_eq!(factorial(3.0), 6.0);
assert_eq!(factorial(4.0), 24.0);
assert_eq!(factorial(5.0), 120.0);
}
#[test]
fn test_expi() {
assert_eq!(expi(2.0, 0), 1.0);
assert_eq!(expi(2.0, 4), 16.0);
assert_eq!(expi(2.0, 5), 32.0);
assert_eq!(expi(3.0, 3), 27.0);
}
#[test]
fn test_exp() {
float_eq!(exp(0.0), 1.0);
float_eq!(exp(1.0), E);
}
#[test]
fn test_sqrt() {
float_eq!(sqrt(0.0), 0.0);
float_eq!(sqrt(1.0), 1.0);
float_eq!(sqrt(4.0), 2.0);
float_eq!(sqrt(9.0), 3.0);
float_eq!(sqrt(16.0), 4.0);
float_eq!(sqrt(25.0), 5.0);
}
#[test]
fn test_cos() {
float_eq!(cos(0.0), 0.0_f64.cos());
float_eq!(cos(1.0), 1.0_f64.cos());
float_eq!(cos(PI), PI.cos());
float_eq!(cos(PI * 8.0), (PI * 8.0).cos());
}
#[test]
fn test_sin() {
float_eq!(sin(0.0), 0.0_f64.sin());
float_eq!(sin(1.0), 1.0_f64.sin());
float_eq!(sin(PI), PI.sin());
float_eq!(sin(PI * 8.0), (PI * 8.0).sin());
}
#[test]
fn test_sinh() {
for x in [0.0, 0.5, 1.0, 1.5, 2.0, 2.5] {
float_eq!(sinh(x), x.sinh());
}
}
#[test]
fn test_cosh() {
for x in [0.0, 0.5, 1.0, 1.5, 2.0, 2.5] {
float_eq!(cosh(x), x.cosh());
}
}
#[test]
fn test_ln() {
float_eq!(ln(1.0), 1.0_f64.ln());
float_eq!(ln(2.0), 2.0_f64.ln());
float_eq!(ln(10.0), 10.0_f64.ln());
float_eq!(ln(1_000.0), 1_000.0_f64.ln());
}
}