From 038bbc5fdeff3cb637fe761a061ee92f7e1e1696 Mon Sep 17 00:00:00 2001 From: Mark Date: Sun, 2 Apr 2023 08:27:21 -0700 Subject: [PATCH] Fixed minus printing --- src/quantity/mod.rs | 2 +- src/quantity/quantity.rs | 14 ++++++++++++++ src/quantity/rationalq.rs | 3 +++ src/tokens.rs | 22 ++++++++++++++++++---- 4 files changed, 36 insertions(+), 5 deletions(-) diff --git a/src/quantity/mod.rs b/src/quantity/mod.rs index f8c3f77..86faff0 100644 --- a/src/quantity/mod.rs +++ b/src/quantity/mod.rs @@ -3,5 +3,5 @@ mod rationalq; pub mod quantity; pub use crate::quantity::quantity::Quantity; -const FLOAT_PRECISION: u32 = 2048; +const FLOAT_PRECISION: u32 = 1024; const PRINT_LEN: usize = 5; // How many significant digits we will show in output \ No newline at end of file diff --git a/src/quantity/quantity.rs b/src/quantity/quantity.rs index 7fbb62f..eb9b45f 100644 --- a/src/quantity/quantity.rs +++ b/src/quantity/quantity.rs @@ -246,6 +246,20 @@ impl Quantity { Quantity::Rational { .. } => {panic!()} } } + + pub fn is_negative(&self) -> bool { + match self { + Quantity::Float { v } => {v.is_sign_negative() && v.is_normal()}, + Quantity::Rational { v } => {v.is_negative()} + } + } + + pub fn is_positive(&self) -> bool { + match self { + Quantity::Float { v } => {v.is_sign_positive() && v.is_normal()}, + Quantity::Rational { v } => {v.is_positive()} + } + } } impl Neg for Quantity where { diff --git a/src/quantity/rationalq.rs b/src/quantity/rationalq.rs index a4c47a0..9f6ea23 100644 --- a/src/quantity/rationalq.rs +++ b/src/quantity/rationalq.rs @@ -86,6 +86,9 @@ impl RationalQ { } + pub fn is_negative(&self) -> bool { self.val.clone().signum() == -1 } + pub fn is_positive(&self) -> bool { self.val.clone().signum() == 1 } + pub fn exp(&self) -> Quantity {float!(self.to_float().exp())} pub fn abs(&self) -> Quantity {rational!(self.val.clone().abs())} diff --git a/src/tokens.rs b/src/tokens.rs index f97a482..8d1c784 100644 --- a/src/tokens.rs +++ b/src/tokens.rs @@ -300,18 +300,26 @@ impl Operator { let mut b = &args[1]; let mut sub = false; + let tmp; + if let Token::Operator(o,ar) = b { if let Operator::Negative = o { sub = true; b = &ar[0]; } + } else if let Token::Number(q) = b { + if q.is_negative() { + sub = true; + tmp = Token::Number(-q.clone()); + b = &tmp; + } } let (b, sub) = (b, sub); if sub { - return format!("{} - {}", self.add_parens_to_arg(a), self.add_parens_to_arg(b)); + return format!("{} - {}", self.add_parens_to_arg(a), self.add_parens_to_arg(&b)); } else { - return format!("{} + {}", self.add_parens_to_arg(a), self.add_parens_to_arg(b)); + return format!("{} + {}", self.add_parens_to_arg(a), self.add_parens_to_arg(&b)); } }, @@ -435,11 +443,17 @@ impl Operator { if args.len() != 2 { panic!() } let a = args.pop_front().unwrap(); let b = args.pop_front().unwrap(); - let b = Token::Operator(Operator::Negative, VecDeque::from(vec!(b))); + + let b_new; + if let Token::Number(q) = b { + b_new = Token::Number(-q); + } else { + b_new = Token::Operator(Operator::Negative, VecDeque::from(vec!(b))); + } Token::Operator( Operator::Add, - VecDeque::from(vec!(a,b)) + VecDeque::from(vec!(a,b_new)) ) },