Skip to content

Commit

Permalink
Minor code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
rben01 committed Nov 4, 2024
1 parent eced4db commit 7475ba2
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 14 deletions.
18 changes: 7 additions & 11 deletions numbat/src/quantity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -348,9 +348,7 @@ impl PrettyPrint for Quantity {
pub(crate) enum QuantityOrdering {
IncompatibleUnits,
NanOperand,
Less,
Equal,
Greater,
Ok(std::cmp::Ordering),
}

impl Quantity {
Expand All @@ -365,14 +363,12 @@ impl Quantity {
return QuantityOrdering::IncompatibleUnits;
};

match self.value.partial_cmp(&other_converted.value) {
Some(cmp) => match cmp {
std::cmp::Ordering::Less => QuantityOrdering::Less,
std::cmp::Ordering::Equal => QuantityOrdering::Equal,
std::cmp::Ordering::Greater => QuantityOrdering::Greater,
},
None => unreachable!("unexpectedly got a None partial_cmp from non-NaN arguments"),
}
let cmp = self
.value
.partial_cmp(&other_converted.value)
.expect("unexpectedly got a None partial_cmp from non-NaN arguments");

QuantityOrdering::Ok(cmp)
}

/// Pretty prints with the given options.
Expand Down
9 changes: 6 additions & 3 deletions numbat/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -754,6 +754,7 @@ impl Vm {
}
op @ (Op::LessThan | Op::GreaterThan | Op::LessOrEqual | Op::GreatorOrEqual) => {
use crate::quantity::QuantityOrdering;
use std::cmp::Ordering;

let rhs = self.pop_quantity();
let lhs = self.pop_quantity();
Expand All @@ -768,11 +769,13 @@ impl Vm {
)))
}
QuantityOrdering::NanOperand => false,
QuantityOrdering::Less => matches!(op, Op::LessThan | Op::LessOrEqual),
QuantityOrdering::Equal => {
QuantityOrdering::Ok(Ordering::Less) => {
matches!(op, Op::LessThan | Op::LessOrEqual)
}
QuantityOrdering::Ok(Ordering::Equal) => {
matches!(op, Op::LessOrEqual | Op::GreatorOrEqual)
}
QuantityOrdering::Greater => {
QuantityOrdering::Ok(Ordering::Greater) => {
matches!(op, Op::GreaterThan | Op::GreatorOrEqual)
}
};
Expand Down

0 comments on commit 7475ba2

Please sign in to comment.