Implement wrap method

This commit is contained in:
Brendan Zabarauskas 2012-11-29 11:16:28 +10:00
parent 4ea08253a7
commit 26a22e0cc7
2 changed files with 20 additions and 2 deletions

View file

@ -37,7 +37,9 @@ pub impl<T:Copy Num NumCast> Radians<T>: Angle<T> {
#[inline(always)] pure fn to_radians() -> Radians<T> { self }
#[inline(always)] pure fn to_degrees() -> Degrees<T> { Degrees(*self * cast(180.0 / pi)) }
#[inline(always)] pure fn wrap() -> Radians<T> { fail(~"Radians.wrap() not yet implemented") }
#[inline(always)] pure fn wrap() -> Radians<T> {
self % cast(2.0 * pi) // TODO: keep in the domain of 0 to two_pi
}
}
pub impl<T:Copy Num> Radians<T>: Add<Radians<T>, Radians<T>> {
@ -87,7 +89,9 @@ pub impl<T:Copy Num NumCast> Degrees<T>: Angle<T> {
#[inline(always)] pure fn to_radians() -> Radians<T> { Radians(*self * cast(pi / 180.0)) }
#[inline(always)] pure fn to_degrees() -> Degrees<T> { self }
#[inline(always)] pure fn wrap() -> Degrees<T> { fail(~"Degrees.wrap() not yet implemented") }
#[inline(always)] pure fn wrap() -> Degrees<T> {
self % cast(360) // TODO: keep in the domain of 0 to 360
}
}
pub impl<T:Copy Num> Degrees<T>: Add<Degrees<T>, Degrees<T>> {

View file

@ -18,6 +18,13 @@ fn test_radians() {
assert *Radians(5.0 * pi).to_radians() == *Radians(5.0 * pi);
assert *Radians(-pi).to_radians() == *Radians(-pi);
assert *Radians(pi).wrap() == *Radians(pi);
assert *Radians(3.0 * pi).wrap() == *Radians(pi);
assert *Radians(2.0 * pi).wrap() == *Radians(0.0);
assert *Radians(-pi).wrap() == *Radians(-pi);
assert *Radians(-3.0 * pi).wrap() == *Radians(-pi);
assert *Radians(-2.0 * pi).wrap() == *Radians(0.0);
assert *(Radians(pi) + Radians(pi)) == *Radians(2.0 * pi);
assert *(Radians(2.0 * pi) - Radians(pi)) == *Radians(pi);
assert *(Radians(pi) * 2.0) == *Radians(2.0 * pi);
@ -40,6 +47,13 @@ fn test_degrees() {
assert *Degrees(900.0).to_degrees() == *Degrees(900.0);
assert *Degrees(-180.0).to_degrees() == *Degrees(-180.0);
assert *Degrees(90.0).wrap() == *Degrees(90.0);
assert *Degrees(450.0).wrap() == *Degrees(90.0);
assert *Degrees(360.0).wrap() == *Degrees(0.0);
assert *Degrees(-90.0).wrap() == *Degrees(-90.0);
assert *Degrees(-450.0).wrap() == *Degrees(-90.0);
assert *Degrees(-360.0).wrap() == *Degrees(0.0);
assert *(Degrees(180.0) + Degrees(180.0)) == *Degrees(360.0);
assert *(Degrees(360.0) - Degrees(180.0)) == *Degrees(180.0);
assert *(Degrees(360.0) * 2.0) == *Degrees(720.0);