Clean up the enum matching for Reals
This commit is contained in:
parent
6ca01cbb5a
commit
e503e6da7c
2 changed files with 8 additions and 39 deletions
|
@ -7,17 +7,8 @@ impl Add for Real {
|
||||||
type Output = Real;
|
type Output = Real;
|
||||||
|
|
||||||
fn add(self, other: Real) -> Real {
|
fn add(self, other: Real) -> Real {
|
||||||
match other {
|
match (self, other) {
|
||||||
Real::Integer(v) => self.add_integer(v),
|
(Real::Integer(v), Real::Integer(ov)) => Real::Integer(v + ov),
|
||||||
_ => Real::Integer(0)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Real {
|
|
||||||
fn add_integer(self, v_other: Int) -> Real {
|
|
||||||
match self {
|
|
||||||
Real::Integer(v) => Real::Integer(v + v_other),
|
|
||||||
_ => Real::Integer(0)
|
_ => Real::Integer(0)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,11 +3,10 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
use std::any::Any;
|
use std::any::Any;
|
||||||
use std::ops::{Add, Sub, Mul, Div};
|
|
||||||
use super::*;
|
use super::*;
|
||||||
use value::*;
|
use value::*;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Clone, Copy, Debug)]
|
||||||
pub enum Real {
|
pub enum Real {
|
||||||
Integer(Int),
|
Integer(Int),
|
||||||
Rational(Int, Int),
|
Rational(Int, Int),
|
||||||
|
@ -16,32 +15,11 @@ pub enum Real {
|
||||||
|
|
||||||
impl PartialEq for Real {
|
impl PartialEq for Real {
|
||||||
fn eq(&self, other: &Real) -> bool {
|
fn eq(&self, other: &Real) -> bool {
|
||||||
match *other {
|
// TODO: Make comparing different variants possible.
|
||||||
Real::Integer(v) => self.eq_integer(v),
|
match (self, other) {
|
||||||
Real::Rational(p, q) => self.eq_rational(p, q),
|
(&Real::Integer(v), &Real::Integer(ov)) => v == ov,
|
||||||
Real::Irrational(v) => self.eq_irrational(v)
|
(&Real::Rational(p, q), &Real::Rational(op, oq)) => p == op && q == oq,
|
||||||
}
|
(&Real::Irrational(v), &Real::Irrational(ov)) => v == ov,
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Real {
|
|
||||||
fn eq_integer(&self, v_other: Int) -> bool {
|
|
||||||
match *self {
|
|
||||||
Real::Integer(v) => v == v_other,
|
|
||||||
_ => false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn eq_rational(&self, p_other: Int, q_other: Int) -> bool {
|
|
||||||
match *self {
|
|
||||||
Real::Rational(p, q) => p == p_other && q == q_other,
|
|
||||||
_ => false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn eq_irrational(&self, v_other: Flt) -> bool {
|
|
||||||
match *self {
|
|
||||||
Real::Irrational(v) => v == v_other,
|
|
||||||
_ => false
|
_ => false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue