diff --git a/types/src/number/integer.rs b/types/src/number/integer.rs index 2515b39..6a9ed92 100644 --- a/types/src/number/integer.rs +++ b/types/src/number/integer.rs @@ -11,6 +11,8 @@ pub struct Integer(pub Int); impl Number for Integer { fn convert_down(&self) -> Option> { 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()); + } } diff --git a/types/src/number/mod.rs b/types/src/number/mod.rs index f09d06a..e27eb7d 100644 --- a/types/src/number/mod.rs +++ b/types/src/number/mod.rs @@ -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>; + + /// 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 { diff --git a/types/src/number/rational.rs b/types/src/number/rational.rs index b105807..4d8d82f 100644 --- a/types/src/number/rational.rs +++ b/types/src/number/rational.rs @@ -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()); + } }