Prevent weird decompositions like Pa m -> A tesla

This commit is contained in:
Tiffany Bennett 2016-08-25 22:41:16 -04:00
parent 685e5149d1
commit 0041d12b1e
2 changed files with 10 additions and 2 deletions

View file

@ -25,7 +25,15 @@ impl cmp::Ord for Factors {
pub fn fast_decompose(value: &Number, aliases: &BTreeMap<Unit, String>) -> Unit {
let mut best = None;
for (unit, name) in aliases.iter() {
'outer: for (unit, name) in aliases.iter() {
// make sure we aren't doing something weird like introducing new base units
for (dim, pow) in unit {
let vpow = value.1.get(dim).cloned().unwrap_or(0);
let snum = (vpow - pow).signum();
if snum != 0 && snum != vpow.signum() {
continue 'outer
}
}
let num = Number(Mpq::one(), unit.clone());
for &i in [-1, 1, 2].into_iter() {
let res = (value / &num.powi(i)).unwrap();

View file

@ -37,5 +37,5 @@ fn test_temp() {
#[test]
fn test_determinism() {
test("pascal m", "1 A tesla (spectral_irradiance_frequency)");
test("weber / m", "1 m tesla");
}