Mathjs: bug report: expression evaluation with units of degC and degF

Created on 27 Jul 2018  路  2Comments  路  Source: josdejong/mathjs

I noticed that doing calculations having units that there exist a bug with the temperature units.

> math.parse('120 degC + 100 degC').eval().toString()
> "493.15 degC"

The calculation is done correctly but the unit should not be "degC", it should be "K"

question

Most helpful comment

If you rewrite your expression using K instead of degC, and compare it side by side with your original expression, it's easier to see what's going on:

120 degC + 100 degC = 493.15 degC
393.15 K + 373.15 K = 766.3 K

120 degC is converted internally to 393.15 K and 100 degC to 373.15 K before the addition. degC is one example of a unit that is not distributive: a degC + b degC != (a + b) degC.

When adding temperatures, you can use the absolute temperature scales K or R to avoid this problem entirely.

All 2 comments

If you rewrite your expression using K instead of degC, and compare it side by side with your original expression, it's easier to see what's going on:

120 degC + 100 degC = 493.15 degC
393.15 K + 373.15 K = 766.3 K

120 degC is converted internally to 393.15 K and 100 degC to 373.15 K before the addition. degC is one example of a unit that is not distributive: a degC + b degC != (a + b) degC.

When adding temperatures, you can use the absolute temperature scales K or R to avoid this problem entirely.

Thanks a lot for the explanation.

Was this page helpful?
0 / 5 - 0 ratings