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

Support for unit 1/x #1061

Closed
XeniaSiskaki opened this Issue Mar 13, 2018 · 8 comments

Comments

Projects
None yet
3 participants
@XeniaSiskaki

XeniaSiskaki commented Mar 13, 2018

Currently a unit like 1/K isn't acceptable and has to be manually converted to K^-1 before creating the unit.

This currently works in the Math notepad and can partially work in mathjs with:

var x = math.eval('1/K')
x.equalBase(math.unit('K^-1')) // => true

Would it be possible to add support for such units?

@josdejong

This comment has been minimized.

Owner

josdejong commented Mar 14, 2018

Thanks for your input Xenia. It's not yet clear to me what you mean exactly, you can already use 1/K both in calculations as well as when defining a new unit. What are you missing exactly?

@XeniaSiskaki

This comment has been minimized.

XeniaSiskaki commented Mar 14, 2018

Doing math.unit('1/K') throws an Uncaught SyntaxError: Unexpected "/" in "1/K" at index 1

@ericman314

This comment has been minimized.

Collaborator

ericman314 commented Mar 15, 2018

The Unit constructor invoked using math.unit uses a very simple parser that can only understand certain expressions. Evidently, "number divided by unit", such as 1/K, is not one of them. We could probably add support for that if necessary. (Curiously, 1 K/K^2 is supported.)

Although using math.unit directly is faster, you can achieve the same results using math.eval('1/K'), which gives you all the functionality of the full expression parser, and which will return the same thing math.unit would. Is it acceptable for you to use math.eval instead?

@XeniaSiskaki

This comment has been minimized.

XeniaSiskaki commented Mar 15, 2018

Thanks for your quick input! We will go with math.eval until this is fixed (if ever).

@josdejong

This comment has been minimized.

Owner

josdejong commented Mar 17, 2018

@ericman314 we should indeed be careful not to built an entire expression parser in the unit parser.

The current unit parser already has support for divisions like 5 m/s, it only doesn't handle a case like 1 / s correctly where the division is right after the number, so I think this is simply a bug that we should solve.

@josdejong josdejong reopened this Mar 17, 2018

@josdejong josdejong added the bug label Mar 17, 2018

@josdejong

This comment has been minimized.

Owner

josdejong commented Mar 17, 2018

this should be fixed via b00e0b7 (currently in the develop branch)

@ericman314

This comment has been minimized.

Collaborator

ericman314 commented Mar 17, 2018

Agreed. Thanks for fixing that.

@josdejong josdejong closed this in b00e0b7 Mar 17, 2018

@XeniaSiskaki

This comment has been minimized.

XeniaSiskaki commented Mar 20, 2018

Thanks!

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