mirror of https://github.com/rm-dr/daisy
Improved superscripts
parent
f35aeefeb1
commit
9698a691aa
|
@ -46,8 +46,22 @@ impl Expression {
|
|||
}
|
||||
}
|
||||
|
||||
// True if this is a power operator applied to a constant or variable
|
||||
// and an integer.
|
||||
// True if this is a unitless integer
|
||||
pub fn is_unitless_integer(&self) -> bool {
|
||||
match self {
|
||||
Expression::Quantity(_, q) => {
|
||||
return q.unitless() && q.fract().is_zero();
|
||||
},
|
||||
Expression::Operator(_, Operator::Negative, q) => {
|
||||
assert!(q.len() == 1);
|
||||
let Expression::Quantity(_, q) = &q[0] else { return false };
|
||||
return q.unitless() && q.fract().is_zero();
|
||||
}
|
||||
_ => { return false; }
|
||||
}
|
||||
}
|
||||
|
||||
// True if this is a integer power operator applied to a constant or variable.
|
||||
// Examples: pi^2, x ^ 3
|
||||
pub fn is_poly_power(&self) -> bool {
|
||||
match self {
|
||||
|
@ -58,7 +72,7 @@ impl Expression {
|
|||
let base = &a[0];
|
||||
let power = &a[1];
|
||||
|
||||
// Make sure base ks const or variable
|
||||
// Make sure base is const or variable
|
||||
match base {
|
||||
Expression::Constant(_, _)
|
||||
| Expression::Variable(_, _)
|
||||
|
@ -68,14 +82,7 @@ impl Expression {
|
|||
};
|
||||
|
||||
// Make sure power is an integer
|
||||
match power {
|
||||
Expression::Quantity(_, q) => {
|
||||
return q.unitless() && q.fract().is_zero();
|
||||
},
|
||||
_ => { return false; }
|
||||
}
|
||||
|
||||
|
||||
return power.is_unitless_integer();
|
||||
},
|
||||
_ => { return false; }
|
||||
};
|
||||
|
|
|
@ -206,9 +206,10 @@ impl Operator {
|
|||
},
|
||||
|
||||
Operator::Power => {
|
||||
if let Expression::Quantity(_, q) = &args[1] {
|
||||
if q.unitless() && q.fract().is_zero() {
|
||||
|
||||
let q = &args[1];
|
||||
|
||||
if q.is_unitless_integer() {
|
||||
// Write integer powers as a superscript
|
||||
let mut b = String::new();
|
||||
for c in q.to_string().chars() {
|
||||
|
@ -240,13 +241,6 @@ impl Operator {
|
|||
self.add_parens_to_arg_strict(&args[1])
|
||||
);
|
||||
}
|
||||
} else {
|
||||
return format!(
|
||||
"{}^{}",
|
||||
self.add_parens_to_arg_strict(&args[0]),
|
||||
self.add_parens_to_arg_strict(&args[1])
|
||||
);
|
||||
}
|
||||
},
|
||||
|
||||
Operator::Factorial => {
|
||||
|
|
Loading…
Reference in New Issue