You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If I calculate Qty("1m").div("1km") I get a unitless 0.001 as I expected.
But if I calculate (Qty("1").div("1km")).mul("m") I get 1 m/km. Which is actually correct, but I would have expected this to just be a unitless 0.001 as above.
Also if I calculate (Qty("1").div("1km")).mul("m").mul("") I get 0.001 m2/km2. Again this is technically the correct result, but not what I would have expected.
Is this an issue with js-quantities, or are my expectations wrong?
The text was updated successfully, but these errors were encountered:
JoshuaCapel
changed the title
Unexpected issues with unitless quantities
Unexpected results when dealing with unitless quantities (dividing quantities in an unsual orders)
Nov 29, 2018
Aims to fixgentooboontoo#94.
The method to multiply q1 by q2 goes as follows:
* if q1 and q2 are compatible (they have exactly the same signature),
convert q2 to the same units as q1
* collect together the numerators and denominators of q1 and q2, and
'clean'
* multiply q1 and q2's scalars together, and return a unit that big with the
cleaned numerator and denominator
The cleaning code doesn't gather together units of the same kind, but
with different prefixes. I suspect this is deliberate, but it leads to
inconsistencies like Qty('1 m').div('1 km') = 0.001, but Qty('1
m').mul('1 1/km') = 1 m/km.
This commit changes `cleanTerms` to keep track of the prefix used with
each unit, and accumulate a scaling factor when the same unit is used with a
different prefix. The function returns numerator, denominator, and the
scaling factor.
Following the logic that 'km*m' returns a quantity in km, and 'm*km'
returns a quantity in m, the prefix associated with the first occurrence
of each unit encountered is used.
Thanks for reporting, your expectations are right and resulting units were inconsistent.
I have merged @christianp's PR #95 which has fixed the problem. I have just published a new release including it.
If I calculate
Qty("1m").div("1km")
I get a unitless 0.001 as I expected.But if I calculate
(Qty("1").div("1km")).mul("m")
I get 1 m/km. Which is actually correct, but I would have expected this to just be a unitless 0.001 as above.Also if I calculate
(Qty("1").div("1km")).mul("m").mul("")
I get 0.001 m2/km2. Again this is technically the correct result, but not what I would have expected.Is this an issue with js-quantities, or are my expectations wrong?
The text was updated successfully, but these errors were encountered: