Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement inverse trigonometric functions from ratio to angle. #184

Merged
merged 1 commit into from
May 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions src/si/angle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,22 @@ where
}
}

#[cfg(feature = "std")]
impl<D, U, V> ::si::Quantity<D, U, V>
where
D: ::si::Dimension + ?Sized,
U: ::si::Units<V> + ?Sized,
V: ::num::Float + ::Conversion<V>,
radian: ::Conversion<V, T = V::T>,
{
/// Computes the four quadrant arctangent of self (y) and other (x).
#[inline(always)]
pub fn atan2(self, other: Self) -> Angle<U, V>
{
Angle::new::<radian>(self.value.atan2(other.value))
}
}

mod convert {
use super::*;

Expand Down Expand Up @@ -148,6 +164,7 @@ mod tests {
use ::lib::f64::consts::PI;
use num::{FromPrimitive, Zero};
use si::angle as a;
use si::length as l;
use si::quantities::*;
use tests::Test;

Expand Down Expand Up @@ -190,6 +207,14 @@ mod tests {
Test::assert_approx_eq(&zero.tanh(), &0.0);
Test::assert_approx_eq(&nzero.tanh(), &0.0);
}

quickcheck! {
#[allow(trivial_casts)]
fn atan2(y: V, x: V) -> bool {
Test::eq(&y.atan2(x),
&Length::new::<l::meter>(y).atan2(Length::new::<l::meter>(x)).get::<a::radian>())
}
}
}
}
}
89 changes: 89 additions & 0 deletions src/si/ratio.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
//! Ratio (dimensionless quantity).

#[cfg(feature = "std")]
use super::angle::{Angle, radian};

quantity! {
/// Ratio (dimensionless quantity).
quantity: Ratio; "ratio";
Expand Down Expand Up @@ -29,6 +32,51 @@ quantity! {
}
}

/// Implementation of various stdlib inverse trigonometric functions
#[cfg(feature = "std")]
impl<U, V> Ratio<U, V>
where
U: ::si::Units<V> + ?Sized,
V: ::num::Float + ::Conversion<V>,
radian: ::Conversion<V, T = V::T>,
{
/// Computes the value of the inverse cosine of the ratio.
#[inline(always)]
pub fn acos(self) -> Angle<U, V> {
Angle::new::<radian>(self.value.acos())
}

/// Computes the value of the inverse hyperbolic cosine of the ratio.
#[inline(always)]
pub fn acosh(self) -> Angle<U, V> {
Angle::new::<radian>(self.value.acosh())
}

/// Computes the value of the inverse sine of the ratio.
#[inline(always)]
pub fn asin(self) -> Angle<U, V> {
Angle::new::<radian>(self.value.asin())
}

/// Computes the value of the inverse hyperbolic sine of the ratio.
#[inline(always)]
pub fn asinh(self) -> Angle<U, V> {
Angle::new::<radian>(self.value.asinh())
}

/// Computes the value of the inverse tangent of the ratio.
#[inline(always)]
pub fn atan(self) -> Angle<U, V> {
Angle::new::<radian>(self.value.atan())
}

/// Computes the value of the inverse hyperbolic tangent of the ratio.
#[inline(always)]
pub fn atanh(self) -> Angle<U, V> {
Angle::new::<radian>(self.value.atanh())
}
}

mod convert {
use super::*;

Expand Down Expand Up @@ -103,4 +151,45 @@ mod tests {
&Ratio::new::<r::part_per_quadrillion>(V::one()));
}
}

#[cfg(feature = "std")]
mod inv_trig {
storage_types! {
types: Float;

use si::angle as a;
use si::quantities::*;
use tests::Test;

fn test_nan_or_eq(yl: V, yr: V) -> bool {
(yl.is_nan() && yr.is_nan()) || Test::eq(&yl, &yr)
}

quickcheck! {
fn acos(x: V) -> bool {
test_nan_or_eq(x.acos(), Ratio::from(x).acos().get::<a::radian>())
}

fn acosh(x: V) -> bool {
test_nan_or_eq(x.acosh(), Ratio::from(x).acosh().get::<a::radian>())
}

fn asin(x: V) -> bool {
test_nan_or_eq(x.asin(), Ratio::from(x).asin().get::<a::radian>())
}

fn asinh(x: V) -> bool {
test_nan_or_eq(x.asinh(), Ratio::from(x).asinh().get::<a::radian>())
}

fn atan(x: V) -> bool {
test_nan_or_eq(x.atan(), Ratio::from(x).atan().get::<a::radian>())
}

fn atanh(x: V) -> bool {
test_nan_or_eq(x.atanh(), Ratio::from(x).atanh().get::<a::radian>())
}
}
}
}
}