Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unexpected results when dealing with unitless quantities (dividing quantities in an unsual orders) #94

Closed
JoshuaCapel opened this issue Nov 29, 2018 · 1 comment · Fixed by #95

Comments

@JoshuaCapel
Copy link

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?

@JoshuaCapel 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
christianp added a commit to christianp/js-quantities that referenced this issue Dec 3, 2018
Aims to fix gentooboontoo#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.
@gentooboontoo
Copy link
Owner

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants