brick/math
's direct dependencies. Data on all dependencies, including transitive ones, is available via CSV download.Name | Version | Size | License | Type | Vulnerabilities |
---|
The Brick\Math Composer package is an incredibly versatile PHP library used to manage arbitrary precision numbers. With heavy-duty calculation power and extensive support for several data types, it's an essential tool especially beneficial in handling large number computations, where standard PHP integers may overflow. The Brick\Math library tools deliver strict precision without any information loss.
To use Brick\Math in PHP, you need to start by installing it through Composer, a tool for dependency management in PHP. The command for installing Brick/Math via composer is: composer require brick/math
.
Once installed, you can create instances of BigInteger, BigDecimal, and BigRational using the of()
factory method. Here's an example of how you can use it:
use Brick\Math\BigInteger;
$integer = BigInteger::of(123);
$squared = $integer->multipliedBy($integer); // Squaring the number.
echo $squared; // 15129
From the above code snippet, we are importing the BigInteger class and then creating an instance from the value of 123. We then perform arithmetic operations on this object to square the number.
Classes from Brick\Math library are immutable, meaning, whenever operations like plus()
, minus()
, or multipliedBy()
are performed, they always return a new instance and leave the original BigInteger, BigDecimal, or BigRational object unaffected.
The library also supports chaining method calls for better readability. Here's an example of how to achieve this:
use Brick\Math\BigInteger;
$integer = BigInteger::of(10);
echo $integer->plus(5)->multipliedBy(3); // Outputs: 45
This above snippet adds 5 to the number and then multiplies the result by 3.
As for the Brick\Math package documentation, you can refer to the README provided on GitHub at https://github.com/brick/math. It covers everything from installation to usage with detailed examples, including arithmetic operations, rounding methods, bitwise operations, and serialization techniques. In addition, the repository also provides direct links to the classes for BigInteger, BigDecimal, BigRational, and RoundingMode, as well as clearly defined exception classes that fall under the Brick\Math\Exception namespace.