forked from php/php-src
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[RFC] ext/bcmath: Added
bcdivmod
(php#15740)
RFC: https://wiki.php.net/rfc/add_bcdivmod_to_bcmath Added bcdivmod() function and added divmod() method to BcMath\Number class.
- Loading branch information
1 parent
2b90acb
commit f6db576
Showing
11 changed files
with
681 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
--TEST-- | ||
bcdivmod() function | ||
--EXTENSIONS-- | ||
bcmath | ||
--INI-- | ||
bcmath.scale=0 | ||
--FILE-- | ||
<?php | ||
require(__DIR__ . "/run_bcmath_tests_function.inc"); | ||
|
||
$dividends = ["15", "-15", "1", "-9", "14.14", "-16.60", "0.15", "-0.01"]; | ||
$divisors = array_merge($dividends, [ | ||
"15151324141414.412312232141241", | ||
"-132132245132134.1515123765412", | ||
"141241241241241248267654747412", | ||
"-149143276547656984948124912", | ||
"0.1322135476547459213732911312", | ||
"-0.123912932193769965476541321", | ||
]); | ||
|
||
$scales = [0, 10]; | ||
foreach ($scales as $scale) { | ||
foreach ($dividends as $firstTerm) { | ||
foreach ($divisors as $secondTerm) { | ||
[$quot, $rem] = bcdivmod($firstTerm, $secondTerm, $scale); | ||
$div_ret = bcdiv($firstTerm, $secondTerm, 0); | ||
$mod_ret = bcmod($firstTerm, $secondTerm, $scale); | ||
|
||
if (bccomp($quot, $div_ret) !== 0) { | ||
echo "Div result is incorrect.\n"; | ||
var_dump($firstTerm, $secondTerm, $scale, $quot, $rem, $div_ret, $mod_ret); | ||
echo "\n"; | ||
} | ||
|
||
if (bccomp($rem, $mod_ret) !== 0) { | ||
echo "Mod result is incorrect.\n"; | ||
var_dump($firstTerm, $secondTerm, $scale, $quot, $rem, $div_ret, $mod_ret); | ||
echo "\n"; | ||
} | ||
} | ||
} | ||
} | ||
echo 'done!'; | ||
?> | ||
--EXPECT-- | ||
done! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
--TEST-- | ||
bcdivmod() function div by zero | ||
--EXTENSIONS-- | ||
bcmath | ||
--INI-- | ||
bcmath.scale=0 | ||
--FILE-- | ||
<?php | ||
require(__DIR__ . "/run_bcmath_tests_function.inc"); | ||
|
||
$dividends = [ | ||
"15", "-15", "1", "-9", "14.14", "-16.60", "0.15", "-0.01", | ||
"15151324141414.412312232141241", | ||
"-132132245132134.1515123765412", | ||
"141241241241241248267654747412", | ||
"-149143276547656984948124912", | ||
"0.1322135476547459213732911312", | ||
"-0.123912932193769965476541321", | ||
]; | ||
|
||
$divisors = [ | ||
'0', | ||
'0.00', | ||
]; | ||
|
||
foreach ($dividends as $firstTerm) { | ||
foreach ($divisors as $secondTerm) { | ||
try { | ||
bcdivmod($firstTerm, $secondTerm); | ||
echo "NG\n"; | ||
} catch (Error $e) { | ||
echo $e->getMessage() === 'Division by zero' ? 'OK' :'NG'; | ||
echo "\n"; | ||
} | ||
} | ||
} | ||
?> | ||
--EXPECT-- | ||
OK | ||
OK | ||
OK | ||
OK | ||
OK | ||
OK | ||
OK | ||
OK | ||
OK | ||
OK | ||
OK | ||
OK | ||
OK | ||
OK | ||
OK | ||
OK | ||
OK | ||
OK | ||
OK | ||
OK | ||
OK | ||
OK | ||
OK | ||
OK | ||
OK | ||
OK | ||
OK | ||
OK |
Oops, something went wrong.