Partial implementation of Rational addition
This commit is contained in:
parent
403536ec4c
commit
5e71947128
1 changed files with 11 additions and 1 deletions
|
@ -3,7 +3,8 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
use std::ops::Add;
|
use std::ops::Add;
|
||||||
use super::Real;
|
use number::Real;
|
||||||
|
use number::math::*;
|
||||||
|
|
||||||
impl Add for Real {
|
impl Add for Real {
|
||||||
type Output = Real;
|
type Output = Real;
|
||||||
|
@ -11,6 +12,15 @@ 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::Rational(p, q), Real::Rational(op, oq)) => {
|
||||||
|
if q == oq {
|
||||||
|
Real::Rational(p + op, q)
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
let lcm = q.lcm(oq);
|
||||||
|
Real::Rational(1, 1)
|
||||||
|
}
|
||||||
|
},
|
||||||
(Real::Irrational(v), Real::Irrational(ov)) => Real::Irrational(v + ov),
|
(Real::Irrational(v), Real::Irrational(ov)) => Real::Irrational(v + ov),
|
||||||
// TODO: The rest.
|
// TODO: The rest.
|
||||||
_ => Real::Integer(0)
|
_ => Real::Integer(0)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue