Actualización

This commit is contained in:
Xes
2025-04-10 12:24:57 +02:00
parent 8969cc929d
commit 45420b6f0d
39760 changed files with 4303286 additions and 0 deletions

View File

@@ -0,0 +1,146 @@
<?php
namespace Yuloh\BcCompPolyfill;
class BigNumber
{
/**
* @var string
*/
private $characteristic = '0';
/**
* @var int
*/
private $characteristicLength;
/**
* @var string
*/
private $mantissa = '';
/**
* @var boolean
*/
private $isNegative = false;
/**
* @param string $value
*/
public function __construct($value)
{
if (static::isNumeric($value)) {
$this->setValues($value);
}
}
/**
* @return boolean
*/
public function isNegative()
{
return $this->isNegative;
}
/**
* @return boolean
*/
public function isPositive()
{
return !$this->isNegative();
}
/**
* @return boolean
*/
public function isZero()
{
return $this->getCharacteristic() === '0' && $this->getMantissa() === '';
}
/**
* @return string
*/
public function getCharacteristic()
{
return $this->characteristic;
}
/**
* @return int
*/
public function getCharacteristicLength()
{
if (is_null($this->characteristicLength)) {
$this->characteristicLength = strlen($this->getCharacteristic());
}
return $this->characteristicLength;
}
/**
* @return string
*/
public function getMantissa()
{
return $this->mantissa;
}
/**
* @param string $value
*/
private function setValues($value)
{
if ($value[0] === '-') {
$this->isNegative = true;
$value = substr($value, 1);
}
$this->characteristic = static::parseCharacteristic($value);
$this->mantissa = static::parseMantissa($value);
}
/**
* @param string $value
* @return string
*/
private static function parseCharacteristic($value)
{
if (strpos($value, '.') !== false) {
$value = substr($value, 0, strpos($value, '.'));
}
return strlen($value) === 1 ? $value : ltrim($value, '0');
}
/**
* @param string $value
* @return string
*/
private static function parseMantissa($value)
{
if (($separatorPos = strrpos($value, '.')) === false) {
return '';
}
$value = substr($value, strrpos($value, '.') + 1);
return rtrim($value, '0');
}
/**
* @param string $value
* @return boolean
*/
private static function isNumeric($value)
{
// remove the last decimal separator only.
// If it has more decimal separators it's invalid.
$separatorPos = strrpos($value, '.');
if ($separatorPos !== false) {
$value = substr_replace($value, '', $separatorPos, 1);
}
return ctype_digit($value) || ($value[0] === '-' && ctype_digit(substr($value, 1)));
}
}

View File

@@ -0,0 +1,102 @@
<?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);
}
}
}