First Commit
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Brick\Math\Exception;
|
||||
|
||||
use RuntimeException;
|
||||
|
||||
/**
|
||||
* Exception thrown when a division by zero occurs.
|
||||
*/
|
||||
final class DivisionByZeroException extends RuntimeException implements MathException
|
||||
{
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
* @pure
|
||||
*/
|
||||
public function __construct(string $message)
|
||||
{
|
||||
parent::__construct($message);
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
* @pure
|
||||
*/
|
||||
public static function divisionByZero(): self
|
||||
{
|
||||
return new self('Division by zero.');
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
* @pure
|
||||
*/
|
||||
public static function zeroModulus(): self
|
||||
{
|
||||
return new self('The modulus must not be zero.');
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
* @pure
|
||||
*/
|
||||
public static function zeroDenominator(): self
|
||||
{
|
||||
return new self('The denominator of a rational number must not be zero.');
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
* @pure
|
||||
*/
|
||||
public static function reciprocalOfZero(): self
|
||||
{
|
||||
return new self('The reciprocal of zero is undefined.');
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
* @pure
|
||||
*/
|
||||
public static function zeroToNegativePower(): self
|
||||
{
|
||||
return new self('Cannot raise zero to a negative power.');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Brick\Math\Exception;
|
||||
|
||||
use Brick\Math\BigInteger;
|
||||
use RuntimeException;
|
||||
|
||||
use function sprintf;
|
||||
|
||||
use const PHP_INT_MAX;
|
||||
use const PHP_INT_MIN;
|
||||
|
||||
/**
|
||||
* Exception thrown when a native integer overflow occurs.
|
||||
*/
|
||||
final class IntegerOverflowException extends RuntimeException implements MathException
|
||||
{
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
* @pure
|
||||
*/
|
||||
public function __construct(string $message)
|
||||
{
|
||||
parent::__construct($message);
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
* @pure
|
||||
*/
|
||||
public static function integerOutOfRange(BigInteger $value): self
|
||||
{
|
||||
$message = '%s is out of range [%d, %d] and cannot be represented as an integer.';
|
||||
|
||||
return new self(sprintf($message, $value->toString(), PHP_INT_MIN, PHP_INT_MAX));
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
* @pure
|
||||
*/
|
||||
public static function nativeIntegerOverflow(string $expression): self
|
||||
{
|
||||
return new self(sprintf(
|
||||
'Cannot compute %s because the result is outside the native integer range [%d, %d].',
|
||||
$expression,
|
||||
PHP_INT_MIN,
|
||||
PHP_INT_MAX,
|
||||
));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,133 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Brick\Math\Exception;
|
||||
|
||||
use function sprintf;
|
||||
|
||||
/**
|
||||
* Exception thrown when an invalid argument is provided.
|
||||
*/
|
||||
final class InvalidArgumentException extends \InvalidArgumentException implements MathException
|
||||
{
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
* @pure
|
||||
*/
|
||||
public function __construct(string $message)
|
||||
{
|
||||
parent::__construct($message);
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
* @pure
|
||||
*/
|
||||
public static function baseOutOfRange(int $base): self
|
||||
{
|
||||
return new self(sprintf('Base %d is out of range [2, 36].', $base));
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
* @pure
|
||||
*/
|
||||
public static function negativeScale(): self
|
||||
{
|
||||
return new self('The scale must not be negative.');
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
* @pure
|
||||
*/
|
||||
public static function negativeBitIndex(): self
|
||||
{
|
||||
return new self('The bit index must not be negative.');
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
* @pure
|
||||
*/
|
||||
public static function negativeBitCount(): self
|
||||
{
|
||||
return new self('The bit count must not be negative.');
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
* @pure
|
||||
*/
|
||||
public static function alphabetTooShort(): self
|
||||
{
|
||||
return new self('The alphabet must contain at least 2 characters.');
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
* @pure
|
||||
*/
|
||||
public static function duplicateCharsInAlphabet(): self
|
||||
{
|
||||
return new self('The alphabet must not contain duplicate characters.');
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
* @pure
|
||||
*/
|
||||
public static function minGreaterThanMax(): self
|
||||
{
|
||||
return new self('The minimum value must be less than or equal to the maximum value.');
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
* @pure
|
||||
*/
|
||||
public static function cannotConvertFloat(string $type): self
|
||||
{
|
||||
return new self(sprintf('Cannot convert %s to a BigDecimal.', $type));
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
* @pure
|
||||
*/
|
||||
public static function negativeExponent(): self
|
||||
{
|
||||
return new self('The exponent must not be negative.');
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
* @pure
|
||||
*/
|
||||
public static function negativeModulus(): self
|
||||
{
|
||||
return new self('The modulus must not be negative.');
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
* @pure
|
||||
*/
|
||||
public static function nonPositiveNthRootDegree(): self
|
||||
{
|
||||
return new self('The degree of an nth root must be a positive integer.');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Brick\Math\Exception;
|
||||
|
||||
use Throwable;
|
||||
|
||||
/**
|
||||
* Base interface for all math exceptions.
|
||||
*/
|
||||
interface MathException extends Throwable
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Brick\Math\Exception;
|
||||
|
||||
use RuntimeException;
|
||||
|
||||
/**
|
||||
* Exception thrown when attempting to perform an unsupported operation, such as a square root, on a negative number.
|
||||
*/
|
||||
final class NegativeNumberException extends RuntimeException implements MathException
|
||||
{
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
* @pure
|
||||
*/
|
||||
public function __construct(string $message)
|
||||
{
|
||||
parent::__construct($message);
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
* @pure
|
||||
*/
|
||||
public static function squareRootOfNegativeNumber(): self
|
||||
{
|
||||
return new self('Cannot calculate the square root of a negative number.');
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
* @pure
|
||||
*/
|
||||
public static function nthRootOfNegativeNumber(): self
|
||||
{
|
||||
return new self('Cannot take an even nth root of a negative number.');
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
* @pure
|
||||
*/
|
||||
public static function toArbitraryBaseOfNegativeNumber(): self
|
||||
{
|
||||
return new self('Cannot convert a negative number to an arbitrary base.');
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
* @pure
|
||||
*/
|
||||
public static function unsignedBytesOfNegativeNumber(): self
|
||||
{
|
||||
return new self('Cannot convert a negative number to a byte string in unsigned mode.');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Brick\Math\Exception;
|
||||
|
||||
use RuntimeException;
|
||||
|
||||
/**
|
||||
* Exception thrown when attempting to compute a modular inverse that does not exist.
|
||||
*/
|
||||
final class NoInverseException extends RuntimeException implements MathException
|
||||
{
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
* @pure
|
||||
*/
|
||||
public function __construct(string $message)
|
||||
{
|
||||
parent::__construct($message);
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
* @pure
|
||||
*/
|
||||
public static function noModularInverse(): self
|
||||
{
|
||||
return new self('This number has no multiplicative inverse modulo the given modulus (they are not coprime).');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,120 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Brick\Math\Exception;
|
||||
|
||||
use RuntimeException;
|
||||
|
||||
use function dechex;
|
||||
use function ord;
|
||||
use function sprintf;
|
||||
use function strtoupper;
|
||||
|
||||
/**
|
||||
* Exception thrown when attempting to create a number from a string with an invalid format.
|
||||
*/
|
||||
final class NumberFormatException extends RuntimeException implements MathException
|
||||
{
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
* @pure
|
||||
*/
|
||||
public function __construct(string $message)
|
||||
{
|
||||
parent::__construct($message);
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
* @pure
|
||||
*/
|
||||
public static function invalidFormat(string $value): self
|
||||
{
|
||||
return new self(sprintf(
|
||||
'Value "%s" does not represent a valid number.',
|
||||
$value,
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
* @param string $char The failing character.
|
||||
*
|
||||
* @pure
|
||||
*/
|
||||
public static function charNotInAlphabet(string $char): self
|
||||
{
|
||||
return new self(sprintf(
|
||||
'Character %s is not valid in the given alphabet.',
|
||||
self::charToString($char),
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
* @pure
|
||||
*/
|
||||
public static function charNotValidInBase(string $char, int $base): self
|
||||
{
|
||||
return new self(sprintf(
|
||||
'Character %s is not valid in base %d.',
|
||||
self::charToString($char),
|
||||
$base,
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
* @pure
|
||||
*/
|
||||
public static function emptyNumber(): self
|
||||
{
|
||||
return new self('The number must not be empty.');
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
* @pure
|
||||
*/
|
||||
public static function emptyByteString(): self
|
||||
{
|
||||
return new self('The byte string must not be empty.');
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
* @pure
|
||||
*/
|
||||
public static function exponentTooLarge(): self
|
||||
{
|
||||
return new self('The exponent is too large to be represented as an integer.');
|
||||
}
|
||||
|
||||
/**
|
||||
* @pure
|
||||
*/
|
||||
private static function charToString(string $char): string
|
||||
{
|
||||
$ord = ord($char);
|
||||
|
||||
if ($ord < 32 || $ord > 126) {
|
||||
$char = strtoupper(dechex($ord));
|
||||
|
||||
if ($ord < 16) {
|
||||
$char = '0' . $char;
|
||||
}
|
||||
|
||||
return '0x' . $char;
|
||||
}
|
||||
|
||||
return '"' . $char . '"';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Brick\Math\Exception;
|
||||
|
||||
use RuntimeException;
|
||||
use Throwable;
|
||||
|
||||
use function get_debug_type;
|
||||
use function sprintf;
|
||||
|
||||
/**
|
||||
* Exception thrown when random byte generation fails.
|
||||
*/
|
||||
final class RandomSourceException extends RuntimeException implements MathException
|
||||
{
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
* @pure
|
||||
*/
|
||||
public function __construct(string $message, ?Throwable $previous = null)
|
||||
{
|
||||
parent::__construct($message, 0, $previous);
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
* @pure
|
||||
*/
|
||||
public static function randomSourceFailure(Throwable $previous): self
|
||||
{
|
||||
return new self('Random byte generation failed.', $previous);
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
* @pure
|
||||
*/
|
||||
public static function invalidRandomBytesType(mixed $value): self
|
||||
{
|
||||
return new self(sprintf(
|
||||
'The random bytes generator must return a string, got %s.',
|
||||
get_debug_type($value),
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
* @pure
|
||||
*/
|
||||
public static function invalidRandomBytesLength(int $expectedLength, int $actualLength): self
|
||||
{
|
||||
return new self(sprintf(
|
||||
'The random bytes generator returned %d byte(s), expected %d.',
|
||||
$actualLength,
|
||||
$expectedLength,
|
||||
));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,163 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Brick\Math\Exception;
|
||||
|
||||
use RuntimeException;
|
||||
|
||||
/**
|
||||
* Exception thrown when a number cannot be represented at the requested scale without rounding.
|
||||
*/
|
||||
final class RoundingNecessaryException extends RuntimeException implements MathException
|
||||
{
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
* @pure
|
||||
*/
|
||||
public function __construct(string $message)
|
||||
{
|
||||
parent::__construct($message);
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
* @pure
|
||||
*/
|
||||
public static function decimalScaleTooSmall(): self
|
||||
{
|
||||
return new self('This decimal number cannot be represented at the requested scale without rounding.');
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
* @pure
|
||||
*/
|
||||
public static function rationalScaleTooSmall(): self
|
||||
{
|
||||
return new self('This rational number cannot be represented at the requested scale without rounding.');
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
* @pure
|
||||
*/
|
||||
public static function integerDivisionNotExact(): self
|
||||
{
|
||||
return new self('The division has a non-zero remainder and cannot be represented as an integer without rounding.');
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
* @pure
|
||||
*/
|
||||
public static function decimalDivisionNotExact(): self
|
||||
{
|
||||
return new self('The division yields a non-terminating decimal expansion and cannot be represented as a decimal without rounding.');
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
* @pure
|
||||
*/
|
||||
public static function decimalDivisionScaleTooSmall(): self
|
||||
{
|
||||
return new self('The division result is exact but cannot be represented at the requested scale without rounding.');
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
* @pure
|
||||
*/
|
||||
public static function integerSquareRootNotExact(): self
|
||||
{
|
||||
return new self('The square root is not exact and cannot be represented as an integer without rounding.');
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
* @pure
|
||||
*/
|
||||
public static function decimalSquareRootNotExact(): self
|
||||
{
|
||||
return new self('The square root is not exact and cannot be represented as a decimal without rounding.');
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
* @pure
|
||||
*/
|
||||
public static function decimalSquareRootScaleTooSmall(): self
|
||||
{
|
||||
return new self('The square root is exact but cannot be represented at the requested scale without rounding.');
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
* @pure
|
||||
*/
|
||||
public static function integerNthRootNotExact(): self
|
||||
{
|
||||
return new self('The nth root is not exact and cannot be represented as an integer without rounding.');
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
* @pure
|
||||
*/
|
||||
public static function decimalNthRootNotExact(): self
|
||||
{
|
||||
return new self('The nth root is not exact and cannot be represented as a decimal without rounding.');
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
* @pure
|
||||
*/
|
||||
public static function decimalNthRootScaleTooSmall(): self
|
||||
{
|
||||
return new self('The nth root is exact but cannot be represented at the requested scale without rounding.');
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
* @pure
|
||||
*/
|
||||
public static function decimalNotConvertibleToInteger(): self
|
||||
{
|
||||
return new self('This decimal number cannot be represented as an integer without rounding.');
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
* @pure
|
||||
*/
|
||||
public static function rationalNotConvertibleToInteger(): self
|
||||
{
|
||||
return new self('This rational number cannot be represented as an integer without rounding.');
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
* @pure
|
||||
*/
|
||||
public static function rationalNotConvertibleToDecimal(): self
|
||||
{
|
||||
return new self('This rational number has a non-terminating decimal expansion and cannot be represented as a decimal without rounding.');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Brick\Math\Exception;
|
||||
|
||||
use RuntimeException;
|
||||
|
||||
/**
|
||||
* Exception thrown when the current PHP platform does not support a required feature.
|
||||
*/
|
||||
final class UnsupportedPlatformException extends RuntimeException implements MathException
|
||||
{
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
* @pure
|
||||
*/
|
||||
public function __construct(string $message)
|
||||
{
|
||||
parent::__construct($message);
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
* @pure
|
||||
*/
|
||||
public static function unsupportedFloatFormat(): self
|
||||
{
|
||||
return new self('Unsupported float format: expected IEEE-754 double.');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user