Depend on brick/math for arbitrary-precision math

This commit is contained in:
Ben Ramsey
2020-01-09 13:04:50 -06:00
parent 07fc6b8f6f
commit cd03f39e9c
6 changed files with 374 additions and 3 deletions
+4
View File
@@ -44,6 +44,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
improved type-safety when dealing with arbitrary string values.
* Introduce `Math\CalculatorInterface` for representing calculators to perform
arithmetic operations on integers.
* Depend on [brick/math](https://github.com/brick/math) for the
`Math\BrickMathCalculator`, which is the default calculator used by this
library when math cannot be performed in native PHP due to integer size
limitations. The calculator is configurable and may be changed, if desired.
### Changed
+1
View File
@@ -12,6 +12,7 @@
"require": {
"php": "^7.2 | ^8",
"ext-json": "*",
"brick/math": "^0.8",
"symfony/polyfill-ctype": "^1.8"
},
"require-dev": {
+138
View File
@@ -0,0 +1,138 @@
<?php
/**
* This file is part of the ramsey/uuid library
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @copyright Copyright (c) Ben Ramsey <ben@benramsey.com>
* @license http://opensource.org/licenses/MIT MIT
*/
declare(strict_types=1);
namespace Ramsey\Uuid\Math;
use Brick\Math\BigInteger;
use Brick\Math\Exception\MathException;
use Brick\Math\RoundingMode as BrickMathRounding;
use Ramsey\Uuid\Exception\InvalidArgumentException;
use Ramsey\Uuid\Type\Hexadecimal;
use Ramsey\Uuid\Type\IntegerValue;
/**
* A calculator using the brick/math library for arbitrary-precision arithmetic
*
* @psalm-immutable
*/
final class BrickMathCalculator implements CalculatorInterface
{
private const ROUNDING_MODE_MAP = [
RoundingMode::UNNECESSARY => BrickMathRounding::UNNECESSARY,
RoundingMode::UP => BrickMathRounding::UP,
RoundingMode::DOWN => BrickMathRounding::DOWN,
RoundingMode::CEILING => BrickMathRounding::CEILING,
RoundingMode::FLOOR => BrickMathRounding::FLOOR,
RoundingMode::HALF_UP => BrickMathRounding::HALF_UP,
RoundingMode::HALF_DOWN => BrickMathRounding::HALF_DOWN,
RoundingMode::HALF_CEILING => BrickMathRounding::HALF_CEILING,
RoundingMode::HALF_FLOOR => BrickMathRounding::HALF_FLOOR,
RoundingMode::HALF_EVEN => BrickMathRounding::HALF_EVEN,
];
public function add(IntegerValue $augend, IntegerValue ...$addends): IntegerValue
{
/** @psalm-suppress ImpureMethodCall */
$sum = BigInteger::of($augend->toString());
foreach ($addends as $addend) {
/** @psalm-suppress ImpureMethodCall */
$sum = $sum->plus($addend->toString());
}
return new IntegerValue((string) $sum);
}
public function subtract(IntegerValue $minuend, IntegerValue ...$subtrahends): IntegerValue
{
/** @psalm-suppress ImpureMethodCall */
$difference = BigInteger::of($minuend->toString());
foreach ($subtrahends as $subtrahend) {
/** @psalm-suppress ImpureMethodCall */
$difference = $difference->minus($subtrahend->toString());
}
return new IntegerValue((string) $difference);
}
public function multiply(IntegerValue $multiplicand, IntegerValue ...$multipliers): IntegerValue
{
/** @psalm-suppress ImpureMethodCall */
$product = BigInteger::of($multiplicand->toString());
foreach ($multipliers as $multiplier) {
/** @psalm-suppress ImpureMethodCall */
$product = $product->multipliedBy($multiplier->toString());
}
return new IntegerValue((string) $product);
}
public function divide(int $roundingMode, IntegerValue $dividend, IntegerValue ...$divisors): IntegerValue
{
$brickRounding = $this->getBrickRoundingMode($roundingMode);
/** @psalm-suppress ImpureMethodCall */
$quotient = BigInteger::of($dividend->toString());
foreach ($divisors as $divisor) {
/** @psalm-suppress ImpureMethodCall */
$quotient = $quotient->dividedBy($divisor->toString(), $brickRounding);
}
return new IntegerValue((string) $quotient);
}
public function fromBase(string $value, int $base): IntegerValue
{
try {
/** @psalm-suppress ImpureMethodCall */
return new IntegerValue((string) BigInteger::fromBase($value, $base));
} catch (MathException $exception) {
throw new InvalidArgumentException(
$exception->getMessage(),
(int) $exception->getCode(),
$exception
);
}
}
public function toBase(IntegerValue $value, int $base): string
{
try {
/** @psalm-suppress ImpureMethodCall */
return BigInteger::of($value->toString())->toBase($base);
} catch (MathException $exception) {
throw new InvalidArgumentException(
$exception->getMessage(),
(int) $exception->getCode(),
$exception
);
}
}
public function toHexadecimal(IntegerValue $value): Hexadecimal
{
return new Hexadecimal($this->toBase($value, 16));
}
/**
* Maps ramsey/uuid rounding modes to those used by brick/math
*/
private function getBrickRoundingMode(int $roundingMode): int
{
return (int) self::ROUNDING_MODE_MAP[$roundingMode] ?? 0;
}
}
+4 -3
View File
@@ -55,16 +55,17 @@ interface CalculatorInterface
public function multiply(IntegerValue $multiplicand, IntegerValue ...$multipliers): IntegerValue;
/**
* Returns the quotient of all the provided parameters divided left-to-right
* Returns the quotient of the provided parameters divided left-to-right
*
* @param int $roundingMode The RoundingMode constant to use for this operation
* @param IntegerValue $dividend The integer to be divided
* @param IntegerValue ...$divisors The integers to divide the dividend, in
* the order in which the division operations should take place
* (left-to-right)
*
* @return IntegerValue The quotient of dividing all the provided parameters left-to-right
* @return IntegerValue The quotient of dividing the provided parameters left-to-right
*/
public function divide(IntegerValue $dividend, IntegerValue ...$divisors): IntegerValue;
public function divide(int $roundingMode, IntegerValue $dividend, IntegerValue ...$divisors): IntegerValue;
/**
* Converts a value from an arbitrary base to a base-10 integer value
+132
View File
@@ -0,0 +1,132 @@
<?php
/**
* This file was originally part of brick/math
*
* Copyright (c) 2013-present Benjamin Morel
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* @link https://github.com/brick/math brick/math at GitHub
*/
declare(strict_types=1);
namespace Ramsey\Uuid\Math;
/**
* Specifies a rounding behavior for numerical operations capable of discarding precision.
*
* Each rounding mode indicates how the least significant returned digit of a rounded result
* is to be calculated. If fewer digits are returned than the digits needed to represent the
* exact numerical result, the discarded digits will be referred to as the discarded fraction
* regardless the digits' contribution to the value of the number. In other words, considered
* as a numerical value, the discarded fraction could have an absolute value greater than one.
*/
final class RoundingMode
{
/**
* Private constructor. This class is not instantiable.
*
* @codeCoverageIgnore
*/
private function __construct()
{
}
/**
* Asserts that the requested operation has an exact result, hence no rounding is necessary.
*
* If this rounding mode is specified on an operation that yields a result that
* cannot be represented at the requested scale, a RoundingNecessaryException is thrown.
*/
public const UNNECESSARY = 0;
/**
* Rounds away from zero.
*
* Always increments the digit prior to a nonzero discarded fraction.
* Note that this rounding mode never decreases the magnitude of the calculated value.
*/
public const UP = 1;
/**
* Rounds towards zero.
*
* Never increments the digit prior to a discarded fraction (i.e., truncates).
* Note that this rounding mode never increases the magnitude of the calculated value.
*/
public const DOWN = 2;
/**
* Rounds towards positive infinity.
*
* If the result is positive, behaves as for UP; if negative, behaves as for DOWN.
* Note that this rounding mode never decreases the calculated value.
*/
public const CEILING = 3;
/**
* Rounds towards negative infinity.
*
* If the result is positive, behave as for DOWN; if negative, behave as for UP.
* Note that this rounding mode never increases the calculated value.
*/
public const FLOOR = 4;
/**
* Rounds towards "nearest neighbor" unless both neighbors are equidistant, in which case round up.
*
* Behaves as for UP if the discarded fraction is >= 0.5; otherwise, behaves as for DOWN.
* Note that this is the rounding mode commonly taught at school.
*/
public const HALF_UP = 5;
/**
* Rounds towards "nearest neighbor" unless both neighbors are equidistant, in which case round down.
*
* Behaves as for UP if the discarded fraction is > 0.5; otherwise, behaves as for DOWN.
*/
public const HALF_DOWN = 6;
/**
* Rounds towards "nearest neighbor" unless both neighbors are equidistant, in which case round towards positive infinity.
*
* If the result is positive, behaves as for HALF_UP; if negative, behaves as for HALF_DOWN.
*/
public const HALF_CEILING = 7;
/**
* Rounds towards "nearest neighbor" unless both neighbors are equidistant, in which case round towards negative infinity.
*
* If the result is positive, behaves as for HALF_DOWN; if negative, behaves as for HALF_UP.
*/
public const HALF_FLOOR = 8;
/**
* Rounds towards the "nearest neighbor" unless both neighbors are equidistant, in which case rounds towards the even neighbor.
*
* Behaves as for HALF_UP if the digit to the left of the discarded fraction is odd;
* behaves as for HALF_DOWN if it's even.
*
* Note that this is the rounding mode that statistically minimizes
* cumulative error when applied repeatedly over a sequence of calculations.
* It is sometimes known as "Banker's rounding", and is chiefly used in the USA.
*/
public const HALF_EVEN = 9;
}
+95
View File
@@ -0,0 +1,95 @@
<?php
declare(strict_types=1);
namespace Ramsey\Uuid\Test\Math;
use Ramsey\Uuid\Math\BrickMathCalculator;
use Ramsey\Uuid\Math\RoundingMode;
use Ramsey\Uuid\Test\TestCase;
use Ramsey\Uuid\Type\Hexadecimal;
use Ramsey\Uuid\Type\IntegerValue;
class BrickMathCalculatorTest extends TestCase
{
public function testAdd()
{
$int1 = new IntegerValue(5);
$int2 = new IntegerValue(6);
$int3 = new IntegerValue(7);
$calculator = new BrickMathCalculator();
$result = $calculator->add($int1, $int2, $int3);
$this->assertSame('18', $result->toString());
}
public function testSubtract()
{
$int1 = new IntegerValue(5);
$int2 = new IntegerValue(6);
$int3 = new IntegerValue(7);
$calculator = new BrickMathCalculator();
$result = $calculator->subtract($int1, $int2, $int3);
$this->assertSame('-8', $result->toString());
}
public function testMultiply()
{
$int1 = new IntegerValue(5);
$int2 = new IntegerValue(6);
$int3 = new IntegerValue(7);
$calculator = new BrickMathCalculator();
$result = $calculator->multiply($int1, $int2, $int3);
$this->assertSame('210', $result->toString());
}
public function testDivide()
{
$int1 = new IntegerValue(1023);
$int2 = new IntegerValue(6);
$int3 = new IntegerValue(7);
$calculator = new BrickMathCalculator();
$result = $calculator->divide(RoundingMode::HALF_UP, $int1, $int2, $int3);
$this->assertSame('24', $result->toString());
}
public function testFromBase()
{
$calculator = new BrickMathCalculator();
$result = $calculator->fromBase('ffffffffffffffffffff', 16);
$this->assertInstanceOf(IntegerValue::class, $result);
$this->assertSame('1208925819614629174706175', $result->toString());
}
public function testToBase()
{
$intValue = new IntegerValue('1208925819614629174706175');
$calculator = new BrickMathCalculator();
$this->assertSame('ffffffffffffffffffff', $calculator->toBase($intValue, 16));
}
public function testToHexadecimal()
{
$intValue = new IntegerValue('1208925819614629174706175');
$calculator = new BrickMathCalculator();
$result = $calculator->toHexadecimal($intValue);
$this->assertInstanceOf(Hexadecimal::class, $result);
$this->assertSame('ffffffffffffffffffff', $result->toString());
}
}