brick/math
Arbitrary-precision math for PHP. Work with big integers, decimals and rationals reliably, with automatic acceleration via GMP or BCMath when available. PHP 8.2+ supported. Stable 0.x release cycles suitable for production.
💥 Breaking changes
BigDecimal::hasNonZeroFractionalPart() has been removed, use ! $number->getFractionalPart()->isZero() instead[@internal](https://github.com/internal)✨ Compatibility improvements
BigDecimal::fromFloatExact() now supports 32-bit PHP✨ New features
New methods to create a BigDecimal from a float value:
BigDecimal::fromFloatExact()BigDecimal::fromFloatShortest()👌 Improvements
[@return](https://github.com/return) non-empty-string to toString(), jsonSerialize() and __toString() (#111 by @vudaltsov)💥 Breaking changes
BigInteger::getLowestSetBit() now returns null instead of -1 when the number is zeroBigRational::simplified() has been removed, as it is now a no-op✨ New features
BigDecimal::getIntegralPart() returns the integral part as BigInteger (this method existed with a different signature in version 0.14, and was removed in 0.15)BigDecimal::getFractionalPart() returns the fractional part as BigDecimal (this method existed with a different signature and meaning in version 0.14, and was removed in 0.15)🗑️ Deprecations
BigDecimal::hasNonZeroFractionalPart() is deprecated, use ->getFractionalPart()->isZero() instead💥 Breaking changes
of() and arithmetic methods, use of((string) $float) to get the same behaviour as before (#105)BigRational is now always simplified to lowest terms: all operations, including of() and ofFraction(), now return a fraction in its simplest form (e.g. 2/3 instead of 4/6)BigDecimal::dividedBy() now requires the $scale parameterBigInteger::sqrt() and BigDecimal::sqrt() now default to RoundingMode::Unnecessary, explicitly pass RoundingMode::Down to get the previous behaviourBigInteger::mod() now uses Euclidean modulo semantics: the modulus must be strictly positive, and the result is always non-negative; this change aligns with Java's BigInteger.mod() behaviourBigInteger::mod(), modInverse() and modPow() now throw InvalidArgumentException (instead of NegativeNumberException) for negative modulus/exponent argumentsMathException is now an interface instead of a classBigDecimal::getPrecision() now returns 1 for zero valuesBigNumber::min(), max() and sum() now throw an ArgumentCountError when called with no arguments (previously threw InvalidArgumentException)BigInteger::randomBits() and randomRange() now throw RandomSourceException when random byte generation fails or returns invalid dataDeprecated API elements removed:
BigInteger::testBit() has been removed, use isBitSet() insteadBigInteger::gcdMultiple() has been removed, use gcdAll() insteadBigDecimal::exactlyDividedBy() has been removed, use dividedByExact() insteadBigDecimal::getIntegralPart() has been removed (will be re-introduced as returning BigInteger in 0.16)BigDecimal::getFractionalPart() has been removed (will be re-introduced as returning BigDecimal with a different meaning in 0.16)BigDecimal::stripTrailingZeros() has been removed, use strippedOfTrailingZeros() insteadBigRational::nd() has been removed, use ofFraction() insteadBigRational::quotient() has been removed, use getIntegralPart() insteadBigRational::remainder() has been removed, use $number->getNumerator()->remainder($number->getDenominator()) insteadBigRational::quotientAndRemainder() has been removed, use $number->getNumerator()->quotientAndRemainder($number->getDenominator()) insteadRoundingMode upper snake case constants (e.g. HALF_UP) have been removed, use the pascal case version (e.g. HalfUp) insteadThe following breaking changes only affect you if you're using named arguments:
BigInteger::mod() now uses $modulus as the parameter nameBigInteger::modInverse() now uses $modulus as the parameter nameBigInteger::modPow() now uses $exponent and $modulus as parameter namesBigInteger::shiftedLeft() now uses $bits as the parameter nameBigInteger::shiftedRight() now uses $bits as the parameter nameBigInteger::isBitSet() now uses $bitIndex as the parameter nameBigInteger::randomBits() now uses $bitCount as the parameter nameBigDecimal::withPointMovedLeft() now uses $places as the parameter nameBigDecimal::withPointMovedRight() now uses $places as the parameter nameThe following breaking changes are unlikely to affect you:
DivisionByZeroException::modulusMustNotBeZero() has been renamed to zeroModulus()DivisionByZeroException::denominatorMustNotBeZero() has been renamed to zeroDenominator()IntegerOverflowException::toIntOverflow() has been renamed to integerOutOfRange()RoundingNecessaryException::roundingNecessary() has been removed🗑️ Deprecations
BigRational::simplified() is deprecated, as it is now a no-op✨ New features
BigInteger::power(), BigDecimal::power() and BigRational::power() no longer enforce an exponent limitBigInteger::shiftedLeft() and BigInteger::shiftedRight() no longer enforce a limit on the number of bitsBigRational::power() now accepts negative exponentsInvalidArgumentException for invalid argument errorsNoInverseException for modular inverse errorsRandomSourceException for random source errors👌 Improvements
🗑️ Deprecations
BigInteger::testBit() is deprecated, use isBitSet() instead✨ New features
BigInteger::isBitSet() (replaces testBit())BigNumber::toString() (alias of magic method __toString())👌 Improvements
BigRational comparison methods[@throws](https://github.com/throws) annotations✨ New features
clamp() is now available on the base BigNumber class👌 Improvements
[@throws](https://github.com/throws) exception documentation🗑️ Deprecations
$scale to BigDecimal::dividedBy() is deprecated; $scale will be required in 0.15👌 Improvements
BigRational::toFloat() never returns NAN anymore🗑️ Deprecations
BigInteger::sqrt() and BigDecimal::sqrt() triggers a deprecation notice: the default rounding mode will change from Down to Unnecessary in 0.15✨ New features
BigInteger::sqrt() and BigDecimal::sqrt() now support roundingabs() and negated() methods are now available on the base BigNumber class👌 Improvements
BigInteger::(from|to)ArbitraryBase()BigNumber::ofNullable() is now marked as [@pure](https://github.com/pure)🗑️ Deprecations
BigInteger::mod() is deprecated to align with Euclidean modulo semantics; it will throw InvalidArgumentException in 0.15BigDecimal::stripTrailingZeros() is deprecated, use strippedOfTrailingZeros() instead✨ New features
BigInteger::modPow() now accepts negative basesBigDecimal::strippedOfTrailingZeros() (replaces stripTrailingZeros())👌 Improvements
clamp() methods are now marked as [@pure](https://github.com/pure)✨ New features
BigInteger::lcm()BigInteger::lcmAll()BigRational::toRepeatingDecimalString()🐛 Bug fixes
BigInteger::gcdAll() / gcdMultiple() could return a negative result when used with a single negative number🗑️ Deprecations
float values to of() or arithmetic methods is deprecated and will be removed in 0.15; cast to string explicitly to preserve the previous behaviour (#105)RoundingMode enum cases through upper snake case (e.g. HALF_UP) is deprecated, use the pascal case version (e.g. HalfUp) insteadBigInteger::gcdMultiple() is deprecated, use gcdAll() insteadBigDecimal::exactlyDividedBy() is deprecated, use dividedByExact() insteadBigDecimal::getIntegralPart() is deprecated (will be removed in 0.15, and re-introduced as returning BigInteger in 0.16)BigDecimal::getFractionalPart() is deprecated (will be removed in 0.15, and re-introduced as returning BigDecimal with a different meaning in 0.16)BigRational::nd() is deprecated, use ofFraction() insteadBigRational::quotient() is deprecated, use getIntegralPart() insteadBigRational::remainder() is deprecated, use $number->getNumerator()->remainder($number->getDenominator()) insteadBigRational::quotientAndRemainder() is deprecated, use $number->getNumerator()->quotientAndRemainder($number->getDenominator()) instead✨ New features
BigInteger::gcdAll() (replaces gcdMultiple())BigRational::clamp()BigRational::ofFraction() (replaces nd())BigRational::getIntegralPart() (replaces quotient())BigRational::getFractionalPart()👌 Improvements
BigInteger::modInverse() now accepts BigNumber|int|float|string instead of just BigIntegerBigInteger::gcdMultiple() now accepts BigNumber|int|float|string instead of just BigInteger🐛 Bug fixes
BigInteger::clamp() and BigDecimal::clamp() now throw an exception on inverted bounds, instead of returning an incorrect result✨ New features
BigNumber::ofNullable() (#94 by @mrkh995)✨ Compatibility fixes
✨ New features
BigInteger::clamp() and BigDecimal::clamp() (#96 by @JesterIruka)✨ Improvements
BigNumber classes are now marked as [@pure](https://github.com/pure) for better static analysis💥 Breaking changes
BigNumber classes are now readonlyBigNumber is now marked as sealed: it must not be extended outside of this packagefinal✨ Improvements
__toString() methods of BigInteger and BigDecimal are now type-hinted as returning numeric-string instead of string (#90 by @vudaltsov)💥 Breaking changes
BigDecimal::ofUnscaledValue() no longer throws an exception if the scale is negativeMathException now extends RuntimeException instead of Exception; this reverts the change introduced in version 0.11.0 (#82)✨ New features
BigDecimal::ofUnscaledValue() allows a negative scale (and converts the values to create a zero scale number)✨ New features
BigDecimal::getPrecision() returns the number of significant digits in a decimal number⚡️ Performance improvements
NativeCalculator is now faster for small divisors, thanks to @Izumi-kun in #87👌 Improvements
RoundingNecessaryException to the [@throws](https://github.com/throws) annotation of BigNumber::of()⚡️ Performance improvements
BigNumber::of() is now faster, thanks to @SebastienDug in #77.💥 Breaking changes
RoundingMode is now an enum; if you're type-hinting rounding modes, you need to type-hint against RoundingMode instead of int nowBigNumber classes do not implement the Serializable interface anymore (they use the new custom object serialization mechanism)BigNumber subclasses:
BigNumber::of() is now staticBigNumber has a new abstract method from()public and protected functions of BigNumber are now final💥 Breaking changes
MathException now extends Exception instead of RuntimeException* You may now run into type errors if you were passing Stringable objects to of() or any of the methods internally calling of(), with strict_types enabled. You can fix this by casting Stringable objects to string first.
👌 Improvements
BigRational::toFloat() now simplifies the fraction before performing division (#73) thanks to @olsavmic✨ New features
BigInteger::gcdMultiple() returns the GCD of multiple BigInteger numbers💥 Breaking changes
🚀 Compatibility with PHP 8.1
Serializable interface being deprecated (#60) thanks @TRowbotham:bug: Bug fix
bcscale(), on PHP >= 7.2 (#55).✨ New features
BigInteger::not() returns the bitwise NOT value🐛 Bug fixes
BigInteger::toBytes() could return an incorrect binary representation for some numbersand(), or(), xor() on BigInteger could return an incorrect result when the GMP extension is not available🐛 Bug fix
BigInteger::toBytes() could return an incorrect binary representation for some numbersand(), or(), xor() on BigInteger could return an incorrect result when the GMP extension is not available👌 Improvements
BigNumber::of() now accepts .123 and 123. formats, both of which return a BigDecimal💥 Breaking changes
BigInteger::powerMod() has been removed - use modPow() insteadBigInteger::parse() has been removed - use fromBase() instead🚑 Critical fix
BigInteger::parse() method, that has been removed by mistake in version 0.8.9 and should have lasted for the whole 0.8 release cycle.✨ New features
BigInteger::modInverse() calculates a modular multiplicative inverseBigInteger::fromBytes() creates a BigInteger from a byte stringBigInteger::toBytes() converts a BigInteger to a byte stringBigInteger::randomBits() creates a pseudo-random BigInteger of a given bit lengthBigInteger::randomRange() creates a pseudo-random BigInteger between two bounds💩 Deprecations
BigInteger::powerMod() is now deprecated in favour of modPow()🐛 Fixes
ext-json requirement, due to BigNumber implementing JsonSerializable⚡️ Optimizations
BigInteger::remainder()✨ New features
BigInteger::getLowestSetBit() returns the index of the rightmost one bit✨ New features
BigInteger::isEven() tests whether the number is evenBigInteger::isOdd() tests whether the number is oddBigInteger::testBit() tests if a bit is setBigInteger::getBitLength() returns the number of bits in the minimal representation of the number🛠️ Maintenance release
Classes are now annotated for better static analysis with psalm.
This is a maintenance release: no bug fixes, no new features, no breaking changes.
✨ New feature
BigInteger::powerMod() performs a power-with-modulo operation. Useful for crypto.
How can I help you explore Laravel packages today?