Skip to content

Commit

Permalink
Updated isMultipleOf helper
Browse files Browse the repository at this point in the history
  • Loading branch information
sorinsarca committed Dec 30, 2024
1 parent a3be216 commit 7128277
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions src/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -259,26 +259,29 @@ public static function equals($a, $b): bool
*/
public static function isMultipleOf($number, $divisor, ?int $scale = null): bool
{
if ($number == $divisor) {
if (!$number || $number == $divisor) {
return true;
}

if ($divisor == 0) {
return $number == 0;
}

if ($divisor == 1 && !is_string($number)) {
return is_int($number) || !fmod($number, 1);
}

// maybe we get lucky
if (!fmod($number, $divisor)) {
return true;
}

// int mod
if (is_int($number) && is_int($divisor)) {
return !($number % $divisor);
$divisor = abs($divisor);
if ($divisor == 1) {
// should have been caught by the above fmod()
return false;
}

$number = abs($number);
if ($divisor > $number) {
// number / divisor < 1
return false;
}

// Use global scale if null
Expand All @@ -289,8 +292,7 @@ public static function isMultipleOf($number, $divisor, ?int $scale = null): bool
!(self::$hasBCMath ??= extension_loaded('bcmath'))
) {
// use an approximation
$div = $number / $divisor;
return abs($div - round($div)) < (10 ** -$scale);
return abs($number - round($number / $divisor) * $divisor) < (10 ** -$scale);
}

// use bcmath
Expand Down

0 comments on commit 7128277

Please sign in to comment.