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.
Aims to fix gentooboontoo/js-quantities#94.
The method to multiply q1 by q2 goes as follows:
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
, butQty('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 inkm
, andm*km
returns a quantity inm
, the prefix associated with the first occurrence of each unit encountered is used.