Make Number its own struct with real/imag elements
This commit is contained in:
parent
27c4a80631
commit
2b2d1029f3
1 changed files with 29 additions and 17 deletions
|
@ -23,29 +23,41 @@ use super::value::*;
|
||||||
type Int = i64;
|
type Int = i64;
|
||||||
type Flt = f64;
|
type Flt = f64;
|
||||||
|
|
||||||
trait Number: Debug + IsBool + IsChar + IsNumber + Value { }
|
// TODO: Implement PartialEq myself cause there are some weird nuances to comparing numbers.
|
||||||
|
#[derive(Debug, PartialEq)]
|
||||||
trait IsExact {
|
pub struct Number {
|
||||||
/// Should return `true` if this Number is represented exactly. This should be an inverse of
|
real: Real,
|
||||||
/// `is_inexact()`.
|
imag: Option<Real>,
|
||||||
fn is_exact(&self) -> bool { false }
|
exact: bool,
|
||||||
|
|
||||||
/// Should return `true` if this Number is not represented exactly. This should be an inverse
|
|
||||||
/// of `is_exact()`.
|
|
||||||
fn is_inexact(&self) -> bool { !self.is_exact() }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Value for Box<Number> {
|
impl Number {
|
||||||
fn as_value(&self) -> &Value { self.deref().as_value() }
|
fn new(real: Real, imag: Option<Real>, exact: bool) -> Number {
|
||||||
|
Number {
|
||||||
|
real: real.reduce(),
|
||||||
|
imag: imag.map(|n| n.reduce()),
|
||||||
|
exact: exact,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn from_int(value: Int, exact: bool) -> Number {
|
||||||
|
Number::new(Real::Integer(value), None, exact)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn is_exact(&self) -> bool { self.exact }
|
||||||
}
|
}
|
||||||
|
|
||||||
impl IsBool for Box<Number> { }
|
impl Value for Number {
|
||||||
impl IsChar for Box<Number> { }
|
fn as_value(&self) -> &Value { self }
|
||||||
impl IsNumber for Box<Number> { }
|
}
|
||||||
|
|
||||||
impl ValueEq for Box<Number> {
|
impl ValueEq for Number {
|
||||||
fn eq(&self, other: &Value) -> bool {
|
fn eq(&self, other: &Value) -> bool {
|
||||||
self.deref().eq(other)
|
other.as_any().downcast_ref::<Self>().map_or(false, |x| x == self)
|
||||||
}
|
}
|
||||||
fn as_any(&self) -> &Any { self }
|
fn as_any(&self) -> &Any { self }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl IsBool for Number { }
|
||||||
|
impl IsChar for Number { }
|
||||||
|
impl IsNumber for Number { }
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue