Skip to content

multipleOf not always behaving correctly #127

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
ghost opened this issue Aug 16, 2021 · 2 comments · Fixed by #128
Closed

multipleOf not always behaving correctly #127

ghost opened this issue Aug 16, 2021 · 2 comments · Fixed by #128

Comments

@ghost
Copy link

ghost commented Aug 16, 2021

Current implementation of multipleOf might lead to unexpected results.
Say we have a schema of number with multipleOf = 0.01. When now the number 4.22 is input then there is an error that 4.22 is no multiple of 0.01. This happens because in https://github.com/swaggest/php-json-schema/blob/master/src/Schema.php#L348 the division yields 422.0 and casting this to integer results in 421 which does not match.

Why PHP does this I'm not sure. Likely the internal representation differs and this leads to some rounding or the like. See https://stackoverflow.com/q/3385685/6221202 for a similar problem and possible solutions.

@vearutop
Copy link
Member

This problem is caused by the nature of float numbers, that infinite variety of floats is represented by finite number of bit combinations.

json-schema-org/json-schema-spec#312
Here are a few alternative strategies to check.
https://3v4l.org/6fSe8

Not sure so far how to proceed with this problem.

@ghost
Copy link
Author

ghost commented Aug 17, 2021

Wow, even the behavior in 7.0, 7.3 and 8 is different. For me it failed with 7.0 (which is EOL I know). I'm also not sure but the string casting method could work. Maybe as most accurate method we could compute the precision of the multipleOf parameter and use that as input to bcdiv?

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

Successfully merging a pull request may close this issue.

1 participant