8000 [Validator] Improve DivisibleBy validator by apfelbox · Pull Request #28212 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Validator] Improve DivisibleBy validator #28212

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

Closed
wants to merge 3 commits into from
Closed

[Validator] Improve DivisibleBy validator #28212

wants to merge 3 commits into from

Conversation

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

The newly added DivisibleBy validator uses internally fmod, which has some issues regarding floating point numbers and numbers with decimals.

For example fmod(4.1, 0.1) ~= 0.1 and currently fails the validator. See for example this comment in the PHP docs: http://de2.php.net/manual/en/function.fmod.php#122782

This change implements the logic manually ( 😞 ) but should solve more edge cases.

@apfelbox apfelbox changed the title Improve DivisibleBy validator [Validator] Improve DivisibleBy validator Aug 16, 2018
@apfelbox
Copy link
Contributor Author

Not sure whether this is a bug fix or a new feature, though. I would say it is a bug fix (or an improvement), but I have not hard stance there.

$epsilon = 0.0000001;

// can't divide by 0
if ($value2 < $epsilon) {
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't this trigger an exception when the constraint is configured with such an invalid value?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not sure. The previous implementation with fmod just returned NaN (so the validator failed).

We can change it, I just adapted the previous behavior.

return (float) 0 === fmod($value1, $value2);
$value1 = \abs($value1);
$value2 = \abs($value2);
$epsilon = 0.0000001;
Copy link
Member

Choose a reason for hiding this comment

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

What about making this configurable?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I‘m not sure whether this would actually add anything.

Might make sense to add something like FloatUtils to capsiluate some of this logic (comparing floats etc)

@nicolas-grekas
Copy link
Member

I proposed an alternative in #28228 which should handle more precision.

@apfelbox apfelbox deleted the improve-divisible-by branch August 21, 2018 11:55
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 modified the milestones: next, 4.2 Nov 1, 2018
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.

4 participants
0