Skip to content

[PHP 8.4] Add bcdivmod #519

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

Merged
merged 1 commit into from
Jun 24, 2025
Merged

Conversation

alexandre-daubois
Copy link
Member

Supersedes #503. Thank you @Ayesh!

Copy link
Member

@nicolas-grekas nicolas-grekas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed the implementation this way:

  • the validation of the arguments is already done by the call to bcdiv so there's not need to do it (and possibility introduce subtle behavioral diffs)
  • the $scale of bcdiv should be 0explicitly IIUC
     public static function bcdivmod(string $num1, string $num2, ?int $scale = null): ?array
     {
-        if (PHP_FLOAT_EPSILON < abs((float) $num2)) {
-            throw new \DivisionByZeroError('Division by zero');
-        }
-
-        if (!is_numeric($num1)) {
-            if (class_exists(\ValueError::class)) {
-                throw new \ValueError('Argument #1 ($num1) is not well-formed');
-            }
-
-            trigger_error('Argument #1 ($num1) is not well-formed', E_USER_WARNING);
-
-            return null;
-        }
-
-        if (!is_numeric($num2)) {
-            if (class_exists(\ValueError::class)) {
-                throw new \ValueError('Argument #2 ($num2) is not well-formed');
-            }
-
-            trigger_error('Argument #2 ($num2) is not well-formed', E_USER_WARNING);
-
-            return null;
-        }
-
-        return [
-            \bcdiv($num1, $num2),
-            \bcmod($num1, $num2, $scale ?? \bcscale() ?? ini_get('bcmath.scale') ?: 0),
-        ];
+        if (null === $quot = \bcdiv($num1, $num2, 0)) {
+            return null;
+        }
+        $scale = $scale ?? (\PHP_VERSION_ID >= 70300 ? \bcscale() : (ini_get('bcmath.scale') ?: 0);
+
+        return [$quot, \bcmod($num1, $num2, $scale)];
     }

@nicolas-grekas
Copy link
Member

Thank you @alexandre-daubois.

@nicolas-grekas nicolas-grekas merged commit 1f819a2 into symfony:1.x Jun 24, 2025
0 of 10 checks passed
@nicolas-grekas
Copy link
Member

And thank you @Ayesh !

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 this pull request may close these issues.

3 participants