-
-
Notifications
You must be signed in to change notification settings - Fork 883
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
multipleOf precision #84
Comments
It is also the case in v8 that 53.198098 / 0.0000001 = 531980980.00000006, although the precise value is 531980980. There is no efficient way to improve integer arithmetics. Also I am not sure what is the practical reason to use Also you probably could define modulo precision for multipleOf as an option. It can be an option If that's what you want maybe you could make a PR with such option. |
@epoberezkin It might be worth looking at |
@blakeembrey thanks, it's interesting. Never heard of it :) The disadvantages are that it's ES6, so won't work in node 0.10 (we still use it...) and in some supported browsers, and that the above would fail anyway: |
@epoberezkin Right, you'd use it to check the difference so you probably can't use it here (maybe, bad at math, sorry!). Also, it's just a number so I'm sure it's fine to polyfill. E.g. Edit: Thought, probably wrong, but you could do |
yes, 1e-7 will work too (it's compiled so can be |
Not sure if this is related, but multipleOf 0.01 fails to validate 4.18, etc. |
Yes, same story here. 4.18 / 0.01 = 417.99999999999994. 4.18 % 0.01 is 0.01which is really bad... (multipleOf doesn't use modulo though, it compares the result of the division with parseInt() of this result). The option I suggested would resolve this issue and probably it should be set to 12 by default... |
For the following property:
Validation will fail for the value 40.7593657 even when |
It passes with 6. multipleOfPrecision is explained in docs, see the docs on the information about this option. |
I read the option description in the docs before posting that comment. It makes no mention of 6 being the upper limit and states this option can be set "to some positive integer N". Did you get 6 from |
You can't really compute this N, it just determines how close the result of division should be to a nearest integer number. The number of correct fractional digits depends on the number of digits in the integer part. So the correct N would depend on the range of the division results. With the divider 1e-8 you can't have too many fractional digits as the result is quite large... There can be a better algorithm than Math.abs(Math.round(division) - division) < 1e-N, e.g. see #652 submitted today too. |
For example 555555555555.22/0.01 = 55555555555521.99 |
It would pass with multipleOfPrecision equal to 1 or 2. multipleOf is not really created to determine the number of digits in the fractional format. If you really need to enforce a certain number of fractional digits you can consider using a string and patterns to validate it, rather than a number. |
NB: the algorithm might trigger less floating points issues if it was just making sure that |
For example, the result of 53.198098 % 0.0000001 is 4.068961201910684e-15. And this causes an errors with the "multipleOf" keyword.
Please, use something another instead of the modulo ("%") operator for validation of this keyword.
The text was updated successfully, but these errors were encountered: