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

Different behavior for unit conversion "degC" and "K" #2499

Closed
kaytak opened this issue Mar 25, 2022 · 4 comments · Fixed by #2501
Closed

Different behavior for unit conversion "degC" and "K" #2499

kaytak opened this issue Mar 25, 2022 · 4 comments · Fixed by #2501

Comments

@kaytak
Copy link

kaytak commented Mar 25, 2022

Hello,
Thank you for making a good work, I use mathjs as a core library in my engineering calculator tool.
In the contexts of heat transfer calculation, I evaluate the following calculation and I got the correct result.

math.evaluate('0.1 kg/s * 4.2 J/K/g * 5 K').toString()
=> '2.10... kW'

However, when I replace "K" with "degC", which shall give the identical result here, the result seems wrong:
math.evaluate('0.1 kg/s * 4.2 J/degC/g * 5 degC').toString()
=> '0.42... kW'

Could you please check this problem?

Best regards,

@gwhitney
Copy link
Collaborator

Well, the problem can be greatly simplified: math.evaluate('1 J/degC') returns 0.0036476... J / degC ! Probably easier to track the bug down from this example. There are other related unit weirdnesses I will file a separate issue for, but I think this example is the core of your specific problem.

@gwhitney
Copy link
Collaborator

Ah, and the bug has nothing to do with joules, it happens with any unit in that place, e.g., "1 m/degC" evaluates to 0.0036.. m/degC, etc. And degF has the same problem, but it produces a different value: "1 in/degF" evaluates to 0.00217.. in/degF. And it's not just about unit division, it happens with multiplication as well: 1 m degC evaluates to 274.15 m degC and 1 in degF evaluates to 460.67 in degF. So seems like it has something to do with the zero points for these different temperature units being different, even though that should only come into play when converting between different temperature units, not when multiplying a temperature unit by another unit...

@gwhitney
Copy link
Collaborator

Further, multiplication with temperature units is not associative: "1 (m degC)" evaluates fine (to "1 m degC").

@gwhitney
Copy link
Collaborator

Aha, the problem is that the "offset" of a unit (which only applies to temperature units other than Kelvin and Rankin) is being applied too much. For example "20 degC + 1 degC" is evaluating to "294.15 degC", not "21 degC" as it should. Similarly "20 deg C - 1 deg C" is coming out below -200 degC. I will see if I can produce a PR that tames the offset at least somewhat.

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

Successfully merging a pull request may close this issue.

2 participants