Fixed unit multiplication prettyprint

pull/2/head
Mark 2023-06-11 15:16:00 -07:00
parent b886927334
commit e816cea236
Signed by: Mark
GPG Key ID: AD62BB059C2AAEE4
4 changed files with 24 additions and 5 deletions

2
Cargo.lock generated
View File

@ -22,7 +22,7 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]] [[package]]
name = "daisy" name = "daisy"
version = "0.2.2" version = "0.2.3"
dependencies = [ dependencies = [
"cfg-if", "cfg-if",
"rug", "rug",

View File

@ -1,6 +1,6 @@
[package] [package]
name = "daisy" name = "daisy"
version = "0.2.2" version = "0.2.3"
edition = "2021" edition = "2021"
[profile.release] [profile.release]

View File

@ -1,6 +1,5 @@
- Prettyprint mi/h, mi/g - Prettyprint mi/h, mi/g
- Better constant definitions - Better constant definitions
- No times when number * unit
- Fix unit conversion (km/s * mile) - Fix unit conversion (km/s * mile)
## Pre-release ## Pre-release

View File

@ -264,7 +264,7 @@ impl Operator {
let a = &args[0]; let a = &args[0];
let b; let sub; let b; let sub;
if let Token::Operator(o,ar) = &args[1] { if let Token::Operator(o, ar) = &args[1] {
if let Operator::Negative = o { if let Operator::Negative = o {
sub = true; sub = true;
b = &ar[0]; b = &ar[0];
@ -290,18 +290,38 @@ impl Operator {
let a = &args[0]; let a = &args[0];
let b; let div; let b; let div;
if let Token::Operator(o,ar) = &args[1] { if let Token::Operator(o, ar) = &args[1] {
if let Operator::Flip = o { if let Operator::Flip = o {
div = true; div = true;
b = &ar[0]; b = &ar[0];
} else { div = false; b = &args[1]; } } else { div = false; b = &args[1]; }
} else { div = false; b = &args[1]; } } else { div = false; b = &args[1]; }
// Division symbol case
if div { if div {
return format!("{} ÷ {}", return format!("{} ÷ {}",
self.add_parens_to_arg_strict(a), self.add_parens_to_arg_strict(a),
self.add_parens_to_arg_strict(b) self.add_parens_to_arg_strict(b)
); );
}
// Omit times sign when we have a number
// multiplied by a unit (like 10 m)
// Times sign should stay in all other cases.
let no_times = {
if let Token::Quantity(p) = a {
if let Token::Quantity(q) = b {
p.unitless() && !q.unitless()
} else {false}
} else {false}
};
if no_times {
return format!("{} {}",
self.add_parens_to_arg_strict(a),
self.add_parens_to_arg_strict(b)
);
} else { } else {
return format!("{} × {}", return format!("{} × {}",
self.add_parens_to_arg_strict(a), self.add_parens_to_arg_strict(a),