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-less values #1240

Closed
Granipouss opened this Issue Sep 10, 2018 · 10 comments

Comments

Projects
None yet
3 participants
@Granipouss

Granipouss commented Sep 10, 2018

Hi,

When I try to execute mathjs.unit('5'), it raises an error Uncaught SyntaxError: "5" contains no units when it should simply create a a variable with value 5 and all units set to 0. I simple work around is to execute mathjs.unit('5 s/s') instead, but I still think this is an issue that can be fixed in a cleaner manner.

Cheers,

@ericman314

This comment has been minimized.

Show comment
Hide comment
@ericman314

ericman314 Sep 10, 2018

Collaborator

I agree. It seems like a trivial case but one that we could easily handle.

Collaborator

ericman314 commented Sep 10, 2018

I agree. It seems like a trivial case but one that we could easily handle.

@josdejong

This comment has been minimized.

Show comment
Hide comment
@josdejong

josdejong Sep 10, 2018

Owner

Should indeed be easy to fix. Just curious, what is your use case for mathjs.unit('5') @Granipouss?

Owner

josdejong commented Sep 10, 2018

Should indeed be easy to fix. Just curious, what is your use case for mathjs.unit('5') @Granipouss?

@Granipouss

This comment has been minimized.

Show comment
Hide comment
@Granipouss

Granipouss Sep 12, 2018

I am working on a nutrition app and I am using mathjs to handle all the rate computation. Food can be measured by mass (for flour, etc.), volume (milk, etc.) or unit (eggs, etc.). I can use all my formula with the proper units for mass and volume but I have to do a workaround for the unit based food item.

As a better work around, I am use the mathjs.eval function right now.

Granipouss commented Sep 12, 2018

I am working on a nutrition app and I am using mathjs to handle all the rate computation. Food can be measured by mass (for flour, etc.), volume (milk, etc.) or unit (eggs, etc.). I can use all my formula with the proper units for mass and volume but I have to do a workaround for the unit based food item.

As a better work around, I am use the mathjs.eval function right now.

@josdejong

This comment has been minimized.

Show comment
Hide comment
@josdejong

josdejong Sep 12, 2018

Owner

@Granipouss thanks that helps. I guess I'm overlooking something, but why can't you use just the number 5 in your use case?

Owner

josdejong commented Sep 12, 2018

@Granipouss thanks that helps. I guess I'm overlooking something, but why can't you use just the number 5 in your use case?

@Granipouss

This comment has been minimized.

Show comment
Hide comment
@Granipouss

Granipouss Sep 12, 2018

I can but the thing is that then quantities are not homogeneous, sometimes it is a mathjs.unit (whether is it kg or L), sometimes is a native Number. And if I want to display them for instance, I have to use format or toString from the former or nothing for the later.

Granipouss commented Sep 12, 2018

I can but the thing is that then quantities are not homogeneous, sometimes it is a mathjs.unit (whether is it kg or L), sometimes is a native Number. And if I want to display them for instance, I have to use format or toString from the former or nothing for the later.

@josdejong

This comment has been minimized.

Show comment
Hide comment
@josdejong

josdejong Sep 13, 2018

Owner

Ah, I understand that. If you want to use methods on the units, you need to make sure you're always dealing with a unit.

You could use functions instead like math.format(unit) instead of methods like unit.format(). The functions math.format() and math.string() work with any input value, like units and numbers.

I don't think it hurts though to allow creating a unit-less unit, we can add it.

Owner

josdejong commented Sep 13, 2018

Ah, I understand that. If you want to use methods on the units, you need to make sure you're always dealing with a unit.

You could use functions instead like math.format(unit) instead of methods like unit.format(). The functions math.format() and math.string() work with any input value, like units and numbers.

I don't think it hurts though to allow creating a unit-less unit, we can add it.

@Granipouss

This comment has been minimized.

Show comment
Hide comment
@Granipouss

Granipouss Sep 13, 2018

math.format() and math.string() seems quite handy, thanks!
Well my main objection on the lack of unit-less support, is that I see no reasn why it's not possible. It doesn't seem harder to implement and physically/mathematically, it is valid. So if you can do it, that super nice.

Granipouss commented Sep 13, 2018

math.format() and math.string() seems quite handy, thanks!
Well my main objection on the lack of unit-less support, is that I see no reasn why it's not possible. It doesn't seem harder to implement and physically/mathematically, it is valid. So if you can do it, that super nice.

@josdejong

This comment has been minimized.

Show comment
Hide comment
@josdejong

josdejong Sep 14, 2018

Owner

Agree

Owner

josdejong commented Sep 14, 2018

Agree

@ericman314

This comment has been minimized.

Show comment
Hide comment
@ericman314

ericman314 Sep 15, 2018

Collaborator

I've submitted #1251 which I think should address this issue. Despite all tests passing, I don't know yet whether there will be any regressions caused by allowing the creation of a dimensionless unit. I don't think there will be, since math.unit('5 s/s') seems to work just fine.

Collaborator

ericman314 commented Sep 15, 2018

I've submitted #1251 which I think should address this issue. Despite all tests passing, I don't know yet whether there will be any regressions caused by allowing the creation of a dimensionless unit. I don't think there will be, since math.unit('5 s/s') seems to work just fine.

@josdejong

This comment has been minimized.

Show comment
Hide comment
@josdejong

josdejong Oct 5, 2018

Owner

This is available now in v5.2.0, thanks again @ericman314 . And sorry for the delay, I had totally forgotten to release this improvement earlier.

Owner

josdejong commented Oct 5, 2018

This is available now in v5.2.0, thanks again @ericman314 . And sorry for the delay, I had totally forgotten to release this improvement earlier.

@josdejong josdejong closed this Oct 5, 2018

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