Files
Chamilo/vendor/yuloh/bccomp-polyfill/src/functions.php
T
Xes 73154ae174
Behat tests 1.11.x 🐞 / PHP 7.4 Test on ubuntu-latest (push) Canceled after 0s
PHP-CS-Fixer / composer_install (7.4) (push) Canceled after 0s
Chamilo 1.11.40 (ZIP oficial v1.11.40)
Fuente: https://github.com/chamilo/chamilo-lms/releases/download/v1.11.40/chamilo-1.11.40.zip
sha256: 1cf4bf2cc7bae1ef1a1eff643235db1d552f78ddf4b6dd1e2d2dac9868679439
Snapshot independiente (rama huerfana); diffable vs 1.11.38. vendor incluido.
2026-08-06 17:59:45 +02:00

103 lines
3.7 KiB
PHP

<?php
namespace Yuloh\BcCompPolyfill{
if (!function_exists('Yuloh\BcCompPolyfill\bccomp')) {
function bccomp($leftOperand, $rightOperand, $scale = 0)
{
$leftOperand = new BigNumber((string) $leftOperand);
$rightOperand = new BigNumber((string) $rightOperand);
// The real bccomp casts floats to int but throws a warning for anything else.
if (is_float($scale)) {
$scale = (int) $scale;
}
if (!is_int($scale)) {
trigger_error(
sprintf('bccomp() expects parameter 3 to be integer, %s given', gettype($scale)),
E_USER_WARNING
);
}
// If both numbers are zero, they are equal.
if ($leftOperand->isZero() && $rightOperand->isZero()) {
return 0;
}
// If one number is positive while the other is negative we can return early.
if ($leftOperand->isPositive() && $rightOperand->isNegative()) {
return 1;
}
if ($leftOperand->isNegative() && $rightOperand->isPositive()) {
return -1;
}
$isPositiveComparison = $leftOperand->isPositive() && $rightOperand->isPositive();
// If the part to the left of the decimal is longer it's the larger number.
if ($leftOperand->getCharacteristicLength() > $rightOperand->getCharacteristicLength()) {
return $isPositiveComparison ? 1 : -1;
}
if ($leftOperand->getCharacteristicLength() < $rightOperand->getCharacteristicLength()) {
return $isPositiveComparison ? -1 : 1;
}
// if the part to the left of the decimal is equal, we check each place for a larger number.
for ($i = 0; $i < $leftOperand->getCharacteristicLength(); $i++) {
if ($leftOperand->getCharacteristic()[$i] > $rightOperand->getCharacteristic()[$i]) {
return $isPositiveComparison ? 1 : -1;
}
if ($leftOperand->getCharacteristic()[$i] < $rightOperand->getCharacteristic()[$i]) {
return $isPositiveComparison ? -1 : 1;
}
}
// if there is a scale and we still haven't found the larger number,
// check each place to the right of the decimal place.
$leftMantissa = $leftOperand->getMantissa();
$rightMantissa = $rightOperand->getMantissa();
for ($i = 0; $i < $scale; $i++) {
// If we are still iterating and out of digits to compare,
// we can return early.
if (!isset($leftMantissa[$i]) && !isset($rightMantissa[$i])) {
return 0;
}
// If there isn't a digit in this decimal place, set it to 0.
if (!isset($leftMantissa[$i])) {
$leftMantissa = $leftMantissa . '0';
}
if (!isset($rightMantissa[$i])) {
$rightMantissa = $rightMantissa . '0';
}
if ($leftMantissa[$i] > $rightMantissa[$i]) {
return $isPositiveComparison ? 1 : -1;
}
if ($leftMantissa[$i] < $rightMantissa[$i]) {
return $isPositiveComparison ? -1 : 1;
}
}
return 0;
}
}
}
namespace {
if (!function_exists('bccomp')) {
function bccomp($leftOperand, $rightOperand, $scale = 0)
{
return \Yuloh\BcCompPolyfill\bccomp($leftOperand, $rightOperand, $scale);
}
}
}