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

Unit's .toNumeric() incorrect immediately after calculation with mixed units #901

Closed
AlexanderBeyn opened this issue Jul 22, 2017 · 3 comments · Fixed by #902
Closed

Unit's .toNumeric() incorrect immediately after calculation with mixed units #901

AlexanderBeyn opened this issue Jul 22, 2017 · 3 comments · Fixed by #902

Comments

@AlexanderBeyn
Copy link
Contributor

When doing calculations with mixed units, such as multiplying feet and meters, .toNumeric() returns an incorrect value immediately after the calculation. Once .formatUnits() has been called on the result, .toNumeric() returns the correct value.

An example at http://jsbin.com/wusavigugi/edit?html,js,output shows that

    const a = math.unit(1, 'ft').multiply(math.unit(1, 'meter'))
    document.write(a.toNumeric() + '<br>');
    document.write(a.formatUnits() + '<br>');
    document.write(a.toNumeric() + '<br>');

outputs

1
meter^2
0.3048
@ericman314
Copy link
Collaborator

ericman314 commented Jul 22, 2017

Thanks for reporting. It seems like this is due to the lazy simplification of units. Combining and simplifying all the units accrued during an operation is costly, so we only do it at the end when the unit is formatted as a string. Perhaps we should alter toNumeric to also trigger the simplification. I can get to this myself next week, or you can submit a PR yourself if it is urgent.

@josdejong
Copy link
Owner

Fixed via #902 (fix not yet released at this moment)

@josdejong
Copy link
Owner

Should be fixed now in v3.16.1

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.

3 participants