Skip to content
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

[Validator] Fix precision issue regarding floats and DivisibleBy constraint #28228

Merged
merged 1 commit into from Aug 21, 2018

Conversation

nicolas-grekas
Copy link
Member

Q A
Branch? master
Bug fix? yes
New feature? no
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets -
License MIT
Doc PR -

Alternative to #28212

@apfelbox
Copy link
Contributor

Looks great. 👍
I am uncomfortable with directly comparing floats, though. Do you think using the "standard epsilon" approach might catch more edge cases?

@nicolas-grekas
Copy link
Member Author

nicolas-grekas commented Aug 19, 2018

The sprintf does rounding up to the 12th decimal, which means it's like your epsilon comparison, but also that is copes with the scale of the number, thanks to using the scientific notation for the rounding.

@@ -23,7 +23,11 @@ class DivisibleByValidator extends AbstractComparisonValidator
*/
protected function compareValues($value1, $value2)
{
return (float) 0 === fmod($value1, $value2);
if (!$value1 = fmod($value1, $value2)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the $value1= assignment important?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is, so that the check just below uses the rest returned by fmod()

@apfelbox
Copy link
Contributor

The sprintf does rounding up to the 12th decimal, which means it's like your epsilon comparison, but also that is copes with the scale of the number, thanks to using the scientific notation for the rounding.

Yeah, but afterwards we still convert both to a float and do a direct comparison.
I am currently not sure whether that works out in all cases, but I trust you, if you say that this is enough. 😉

So 👍 for the PR, as it is simpler and more readable

@nicolas-grekas
Copy link
Member Author

I improved precision a bit for integers and rounded both operands to the 12th decimal for floats.

@apfelbox
Copy link
Contributor

Looks great, thank you! 👍

@nicolas-grekas
Copy link
Member Author

Thank you @apfelbox.

@nicolas-grekas nicolas-grekas merged commit ae04b48 into symfony:master Aug 21, 2018
nicolas-grekas added a commit that referenced this pull request Aug 21, 2018
…ibleBy constraint (apfelbox)

This PR was merged into the 4.2-dev branch.

Discussion
----------

[Validator] Fix precision issue regarding floats and DivisibleBy constraint

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | -
| License       | MIT
| Doc PR        | -

Alternative to #28212

Commits
-------

ae04b48 [Validator] Fix precision issue regarding floats and DivisibleBy constraint
@nicolas-grekas nicolas-grekas deleted the improve-divisible-by branch August 24, 2018 13:46
@nicolas-grekas nicolas-grekas modified the milestones: next, 4.2 Nov 1, 2018
@ERuban
Copy link
Contributor

ERuban commented Jul 25, 2020

@nicolas-grekas but seems that still didn't works.
As example,
fmod(0.01, 0.001) returns 0, and we got true,
but fmod(0.1, 0.001) returns 3.4694469519536E-18, and we got false.
¯_(ツ)_/¯

@xabbuh
Copy link
Member

xabbuh commented Jul 27, 2020

@ERuban Can you please open a new issue if you think that things still need to be improved? Comments on already merged PRs are likely to get lost.

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

Successfully merging this pull request may close these issues.

None yet

6 participants