Move Exact to types::number
This commit is contained in:
parent
d783e561da
commit
3ffb694ce1
2 changed files with 32 additions and 19 deletions
|
@ -2,7 +2,7 @@
|
||||||
* Eryn Wells <eryn@erynwells.me>
|
* Eryn Wells <eryn@erynwells.me>
|
||||||
*/
|
*/
|
||||||
|
|
||||||
use sibiltypes::Number;
|
use sibiltypes::number::{Number, Exact};
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum Radix { Bin, Oct, Dec, Hex }
|
pub enum Radix { Bin, Oct, Dec, Hex }
|
||||||
|
@ -10,12 +10,9 @@ pub enum Radix { Bin, Oct, Dec, Hex }
|
||||||
#[derive(PartialEq, Debug)]
|
#[derive(PartialEq, Debug)]
|
||||||
pub enum Sign { Pos, Neg }
|
pub enum Sign { Pos, Neg }
|
||||||
|
|
||||||
#[derive(PartialEq, Debug)]
|
|
||||||
pub enum Exactness { Exact, Inexact }
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct NumberBuilder {
|
pub struct NumberBuilder {
|
||||||
exact: Option<Exactness>,
|
exact: Exact,
|
||||||
radix: Radix,
|
radix: Radix,
|
||||||
sign: Sign,
|
sign: Sign,
|
||||||
value: f64,
|
value: f64,
|
||||||
|
@ -25,7 +22,7 @@ pub struct NumberBuilder {
|
||||||
impl NumberBuilder {
|
impl NumberBuilder {
|
||||||
pub fn new() -> NumberBuilder {
|
pub fn new() -> NumberBuilder {
|
||||||
NumberBuilder {
|
NumberBuilder {
|
||||||
exact: None,
|
exact: Exact::Yes,
|
||||||
radix: Radix::Dec,
|
radix: Radix::Dec,
|
||||||
sign: Sign::Pos,
|
sign: Sign::Pos,
|
||||||
value: 0.0,
|
value: 0.0,
|
||||||
|
@ -33,8 +30,8 @@ impl NumberBuilder {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn exact<'a>(&'a mut self, ex: Exactness) -> &'a mut NumberBuilder {
|
pub fn exact<'a>(&'a mut self, ex: Exact) -> &'a mut NumberBuilder {
|
||||||
self.exact = Some(ex);
|
self.exact = ex;
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -79,8 +76,7 @@ impl NumberBuilder {
|
||||||
value
|
value
|
||||||
};
|
};
|
||||||
// TODO: Use an integer if we can.
|
// TODO: Use an integer if we can.
|
||||||
let exact = self.point == 0 || self.exact == Some(Exactness::Exact);
|
Number::from_float(value, self.exact)
|
||||||
Number::from_float(value, exact)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn radix_value(&self) -> u32 {
|
pub fn radix_value(&self) -> u32 {
|
||||||
|
|
|
@ -17,16 +17,28 @@ pub use self::real::Real;
|
||||||
type Int = i64;
|
type Int = i64;
|
||||||
type Flt = f64;
|
type Flt = f64;
|
||||||
|
|
||||||
|
#[derive(Debug, Eq, PartialEq)]
|
||||||
|
pub enum Exact { Yes, No }
|
||||||
|
|
||||||
|
impl fmt::Display for Exact {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
|
write!(f, "{}", match *self {
|
||||||
|
Exact::Yes => "#e",
|
||||||
|
Exact::No => "#i",
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: Implement PartialEq myself cause there are some weird nuances to comparing numbers.
|
// TODO: Implement PartialEq myself cause there are some weird nuances to comparing numbers.
|
||||||
#[derive(Debug, PartialEq)]
|
#[derive(Debug, PartialEq)]
|
||||||
pub struct Number {
|
pub struct Number {
|
||||||
real: Real,
|
real: Real,
|
||||||
imag: Option<Real>,
|
imag: Option<Real>,
|
||||||
exact: bool,
|
exact: Exact,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Number {
|
impl Number {
|
||||||
fn new(real: Real, imag: Option<Real>, exact: bool) -> Number {
|
fn new(real: Real, imag: Option<Real>, exact: Exact) -> Number {
|
||||||
Number {
|
Number {
|
||||||
real: real.reduce(),
|
real: real.reduce(),
|
||||||
imag: imag.map(|n| n.reduce()),
|
imag: imag.map(|n| n.reduce()),
|
||||||
|
@ -34,12 +46,12 @@ impl Number {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn from_int(value: Int, exact: bool) -> Number {
|
pub fn from_int(value: Int, exact: Exact) -> Number {
|
||||||
Number::new(Real::Integer(value), None, exact)
|
Number::new(Real::Integer(value), None, exact)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn from_quotient(p: Int, q: Int, exact: bool) -> Number {
|
pub fn from_quotient(p: Int, q: Int, exact: Exact) -> Number {
|
||||||
let real = if exact {
|
let real = if exact == Exact::Yes {
|
||||||
// Make an exact rational an integer if possible.
|
// Make an exact rational an integer if possible.
|
||||||
Real::Rational(p, q).demote()
|
Real::Rational(p, q).demote()
|
||||||
}
|
}
|
||||||
|
@ -50,8 +62,8 @@ impl Number {
|
||||||
Number::new(real, None, exact)
|
Number::new(real, None, exact)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn from_float(value: Flt, exact: bool) -> Number {
|
pub fn from_float(value: Flt, exact: Exact) -> Number {
|
||||||
let real = if exact {
|
let real = if exact == Exact::Yes {
|
||||||
// Attempt to demote irrationals.
|
// Attempt to demote irrationals.
|
||||||
Real::Irrational(value).demote()
|
Real::Irrational(value).demote()
|
||||||
}
|
}
|
||||||
|
@ -61,7 +73,12 @@ impl Number {
|
||||||
Number::new(real, None, exact)
|
Number::new(real, None, exact)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn is_exact(&self) -> bool { self.exact }
|
pub fn is_exact(&self) -> bool {
|
||||||
|
match self.exact {
|
||||||
|
Exact::Yes => true,
|
||||||
|
Exact::No => false,
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl fmt::Display for Number {
|
impl fmt::Display for Number {
|
||||||
|
@ -85,6 +102,6 @@ mod tests {
|
||||||
#[test]
|
#[test]
|
||||||
fn exact_irrationals_are_reduced() {
|
fn exact_irrationals_are_reduced() {
|
||||||
let real = Real::Rational(3, 2);
|
let real = Real::Rational(3, 2);
|
||||||
assert_eq!(Number::from_float(1.5, true), Number::new(real, None, true));
|
assert_eq!(Number::from_float(1.5, Exact::Yes), Number::new(real, None, Exact::Yes));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue