-
Notifications
You must be signed in to change notification settings - Fork 7
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
Avoiding the precision loss footgun #15
Comments
What I propose (largely thinking out loud) is the following (for one-argument constructor).
The means that Thoughts? |
I appreciate you giving this some thought, Harry. Would indeed be nice if that could work, especially for the Decimal.js case. The reason I wrote that message in the readme was that I considered it a good compromise between two extremes. On the one extreme, we could attempt to parse those custom types as you suggest, by passing everything up to the first whitespace to the Another possibility is to allow the user to control how the custom type is parsed from a string, thus allowing the single string constructor, by providing a One more possibility that might be a better compromise: The line below in Line 194 in c7ce5df
We could skip the |
I've adjusted things so that those lines in Lines 194 to 198 in 3260233
Each of the
If there is a custom unit('3.1415926535897932384626433832795 rad') results in no loss of precision when unit('1 / 2', 'kg')
unit('4 + 3i', 'kVA') Do you think this is acceptable? |
I think this hits the nail on the head:
Any solution that results in no loss of precision would be great in my book 👍
Yes, with one caveat:
My opinion is that this should throw an error. I would go even further and say that simply throwing an error if the one argument constructor is invoked when using a custom type is acceptable. However, the solution you propose is much better than this. I am arguing that if the constructor throws an error, a user can pull up the unitmath README and realise what they need to change. If the constructor silently loses precision then the user will have hard to track down bugs appearing in their program sometime in the future. |
Interesting discussion. Would it work to just have a |
And maybe it's just fine not to accept things like |
@harrysarson, I was a little uneasy about that as well. I will add an error as appropriate to ensure that the
@josdejong, possibly we could do that, but I thought it would be good to have two separate functions so that the user doesn't have to do their own type checking if they don't want to. Once Harry's latest suggestion is implemented, So in summary, this is what will happen: If the user supplies unit('rad') // Works
unit('3.14159 rad') // Works
unit('3.14159', 'rad') // Works
unit(3.14159, 'rad') // Works
unit(Decimal('3.14159', 'rad') // Works
unit('1 / 2 rad') // Parse error
unit('1 / 2', 'rad') // Works
unit(Fraction('1 / 2'), 'rad') // Works If the user only supplies unit('rad') // Works
unit('3.14159 rad') // Parse error
unit('3.14159', 'rad') // Parse error
unit(3.14159, 'rad') // Works
unit(Decimal('3.14159', 'rad') // Works
unit('1 / 2 rad') // Parse error
unit('1 / 2', 'rad') // Parse error
unit(Fraction('1 / 2'), 'rad') // Works |
I get it. Maybe though in that case it's not needed to embed |
After considering all your comments and giving this a lot of thought, I have put together a PR that combines |
UnitMath is shaping up nicely :) I have a question:
From the README (v0.3)
This seems like an unnecessary footgun to introduce and I wonder if it is not possible to provide a better alternative.
The text was updated successfully, but these errors were encountered: