Skip to content

Commit

Permalink
Merge pull request #470 from AzuraGroup/fmod-fix
Browse files Browse the repository at this point in the history
Don't use PHP's fmod
  • Loading branch information
bighappyface authored May 23, 2018
2 parents a4bf903 + 1b51bd2 commit 52086d6
Showing 1 changed file with 4 additions and 9 deletions.
13 changes: 4 additions & 9 deletions src/JsonSchema/Constraints/NumberConstraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,18 +70,13 @@ public function check(&$element, $schema = null, JsonPointer $path = null, $i =

private function fmod($number1, $number2)
{
$number1 = abs($number1);
$modulus = fmod($number1, $number2);
$precision = abs(0.0000000001);
$diff = (float) ($modulus - $number2);
$modulus = ($number1 - round($number1 / $number2) * $number2);
$precision = 0.0000000001;

if (-$precision < $diff && $diff < $precision) {
if (-$precision < $modulus && $modulus < $precision) {
return 0.0;
}

$decimals1 = mb_strpos($number1, '.') ? mb_strlen($number1) - mb_strpos($number1, '.') - 1 : 0;
$decimals2 = mb_strpos($number2, '.') ? mb_strlen($number2) - mb_strpos($number2, '.') - 1 : 0;

return (float) round($modulus, max($decimals1, $decimals2));
return $modulus;
}
}

0 comments on commit 52086d6

Please sign in to comment.