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

Comments

Projects
None yet
3 participants
@AlexanderBeyn
Contributor

AlexanderBeyn commented Jul 22, 2017

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

This comment has been minimized.

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

This comment has been minimized.

Owner

josdejong commented Aug 12, 2017

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

@josdejong

This comment has been minimized.

Owner

josdejong commented Aug 12, 2017

Should be fixed now in v3.16.1

@josdejong josdejong closed this Aug 12, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment