Fixed minus printing

pull/2/head
Mark 2023-04-02 08:27:21 -07:00
parent b5baaf293f
commit 038bbc5fde
Signed by: Mark
GPG Key ID: AD62BB059C2AAEE4
4 changed files with 36 additions and 5 deletions

View File

@ -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

View File

@ -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 {

View File

@ -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())}

View File

@ -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))
)
},