Added the basic trigonometric functions.

These were moved to their own trait and removed from the definition of
a Real number. This allows the definition of a trigonometric function
to call a different libc trig function depending on type.
This commit is contained in:
2017-06-27 18:19:33 -04:00
parent cbde731b74
commit 43a1e30808
14 changed files with 839 additions and 1094 deletions

134
src/trig/degree.rs Normal file
View File

@ -0,0 +1,134 @@
use std::ops::{Add, Sub, Mul, Div, Rem, Neg};
use std::ops::{AddAssign, SubAssign, MulAssign, DivAssign, RemAssign};
use ::constants::Constants;
use ::real::Real;
use ::trig::radian::Radian;
/// A degree usually denoted by ° (the degree symbol),
/// is a measurement of a plane angle, defined so that a
/// full rotation is 360 degree.
#[derive(Clone, Copy, Eq, Ord, PartialEq, PartialOrd, Hash)]
pub struct Degree<T>
{
value: T
}
impl<T> Degree<T> where T: Real
{
/// Create a new Degree structure with the given value.
pub fn new(val: T) -> Self
{
Degree
{
value: val
}
}
}
impl<T> ::std::ops::Deref for Degree<T> where T: Real
{
type Target = T;
fn deref(&self) -> &T
{
&self.value
}
}
impl<T> ::std::ops::DerefMut for Degree<T> where T: Real
{
fn deref_mut<'a>(&'a mut self) -> &'a mut T
{
&mut self.value
}
}
binary_operator_impl!(Add::add, Real, Degree {value});
binary_operator_impl!(Sub::sub, Real, Degree {value});
binary_operator_impl!(Mul::mul, Real, Degree {value});
binary_operator_impl!(Div::div, Real, Degree {value});
binary_operator_impl!(Rem::rem, Real, Degree {value});
impl<T> ::std::ops::Neg for Degree<T>
where T: Neg<Output = T>
{
type Output = Degree<T>;
fn neg(self) -> Degree<T>
{
let mut degree: Degree<T>;
degree = self;
degree.value = degree.value.neg();
degree
}
}
binary_operator_assign_impl!(AddAssign::add_assign,
Real, Degree {value});
binary_operator_assign_impl!(SubAssign::sub_assign,
Real, Degree {value});
binary_operator_assign_impl!(MulAssign::mul_assign,
Real, Degree {value});
binary_operator_assign_impl!(DivAssign::div_assign,
Real, Degree {value});
binary_operator_assign_impl!(RemAssign::rem_assign,
Real, Degree {value});
impl ::std::convert::From<Degree<f32>> for f32
{
fn from(degree: Degree<f32>) -> f32
{
degree.value
}
}
impl ::std::convert::From<Degree<f64>> for f64
{
fn from(degree: Degree<f64>) -> f64
{
degree.value
}
}
impl<T> ::std::convert::From<T> for Degree<T> where T: Real
{
fn from(val: T) -> Degree<T>
{
Degree::new(val)
}
}
impl<T> ::std::convert::From<Radian<T>> for Degree<T> where T: Real
{
fn from(radians: Radian<T>) -> Degree<T>
{
let degs: T;
degs = *radians * Constants::INVERSE_PI_DIVIDED_BY_180;
Degree::new(degs)
}
}
impl<T> ::std::fmt::Debug for Degree<T>
where T: Real
{
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result
{
::std::fmt::Display::fmt(self, f)
}
}
impl<T> ::std::fmt::Display for Degree<T>
where T: Real
{
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result
{
write!(f, "Degree({})", self.value)
}
}

15
src/trig/mod.rs Normal file
View File

@ -0,0 +1,15 @@
//! This section of the library handles the branch of mathematics
//! that studies relationships involving lengths and angles of triangles.
mod degree;
//mod grad;
mod radian;
mod trig;
//mod turn;
pub use self::degree::Degree;
//pub use self::grad::Grad;
pub use self::radian::Radian;
pub use self::trig::Trig;
//pub use self::turn::Turn;

133
src/trig/radian.rs Normal file
View File

