From 761c47a675c4d644eb80a4e98de02e44109b22b4 Mon Sep 17 00:00:00 2001 From: Bert Ramakers Date: Tue, 8 Oct 2024 10:32:02 +0200 Subject: [PATCH] Make sure result of division is always a float --- src/Schema/Type/Number.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/Schema/Type/Number.php b/src/Schema/Type/Number.php index 6f534db..4490dbd 100644 --- a/src/Schema/Type/Number.php +++ b/src/Schema/Type/Number.php @@ -52,9 +52,12 @@ public function validate(mixed $value, callable $fail): void // Divide the value by multipleOf instead of using the modulo operator to avoid bugs when using a multipleOf // that has decimal places. (Since the modulo operator converts the multipleOf to int) + // Note that dividing two integers returns another integer if the result is a whole number. So to make the + // comparison work at all times we need to cast the result to float. Casting both to integer will not work + // as intended since then the result of the division would also be rounded. if ( $this->multipleOf !== null && - $value / $this->multipleOf !== round($value / $this->multipleOf) + (float) ($value / $this->multipleOf) !== round($value / $this->multipleOf) ) { $fail(sprintf('must be a multiple of %d', $this->multipleOf)); }