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 joules in watt hour calculations #1481

Closed
zcohan opened this issue Apr 21, 2019 · 3 comments
Closed

Unexpected joules in watt hour calculations #1481

zcohan opened this issue Apr 21, 2019 · 3 comments

Comments

@zcohan
Copy link

zcohan commented Apr 21, 2019

I'm having trouble understanding math.js's behaviour in regard to watt hour units. I'd expect to be able to work directly with kilowatt hours or watt hours, however they sometimes are displayed in joules implicitly:

Example:

1 kWh = 1 kWh (expected)
1 kilowatt hour = 3.6 MJ (unexpected).

@ericman314
Copy link
Collaborator

The way math.js chooses a display unit when using the auto unit system is that it gives preference to units that you have used before. The best way to describe it is with an example:

console.log(math.eval('1 kilowatt * 1 hour').toString())
console.log(math.eval('1 kWh').toString())
console.log(math.eval('1 kilowatt * 1 hour').toString())

Which outputs:

3.6 MJ
1 kWh
1 kWh

Before you use the kWh unit explicitly in the second statement, math.js does not know whether you prefer MJ or something else.

We don't like side effects but we haven't been able to come up with a better way to do it yet.

@zcohan
Copy link
Author

zcohan commented Apr 21, 2019

Thanks Eric, that's indeed quite surprising. Can I ask what the reasoning was behind the decision to take previous calculations into account when evaluating an expression?

What would be the downside to math.js only doing a unit conversion in auto when the conversion is explicitly requested?

@ericman314
Copy link
Collaborator

We've had lots of issues regarding making the unit formatting more configurable. I've opened another issue so we can collect all the ideas in one place: #1483

@zcohan zcohan closed this as completed Apr 22, 2019
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

No branches or pull requests

2 participants