@ -0,0 +1,133 @@
use std::ops::{Add, Sub, Mul, Div, Rem, Neg};
use std::ops::{AddAssign, SubAssign, MulAssign, DivAssign, RemAssign};
use ::constants::Constants;
use ::real::Real;
use ::trig::degree::Degree;
/// A unit of angle, equal to an angle at the center of
/// a circle whose arc is equal in length to the radius.
#[derive(Clone, Copy, Eq, Ord, PartialEq, PartialOrd, Hash)]
pub struct Radian<T>
{
value: T
}
impl<T> Radian<T> where T: Real
{
/// Create a new Radian structure with the given value.
pub fn new(val: T) -> Self
{
Radian
{
value: val
}
}
}
impl<T> ::std::ops::Deref for Radian<T> where T: Real
{
type Target = T;
fn deref(&self) -> &T
{
&self.value
}
}
impl<T> ::std::ops::DerefMut for Radian<T> where T: Real
{
fn deref_mut<'a>(&'a mut self) -> &'a mut T
{
&mut self.value
}
}
binary_operator_impl!(Add::add, Real, Radian {value});
binary_operator_impl!(Sub::sub, Real, Radian {value});
binary_operator_impl!(Mul::mul, Real, Radian {value});
binary_operator_impl!(Div::div, Real, Radian {value});
binary_operator_impl!(Rem::rem, Real, Radian {value});
impl<T> ::std::ops::Neg for Radian<T>
where T: Neg<Output = T>
{
type Output = Radian<T>;
fn neg(self) -> Radian<T>
{
let mut radian: Radian<T>;
radian = self;
radian.value = radian.value.neg();
radian
}
}
binary_operator_assign_impl!(AddAssign::add_assign,
Real, Radian {value});
binary_operator_assign_impl!(SubAssign::sub_assign,
Real, Radian {value});
binary_operator_assign_impl!(MulAssign::mul_assign,
Real, Radian {value});
binary_operator_assign_impl!(DivAssign::div_assign,
Real, Radian {value});
binary_operator_assign_impl!(RemAssign::rem_assign,
Real, Radian {value});
impl ::std::convert::From<Radian<f32>> for f32
{
fn from(radian: Radian<f32>) -> f32
{
radian.value
}
}
impl ::std::convert::From<Radian<f64>> for f64
{
fn from(radian: Radian<f64>) -> f64
{
radian.value
}
}
impl<T> ::std::convert::From<T> for Radian<T> where T: Real
{
fn from(val: T) -> Radian<T>
{
Radian::new(val)
}
}
impl<T> ::std::convert::From<Degree<T>> for Radian<T>
where T: Real
{
fn from(degrees: Degree<T>) -> Radian<T>
{
let rads: T;
rads = *degrees * Constants::PI_DIVIDED_BY_180;
Radian::new(rads)
}
}
impl<T> ::std::fmt::Debug for Radian<T>
where T: Real
{
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result
{
::std::fmt::Display::fmt(self, f)
}
}
impl<T> ::std::fmt::Display for Radian<T>
where T: Real
{
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result
{
write!(f, "Radian({})", self.value)
}
}

421
src/trig/trig.rs Normal file
View File

@ -0,0 +1,421 @@
use ::binding::{CDouble, CFloat};
use ::real::Real;
use ::trig::radian::Radian;
/// The available trigonometric functions.
pub trait Trig: Real
{
/// Computes the cosine of this angle.
///
/// ```
/// use sigils::Radian;
/// use std::f64;
///
/// let x: Radian<f64> = Radian::from(2.0*f64::consts::PI);
///
/// let abs_difference = (x.cos() - 1.0).abs();
///
/// assert!(abs_difference < 1e-10);
/// ```
fn cos<T>(arg: T) -> Self
where T: Into<Radian<Self>>;
/// Computes the sine of this angle.
///
/// ```
/// use sigils::Radian;
/// use std::f64;
///
/// let x: Radian<f64> = Radian::from(f64::consts::PI/2.0);
///
/// let abs_difference = (x.sin() - 1.0).abs();
///
/// assert!(abs_difference < 1e-10);
/// ```
fn sin<T>(arg: T) -> Self
where T: Into<Radian<Self>>;
/// Computes the tangent of this angle.
///
/// ```
/// use sigils::Radian;
/// use std::f64;
///
/// let x: Radian<f64> = Radian::from(f64::consts::PI/4.0);
///
/// let abs_difference = (x.tan() - 1.0).abs();
///
/// assert!(abs_difference < 1e-14);
/// ```
fn tan<T>(arg: T) -> Self
where T: Into<Radian<Self>>;
/// Computes the arccosine of a number. Return value is in Degrees in
/// the range [0, pi] or NaN if the number is outside the range
/// [-1, 1].
///
///```
/// use sigils::{Constants, Radian, Real};
///
/// let f: Radian<f64>;
///
/// f = Radian::from(f64::PI / 4.0f64);
/// assert!(((f64::PI / 4.0f64) - *Radian::acos(f.cos())) < 1e-10);
///```
fn acos<T>(arg: Self) -> T
where T: From<Radian<Self>>;
/// Computes the arcsine of a number. Return value is in Degrees in
/// the range [-pi/2, pi/2] or NaN if the number is
/// outside the range [-1, 1].
///
///```
/// use sigils::{Constants, Radian, Real};
///
/// let f: Radian<f64>;
///
/// f = Radian::from(f64::PI / 4.0f64);
/// assert!(((f64::PI / 4.0f64) - *Radian::asin(f.sin())) < 1e-10);
///```
fn asin<T>(arg: Self) -> T
where T: From<Radian<Self>>;
/// Computes the arctangent of a number. Return value is in degrees in the
/// range [-pi/2, pi/2];
///
///```
/// use sigils::{Constants, Radian, Real};
///
/// let f: Radian<f64>;
///
/// f = Radian::from(f64::PI / 4.0f64);
/// assert!(((f64::PI / 4.0f64) - *Radian::atan(f.tan())) < 1e-10);
///```
fn atan<T>(arg: Self) -> T
where T: From<Radian<Self>>;
/// Computes the four quadrant arctangent of y and x.
fn atan2<T>(y: Self, x: Self) -> T
where T: From<Radian<Self>>;
/// Hyperbolic cosine function.
///
/// ```
/// use sigils::Radian;
/// use sigils::Constants;
///
/// let e32: f32 = Constants::E;
/// let x32: Radian<f32> = Radian::new(1.0f32);
/// let f_val32 = x32.cosh();
/// let g_val32 = (e32*e32 + 1.0f32)/(2.0f32*e32);
/// let abs_difference32 = (f_val32 - g_val32).abs();
///
/// let e64: f64 = Constants::E;
/// let x64: Radian<f64> = Radian::new(1.0f64);
/// let f_val64 = x64.cosh();
/// let g_val64 = (e64*e64 + 1.0f64)/(2.0f64*e64);
/// let abs_difference64 = (f_val64 - g_val64).abs();
///
/// // Solving cosh() at 1 gives this result
/// //assert!(abs_difference32 < 1.0e-10);
/// assert!(abs_difference64 < 1.0e-10);
/// ```
fn cosh(arg: Self) -> Self;
/// Hyperbolic sine function.
///
/// ```
/// use sigils::Radian;
/// use sigils::Constants;
///
/// let e32: f32 = Constants::E;
/// let x32: Radian<f32> = Radian::from(1.0f32);
///
/// let f_val32 = x32.sinh();
/// let g_val32 = (e32*e32 - 1.0f32)/(2.0f32*e32);
/// let abs_difference32 = (f_val32 - g_val32).abs();
///
/// let e64: f64 = Constants::E;
/// let x64: Radian<f64> = Radian::from(1.0f64);
///
/// let f_val64 = x64.sinh();
/// let g_val64 = (e64*e64 - 1.0f64)/(2.0f64*e64);
/// let abs_difference64 = (f_val64 - g_val64).abs();
///
/// // Solving sinh() at 1 gives `(e^2-1)/(2e)`
/// //assert!(abs_difference32 < 1e-10);
/// assert!(abs_difference64 < 1e-10);
/// ```
fn sinh(arg: Self) -> Self;
/// Hyperbolic tangent function.
///
/// ```
/// use sigils::Radian;
/// use sigils::Constants;
///
/// let e32: f32 = Constants::E;
/// let x32: Radian<f32> = Radian::from(1.0f32);
///
/// let f_val32 = x32.tanh();
/// let g_val32 = (1.0f32 - e32.powi(-2i32))/(1.0f32 + e32.powi(-2i32));
/// let abs_difference32 = (f_val32 - g_val32).abs();
///
/// let e64: f64 = Constants::E;
/// let x64: Radian<f64> = Radian::from(1.0f64);
///
/// let f_val64 = x64.tanh();
/// let g_val64 = (1.0f64 - e64.powi(-2i32))/(1.0f64 + e64.powi(-2i32));
/// let abs_difference64 = (f_val64 - g_val64).abs();
///
/// // Solving tanh() at 1 gives `(1 - e^(-2))/(1 + e^(-2))`
/// //assert!(abs_difference32 < 1.0e-10);
/// assert!(abs_difference64 < 1.0e-10);
/// ```
fn tanh(arg: Self) -> Self;
/// Inverse hyperbolic cosine function.
fn acosh(arg: Self) -> Self;
/// Inverse hyperbolic sine function.
fn asinh(arg: Self) -> Self;
/// Inverse hyperbolic tangent function.
fn atanh(arg: Self) -> Self;
}
impl Trig for f32
{
fn cos<T>(arg: T) -> Self
where T: Into<Radian<Self>>
{
unsafe
{
::pact::cosf(*arg.into() as CFloat) as Self
}
}
fn sin<T>(arg: T) -> Self
where T: Into<Radian<Self>>
{
unsafe
{
::pact::sinf(*arg.into() as CFloat) as Self
}
}
fn tan<T>(arg: T) -> Self
where T: Into<Radian<Self>>
{
unsafe
{
::pact::tanf(*arg.into() as CFloat) as Self
}
}
fn acos<T>(arg: Self) -> T
where T: From<Radian<Self>>
{
unsafe
{
Radian::new(::pact::acosf(arg as CFloat) as Self).into()
}
}
fn asin<T>(arg: Self) -> T
where T: From<Radian<Self>>
{
unsafe
{
Radian::new(::pact::asinf(arg as CFloat) as Self).into()
}
}
fn atan<T>(arg: Self) -> T
where T: From<Radian<Self>>
{
unsafe
{
Radian::new(::pact::atanf(arg as CFloat) as Self).into()
}
}
fn atan2<T>(y: Self, x: Self) -> T
where T: From<Radian<Self>>
{
unsafe
{
Radian::new(::pact::atan2f(y as CFloat, x as CFloat) as Self).into()
}
}
fn cosh(arg: Self) -> Self
{
unsafe
{
::pact::coshf(arg as CFloat) as Self
}
}
fn sinh(arg: Self) -> Self
{
unsafe
{
::pact::sinhf(arg as CFloat) as Self
}
}
fn tanh(arg: Self) -> Self
{
unsafe
{
::pact::tanhf(arg as CFloat) as Self
}
}
fn acosh(arg: Self) -> Self
{
unsafe
{
::pact::acoshf(arg as CFloat) as Self
}
}
fn asinh(arg: Self) -> Self
{
unsafe
{
::pact::asinhf(arg as CFloat) as Self
}
}
fn atanh(arg: Self) -> Self
{
unsafe
{
::pact::atanhf(arg as CFloat) as Self
}
}
}
impl Trig for f64
{
fn cos<T>(arg: T) -> Self
where T: Into<Radian<Self>>
{
unsafe
{
::pact::cos(*arg.into() as CDouble) as Self
}
}
fn sin<T>(arg: T) -> Self
where T: Into<Radian<Self>>
{
unsafe
{
::pact::sin(*arg.into() as CDouble) as Self
}
}
fn tan<T>(arg: T) -> Self
where T: Into<Radian<Self>>
{
unsafe
{
::pact::tan(*arg.into() as CDouble) as Self
}
}
fn acos<T>(arg: Self) -> T
where T: From<Radian<Self>>
{
unsafe
{
Radian::new(::pact::acos(arg as CDouble) as Self).into()
}
}
fn asin<T>(arg: Self) -> T
where T: From<Radian<Self>>
{
unsafe
{
Radian::new(::pact::asin(arg as CDouble) as Self).into()
}
}
fn atan<T>(arg: Self) -> T
where T: From<Radian<Self>>
{
unsafe
{
Radian::new(::pact::atan(arg as CDouble) as Self).into()
}
}
fn atan2<T>(y: Self, x: Self) -> T
where T: From<Radian<Self>>
{
unsafe
{
Radian::new(::pact::atan2(y as CDouble, x as CDouble) as Self).into()
}
}
fn cosh(arg: Self) -> Self
{
unsafe
{
::pact::cosh(arg as CDouble) as Self
}
}
fn sinh(arg: Self) -> Self
{
unsafe
{
::pact::sinh(arg as CDouble) as Self
}
}
fn tanh(arg: Self) -> Self
{
unsafe
{
::pact::tanh(arg as CDouble) as Self
}
}
fn acosh(arg: Self) -> Self
{
unsafe
{
::pact::acosh(arg as CDouble) as Self
}
}
fn asinh(arg: Self) -> Self
{
unsafe
{
::pact::asinh(arg as CDouble) as Self
}
}
fn atanh(arg: Self) -> Self
{
unsafe
{
::pact::atanh(arg as CDouble) as Self
}
}
}