Added nounit and tobase functions

This commit is contained in:
2023-06-14 09:24:34 -07:00
parent aa02b35cc5
commit 061f58ba53
6 changed files with 83 additions and 35 deletions

View File

@ -51,7 +51,7 @@ impl FreeUnit {
}
// Get this unit in terms of base units
pub fn get_base(&self) -> Quantity {
pub fn to_base(&self) -> Quantity {
let q = self.whole.base_factor();
let mut q = q.unwrap_or(Quantity::new_rational_from_string("1").unwrap());

View File

@ -123,7 +123,7 @@ impl Unit {
flag = false;
for (uo, po) in other.get_val() {
if {
us.get_base().unit.compatible_with(&uo.get_base().unit)
us.to_base().unit.compatible_with(&uo.to_base().unit)
} {
factor.insert_unit(us.clone(), po.clone());
flag = true;
@ -141,7 +141,7 @@ impl Unit {
flag = false;
for (us, _) in self.get_val() {
if {
us.get_base().unit.compatible_with(&uo.get_base().unit)
us.to_base().unit.compatible_with(&uo.to_base().unit)
} {
factor.insert_unit(us.clone(), po.clone());
flag = true;
@ -186,11 +186,21 @@ impl Unit {
return q;
}
pub fn to_base(&self) -> Quantity {
let mut q = Quantity::new_rational(1f64).unwrap();
for (u, p) in self.get_val().iter() {
let b = u.to_base();
q.mul_assign_no_convert(b.pow(Quantity::from_scalar(p.clone())));
}
return q;
}
}
impl Unit {
pub fn from_string(s: &str) -> Option<Quantity> {
let b = freeunit_from_string(s);
if b.is_none() { return None; }
let b = Unit::from_free(b.unwrap());