Implement irrational addition
This commit is contained in:
parent
1b816e19df
commit
a60ec25c8e
1 changed files with 14 additions and 8 deletions
|
@ -3,7 +3,7 @@
|
|||
*/
|
||||
|
||||
use std::ops::Add;
|
||||
use super::{Int, Flt, Real};
|
||||
use super::Real;
|
||||
|
||||
impl Add for Real {
|
||||
type Output = Real;
|
||||
|
@ -11,6 +11,7 @@ impl Add for Real {
|
|||
fn add(self, other: Real) -> Real {
|
||||
match (self, other) {
|
||||
(Real::Integer(v), Real::Integer(ov)) => Real::Integer(v + ov),
|
||||
(Real::Irrational(v), Real::Irrational(ov)) => Real::Irrational(v + ov),
|
||||
// TODO: The rest.
|
||||
_ => Real::Integer(0)
|
||||
}
|
||||
|
@ -19,18 +20,23 @@ impl Add for Real {
|
|||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use number::Real;
|
||||
|
||||
#[test]
|
||||
fn integer_addition_works() {
|
||||
let result = Real::Integer(3) + Real::Integer(5);
|
||||
assert_eq!(result, Real::Integer(8));
|
||||
fn integer_addition() {
|
||||
let r = Real::Integer(3) + Real::Integer(5);
|
||||
assert_eq!(r, Real::Integer(8));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rational_addition_works() {
|
||||
let result = Real::Rational(1, 4) + Real::Rational(1, 4);
|
||||
assert_eq!(result, Real::Rational(1, 2));
|
||||
fn rational_addition() {
|
||||
let r = Real::Rational(1, 4) + Real::Rational(1, 4);
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue