Skip to content

Commit 761c47a

Browse files
authored
Make sure result of division is always a float
1 parent 15f65a2 commit 761c47a

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

src/Schema/Type/Number.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,12 @@ public function validate(mixed $value, callable $fail): void
5252

5353
// Divide the value by multipleOf instead of using the modulo operator to avoid bugs when using a multipleOf
5454
// that has decimal places. (Since the modulo operator converts the multipleOf to int)
55+
// Note that dividing two integers returns another integer if the result is a whole number. So to make the
56+
// comparison work at all times we need to cast the result to float. Casting both to integer will not work
57+
// as intended since then the result of the division would also be rounded.
5558
if (
5659
$this->multipleOf !== null &&
57-
$value / $this->multipleOf !== round($value / $this->multipleOf)
60+
(float) ($value / $this->multipleOf) !== round($value / $this->multipleOf)
5861
) {
5962
$fail(sprintf('must be a multiple of %d', $this->multipleOf));
6063
}

0 commit comments

Comments
 (0)