Integer and Rational correctly report exactness
This commit is contained in:
parent
6d9599551b
commit
465b188d48
3 changed files with 25 additions and 0 deletions
|
@ -11,6 +11,8 @@ pub struct Integer(pub Int);
|
||||||
|
|
||||||
impl Number for Integer {
|
impl Number for Integer {
|
||||||
fn convert_down(&self) -> Option<Box<Number>> { None }
|
fn convert_down(&self) -> Option<Box<Number>> { None }
|
||||||
|
|
||||||
|
fn is_exact(&self) -> bool { true }
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Value for Integer {
|
impl Value for Integer {
|
||||||
|
@ -35,6 +37,7 @@ impl ValueEq for Integer {
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::Integer;
|
use super::Integer;
|
||||||
|
use number::*;
|
||||||
use value::*;
|
use value::*;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -52,4 +55,10 @@ mod tests {
|
||||||
assert_eq!(Integer(6).is_char(), false);
|
assert_eq!(Integer(6).is_char(), false);
|
||||||
assert_eq!(Integer(6).is_bool(), false);
|
assert_eq!(Integer(6).is_bool(), false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn integers_are_exact() {
|
||||||
|
assert!(Integer(4).is_exact());
|
||||||
|
assert!(!Integer(4).is_inexact());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,6 +24,14 @@ type Flt = f64;
|
||||||
trait Number: Debug + IsBool + IsChar + IsNumber + Value {
|
trait Number: Debug + IsBool + IsChar + IsNumber + Value {
|
||||||
/// Convert a Number to the next lowest type in Scheme's number pyramid, if possible.
|
/// Convert a Number to the next lowest type in Scheme's number pyramid, if possible.
|
||||||
fn convert_down(&self) -> Option<Box<Number>>;
|
fn convert_down(&self) -> Option<Box<Number>>;
|
||||||
|
|
||||||
|
/// 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() }
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Value for Box<Number> {
|
impl Value for Box<Number> {
|
||||||
|
|
|
@ -18,6 +18,8 @@ impl Number for Rational {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn is_exact(&self) -> bool { true }
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Value for Rational {
|
impl Value for Rational {
|
||||||
|
@ -77,4 +79,10 @@ mod tests {
|
||||||
let rational_as_integer = Rational(3, 2).convert_down();
|
let rational_as_integer = Rational(3, 2).convert_down();
|
||||||
assert!(rational_as_integer.is_none());
|
assert!(rational_as_integer.is_none());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn rationals_are_exact() {
|
||||||
|
assert!(Rational(4, 2).is_exact());
|
||||||
|
assert!(!Rational(4, 2).is_inexact());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue