The trigonometric functions don't have to be methods!

This commit is contained in:
Brendan Zabarauskas 2013-09-04 15:52:44 +10:00
parent bb17d95abe
commit 6d16999d29

View file

@ -86,18 +86,8 @@ pub trait Angle
#[inline] fn mul_self_s(&mut self, s: S) { *self.mut_s() = *self.s() * s } #[inline] fn mul_self_s(&mut self, s: S) { *self.mut_s() = *self.s() * s }
#[inline] fn div_self_s(&mut self, s: S) { *self.mut_s() = *self.s() / s } #[inline] fn div_self_s(&mut self, s: S) { *self.mut_s() = *self.s() / s }
#[inline] fn rem_self_s(&mut self, s: S) { *self.mut_s() = *self.s() % s } #[inline] fn rem_self_s(&mut self, s: S) { *self.mut_s() = *self.s() % s }
#[inline] fn sin(&self) -> S { self.s().sin() }
#[inline] fn cos(&self) -> S { self.s().cos() }
#[inline] fn tan(&self) -> S { self.s().tan() }
#[inline] fn sin_cos(&self) -> (S, S) { self.s().sin_cos() }
} }
#[inline] pub fn sin<S: Clone + Float, A: Angle<S>>(theta: A) -> S { theta.sin() }
#[inline] pub fn cos<S: Clone + Float, A: Angle<S>>(theta: A) -> S { theta.cos() }
#[inline] pub fn tan<S: Clone + Float, A: Angle<S>>(theta: A) -> S { theta.tan() }
#[inline] pub fn sin_cos<S: Clone + Float, A: Angle<S>>(theta: A) -> (S, S) { theta.sin_cos() }
impl<S: Clone + Float> Angle<S> for Rad<S> { impl<S: Clone + Float> Angle<S> for Rad<S> {
#[inline] fn from<A: Angle<S>>(theta: A) -> Rad<S> { theta.to_rad() } #[inline] fn from<A: Angle<S>>(theta: A) -> Rad<S> { theta.to_rad() }
} }
@ -106,21 +96,15 @@ impl<S: Clone + Float> Angle<S> for Deg<S> {
#[inline] fn from<A: Angle<S>>(theta: A) -> Deg<S> { theta.to_deg() } #[inline] fn from<A: Angle<S>>(theta: A) -> Deg<S> { theta.to_deg() }
} }
pub trait ScalarTrig: Clone + Float { #[inline] pub fn sin<S: Clone + Float, A: Angle<S>>(theta: A) -> S { theta.to_rad().s.sin() }
// These need underscores so that they don't conflict with the methods #[inline] pub fn cos<S: Clone + Float, A: Angle<S>>(theta: A) -> S { theta.to_rad().s.cos() }
// defined in the `std::num::Trigonometric` trait. #[inline] pub fn tan<S: Clone + Float, A: Angle<S>>(theta: A) -> S { theta.to_rad().s.tan() }
#[inline] fn asin_<A: Angle<Self>>(&self) -> A { Angle::from(rad(self.asin())) } #[inline] pub fn sin_cos<S: Clone + Float, A: Angle<S>>(theta: A) -> (S, S) { theta.to_rad().s.sin_cos() }
#[inline] fn acos_<A: Angle<Self>>(&self) -> A { Angle::from(rad(self.acos())) }
#[inline] fn atan_<A: Angle<Self>>(&self) -> A { Angle::from(rad(self.atan())) }
#[inline] fn atan2_<A: Angle<Self>>(&self, other: &Self) -> A { Angle::from(rad(self.atan2(other))) }
}
#[inline] pub fn asin<S: Clone + ScalarTrig, A: Angle<S>>(s: S) -> A { s.asin_() } #[inline] pub fn asin<S: Clone + Float, A: Angle<S>>(s: S) -> A { Angle::from(rad(s.asin())) }
#[inline] pub fn acos<S: Clone + ScalarTrig, A: Angle<S>>(s: S) -> A { s.acos_() } #[inline] pub fn acos<S: Clone + Float, A: Angle<S>>(s: S) -> A { Angle::from(rad(s.acos())) }
#[inline] pub fn atan<S: Clone + ScalarTrig, A: Angle<S>>(s: S) -> A { s.atan_() } #[inline] pub fn atan<S: Clone + Float, A: Angle<S>>(s: S) -> A { Angle::from(rad(s.atan())) }
#[inline] pub fn atan2<S: Clone + ScalarTrig, A: Angle<S>>(a: S, b: S) -> A { a.atan2_(&b) } #[inline] pub fn atan2<S: Clone + Float, A: Angle<S>>(a: S, b: S) -> A { Angle::from(rad(a.atan2(&b))) }
impl<S: Clone + Float> ScalarTrig for S;
impl<S: Clone + Float> ToStr for Rad<S> { fn to_str(&self) -> ~str { fmt!("%? rad", self.s) } } impl<S: Clone + Float> ToStr for Rad<S> { fn to_str(&self) -> ~str { fmt!("%? rad", self.s) } }
impl<S: Clone + Float> ToStr for Deg<S> { fn to_str(&self) -> ~str { fmt!("%?°", self.s) } } impl<S: Clone + Float> ToStr for Deg<S> { fn to_str(&self) -> ~str { fmt!("%?°", self.s) } }