gentooboontoo / js-quantities

JavaScript library for quantity calculation and unit conversion
http://gentooboontoo.github.io/js-quantities/
MIT License
396 stars 102 forks source link

deal with prefixes when multiplying or dividing #95

Closed christianp closed 5 years ago

christianp commented 5 years ago

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, 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.

gentooboontoo commented 5 years ago

Thanks for your PR. I have published a new release after merging it.