Implement irrational addition

This commit is contained in:
Eryn Wells 2017-04-12 09:36:18 -07:00
parent 1b816e19df
commit a60ec25c8e

View file

@ -3,7 +3,7 @@
*/ */
use std::ops::Add; use std::ops::Add;
use super::{Int, Flt, Real}; use super::Real;
impl Add for Real { impl Add for Real {
type Output = Real; type Output = Real;
@ -11,6 +11,7 @@ impl Add for Real {
fn add(self, other: Real) -> Real { fn add(self, other: Real) -> Real {
match (self, other) { match (self, other) {
(Real::Integer(v), Real::Integer(ov)) => Real::Integer(v + ov), (Real::Integer(v), Real::Integer(ov)) => Real::Integer(v + ov),
(Real::Irrational(v), Real::Irrational(ov)) => Real::Irrational(v + ov),
// TODO: The rest. // TODO: The rest.
_ => Real::Integer(0) _ => Real::Integer(0)
} }
@ -19,18 +20,23 @@ impl Add for Real {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*;
use number::Real; use number::Real;
#[test] #[test]
fn integer_addition_works() { fn integer_addition() {
let result = Real::Integer(3) + Real::Integer(5); let r = Real::Integer(3) + Real::Integer(5);
assert_eq!(result, Real::Integer(8)); assert_eq!(r, Real::Integer(8));
} }
#[test] #[test]
fn rational_addition_works() { fn rational_addition() {
let result = Real::Rational(1, 4) + Real::Rational(1, 4); let r = Real::Rational(1, 4) + Real::Rational(1, 4);
assert_eq!(result, Real::Rational(1, 2)); assert_eq!(r, Real::Rational(1, 2));
}
#[test]
fn irrational_addition() {
let r = Real::Irrational(3.2) + Real::Irrational(3.2);
assert_eq!(r, Real::Irrational(6.4));
} }
} }