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 Flt = f64;
|
||||
|
||||
trait Number: Debug + IsBool + IsChar + IsNumber + Value { }
|
||||
|
||||
trait IsExact {
|
||||
/// Should return `true` if this Number is represented exactly. This should be an inverse of
|
||||
/// `is_inexact()`.
|
||||
fn is_exact(&self) -> bool { false }
|
||||
|
||||
/// 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() }
|
||||
// TODO: Implement PartialEq myself cause there are some weird nuances to comparing numbers.
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub struct Number {
|
||||
real: Real,
|
||||
imag: Option<Real>,
|
||||
exact: bool,
|
||||
}
|
||||
|
||||
impl Value for Box<Number> {
|
||||
fn as_value(&self) -> &Value { self.deref().as_value() }
|
||||
impl Number {
|
||||
fn new(real: Real, imag: Option<Real>, exact: bool) -> Number {
|
||||
Number {
|
||||
real: real.reduce(),
|
||||
imag: imag.map(|n| n.reduce()),
|
||||
exact: exact,
|
||||
}
|
||||
}
|
||||
|
||||
impl IsBool for Box<Number> { }
|
||||
impl IsChar for Box<Number> { }
|
||||
impl IsNumber for Box<Number> { }
|
||||
pub fn from_int(value: Int, exact: bool) -> Number {
|
||||
Number::new(Real::Integer(value), None, exact)
|
||||
}
|
||||
|
||||
impl ValueEq for Box<Number> {
|
||||
pub fn is_exact(&self) -> bool { self.exact }
|
||||
}
|
||||
|
||||
impl Value for Number {
|
||||
fn as_value(&self) -> &Value { self }
|
||||
}
|
||||
|
||||
impl ValueEq for Number {
|
||||
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 }
|
||||
}
|
||||
|
||||
impl IsBool for Number { }
|
||||
impl IsChar for Number { }
|
||||
impl IsNumber for Number { }
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue