Should number(x) always result in a number (if it doesn't throw)?
#3451
Replies: 2 comments 2 replies
-
|
The construction functions like I think we should not let If we want function |
Beta Was this translation helpful? Give feedback.
-
|
Do we know whether these conveniences are really adding value -- are they ever practically used, or do they just seem nice in theory? They come at a significant cost: we can't really describe or use So we need to add to mathjs a "pure" converter/constructor into the number type. What should we call it? And since we are in process of adding options to the conversion, would you be OK with adding a "predictable" option to the constructors like |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I would think, as the family of functions in the
typeorconstructorgroup (number,bignumber,bigint, etc.) are represented as the explicit conversions into the named type, that the answer to the title question should clearly be "yes." However, I just realized it is currently not always the case:number(2 gram)returns2 gram. I certainly expected it to produce2. However, it is a pattern among several of the number conversions: calling it on a unit value calls the conversion on the numeric value part of the unit and leaves the dimensions of the unit alone, resulting in a unit value.As someone writing code in which I want a consistent way to ensure the type produced from a user-supplied MaJE formula, I would heartily recommend that
number(Unit)be changed either to always throw, or to produce the numeric value of the unit argument in its current units, converted to number type if need be (in other words, computeu.toNumber(u.measuredIn())in the proposed convention from #3441). For performing a numeric conversion on the numeric value of a unit, #3441 together with the non-throwing proposal would allowunit(number(u), u.measuredIn()); a shorter syntax could be provided if need be, perhapsu.valueTo('number')or something like that.Beta Was this translation helpful? Give feedback.
All reactions