askew 0.1.0

Angles (degrees, radians, and turns)
Documentation
#[cfg(not(feature = "libm"))]
compile_error!("Requires the `libm` feature to be enabled.");

#[inline]
pub fn tan_f32(x: f32) -> f32 {
    libm::tanf(x)
}

#[inline]
pub fn sin_f32(x: f32) -> f32 {
    libm::sinf(x)
}

#[inline]
pub fn cos_f32(x: f32) -> f32 {
    libm::cosf(x)
}

#[inline]
pub fn atan_f32(x: f32) -> f32 {
    libm::atanf(x)
}

#[inline]
pub fn atan2_f32(y: f32, x: f32) -> f32 {
    libm::atan2f(y, x)
}

#[inline]
pub fn asin_f32(x: f32) -> f32 {
    libm::asinf(x)
}

#[inline]
pub fn acos_f32(x: f32) -> f32 {
    libm::acosf(x)
}

#[cfg(test)]
#[allow(clippy::float_cmp)]
mod tests {
    use super::*;

    #[test]
    fn test_tan_f32() {
        assert_eq!(tan_f32(0.0), 0.0);
        assert_eq!(tan_f32(std::f32::consts::FRAC_PI_4), 1.0);
    }

    #[test]
    fn test_sin_f32() {
        assert_eq!(sin_f32(0.0), 0.0);
        assert_eq!(sin_f32(std::f32::consts::FRAC_PI_2), 1.0);
    }

    #[test]
    fn test_cos_f32() {
        assert_eq!(cos_f32(0.0), 1.0);
        let expected = 0.0;
        let actual = cos_f32(std::f32::consts::FRAC_PI_2);
        assert!(
            (actual - expected).abs() < 1e-6,
            "actual: {actual}, expected: {expected}"
        );
    }

    #[test]
    fn test_atan_f32() {
        assert_eq!(atan_f32(0.0), 0.0);
        assert_eq!(atan_f32(1.0), std::f32::consts::FRAC_PI_4);
    }

    #[test]
    fn test_atan2_f32() {
        assert_eq!(atan2_f32(0.0, 1.0), 0.0);
        assert_eq!(atan2_f32(1.0, 1.0), std::f32::consts::FRAC_PI_4);
    }

    #[test]
    fn test_asin_f32() {
        assert_eq!(asin_f32(0.0), 0.0);
        let expected = std::f32::consts::FRAC_PI_2;
        let actual = asin_f32(1.0);
        assert!(
            (actual - expected).abs() < 1e-6,
            "actual: {actual}, expected: {expected}"
        );
    }

    #[test]
    fn test_acos_f32() {
        assert_eq!(acos_f32(1.0), 0.0);
        assert_eq!(acos_f32(0.0), std::f32::consts::FRAC_PI_2);
    }
}