Integer and Rational correctly report exactness

This commit is contained in:
Eryn Wells 2017-04-09 17:20:24 -07:00
parent 6d9599551b
commit 465b188d48
3 changed files with 25 additions and 0 deletions

View file

@ -11,6 +11,8 @@ pub struct Integer(pub Int);
impl Number for Integer {
fn convert_down(&self) -> Option<Box<Number>> { None }
fn is_exact(&self) -> bool { true }
}
impl Value for Integer {
@ -35,6 +37,7 @@ impl ValueEq for Integer {
#[cfg(test)]
mod tests {
use super::Integer;
use number::*;
use value::*;
#[test]
@ -52,4 +55,10 @@ mod tests {
assert_eq!(Integer(6).is_char(), false);
assert_eq!(Integer(6).is_bool(), false);
}
#[test]
fn integers_are_exact() {
assert!(Integer(4).is_exact());
assert!(!Integer(4).is_inexact());
}
}

View file

@ -24,6 +24,14 @@ type Flt = f64;
trait Number: Debug + IsBool + IsChar + IsNumber + Value {
/// Convert a Number to the next lowest type in Scheme's number pyramid, if possible.
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> {

View file

@ -18,6 +18,8 @@ impl Number for Rational {
None
}
}
fn is_exact(&self) -> bool { true }
}
impl Value for Rational {
@ -77,4 +79,10 @@ mod tests {
let rational_as_integer = Rational(3, 2).convert_down();
assert!(rational_as_integer.is_none());
}
#[test]
fn rationals_are_exact() {
assert!(Rational(4, 2).is_exact());
assert!(!Rational(4, 2).is_inexact());
}
}