### Install Brick\Math PHP Library with Composer Source: https://github.com/brick/math/blob/master/README.md This snippet demonstrates how to install the Brick\Math library using Composer. It requires Composer to be installed and configured on your system. The command adds the library as a dependency to your project, allowing you to use arbitrary precision numbers in PHP. ```Bash composer require brick/math ``` -------------------------------- ### Brick\Math PHP Library API Overview Source: https://github.com/brick/math/blob/master/README.md This section provides an overview of the core classes and exceptions available in the Brick\Math PHP library. It details the main classes for arbitrary-precision numbers and the specific exceptions that can be thrown during operations. This serves as a quick reference for the library's public API. ```APIDOC Classes: - Brick\Math\BigNumber: Description: Base class for BigInteger, BigDecimal and BigRational. - Brick\Math\BigInteger: Description: Represents an arbitrary-precision integer number. - Brick\Math\BigDecimal: Description: Represents an arbitrary-precision decimal number. - Brick\Math\BigRational: Description: Represents an arbitrary-precision rational number (fraction). - Brick\Math\RoundingMode: Description: Enum representing all available rounding modes. Exceptions: - Brick\Math\Exception\MathException: Description: Base class for all exceptions. - Brick\Math\Exception\DivisionByZeroException: Description: Thrown when a division by zero occurs. - Brick\Math\Exception\IntegerOverflowException: Description: Thrown when attempting to convert a too large BigInteger to int. - Brick\Math\Exception\NumberFormatException: Description: Thrown when parsing a number string in an invalid format. - Brick\Math\Exception\RoundingNecessaryException: Description: Thrown when the result of the operation cannot be represented without explicit rounding. - Brick\Math\Exception\NegativeNumberException: Description: Thrown when attempting to calculate the square root of a negative number. ``` -------------------------------- ### Instantiate Brick Math BigNumber Classes in PHP Source: https://github.com/brick/math/blob/master/README.md Learn how to create instances of BigInteger, BigDecimal, and BigRational using their of() factory methods. These methods accept BigNumber instances, int, float, or string representations, ensuring safe and precise number initialization. Be aware of potential precision loss when using native int or float types for very large or precise numbers. ```PHP BigInteger::of(123546); BigInteger::of('9999999999999999999999999999999999999999999'); BigDecimal::of(1.2); BigDecimal::of('9.99999999999999999999999999999999999999999999'); BigRational::of('2/3'); BigRational::of('1.1'); // 11/10 BigInteger::of('1.00'); // 1 BigInteger::of('1.01'); // RoundingNecessaryException BigDecimal::of('1/8'); // 0.125 BigDecimal::of('1/3'); // RoundingNecessaryException echo BigInteger::of(999999999999999999999); // 1000000000000000000000 echo BigInteger::of('999999999999999999999'); // 999999999999999999999 echo BigDecimal::of(1.99999999999999999999); // 2 echo BigDecimal::of('1.99999999999999999999'); // 1.99999999999999999999 ``` -------------------------------- ### Perform Division and Rounding with BigInteger in PHP Source: https://github.com/brick/math/blob/master/README.md Learn how to divide BigInteger instances. By default, dividedBy() requires an exact division or throws an exception. You can specify a RoundingMode to handle non-exact divisions. Methods for quotient(), remainder(), and quotientAndRemainder() are also available for specific division results. ```PHP echo BigInteger::of(999)->dividedBy(3); // 333 echo BigInteger::of(1000)->dividedBy(3); // RoundingNecessaryException echo BigInteger::of(1000)->dividedBy(3, RoundingMode::DOWN); // 333 echo BigInteger::of(1000)->dividedBy(3, RoundingMode::UP); // 334 echo BigInteger::of(1000)->quotient(3); // 333 echo BigInteger::of(1000)->remainder(3); // 1 [$quotient, $remainder] = BigInteger::of(1000)->quotientAndRemainder(3); ``` -------------------------------- ### Serialize and Unserialize BigNumber Classes in PHP Source: https://github.com/brick/math/blob/master/README.md Explains the robust serialization capabilities of BigInteger, BigDecimal, and BigRational classes. These classes can be safely serialized on one machine and unserialized on another, even if the PHP extension environment differs (e.g., GMP support). This ensures data portability and integrity across various deployments. ```APIDOC BigInteger, BigDecimal and BigRational can be safely serialized on a machine and unserialized on another, even if these machines do not share the same set of PHP extensions. For example, serializing on a machine with GMP support and unserializing on a machine that does not have this extension installed will still work as expected. ``` -------------------------------- ### Use Flexible Parameter Types in Brick Math Methods (PHP) Source: https://github.com/brick/math/blob/master/README.md Discover that arithmetic methods like plus(), minus(), and multipliedBy() accept the same flexible input types as the of() factory method. This allows for seamless operations with BigNumber instances, int, float, or string values, as long as the conversion to the current BigNumber type is safe. ```PHP $integer = BigInteger::of(123); $integer->multipliedBy(123); $integer->multipliedBy('123'); $integer->multipliedBy($integer); echo BigInteger::of(2)->multipliedBy(BigDecimal::of('2.0')); // 4 echo BigInteger::of(2)->multipliedBy(BigDecimal::of('2.5')); // RoundingNecessaryException echo BigDecimal::of(2.5)->multipliedBy(BigInteger::of(2)); // 5.0 ``` -------------------------------- ### Perform Bitwise Operations with BigInteger in PHP Source: https://github.com/brick/math/blob/master/README.md Lists the available bitwise operations for the BigInteger class, including logical operations like AND, OR, XOR, NOT, and bit shifting methods. These operations allow for precise manipulation of large integer values at the bit level. ```APIDOC - `and()` - `or()` - `xor()` - `not()` - `shiftedLeft()` - `shiftedRight()` ``` -------------------------------- ### Handle Division and Rounding with BigDecimal in PHP Source: https://github.com/brick/math/blob/master/README.md Understand how to perform division with BigDecimal, which always requires a specified scale. If the exact result doesn't fit the scale, a RoundingMode must be provided. For divisions yielding a finite number of decimal places, exactlyDividedBy() can automatically determine the scale or throw an exception for infinite repeating decimals. ```PHP echo BigDecimal::of(1)->dividedBy('8', 3); // 0.125 echo BigDecimal::of(1)->dividedBy('8', 2); // RoundingNecessaryException echo BigDecimal::of(1)->dividedBy('8', 2, RoundingMode::HALF_DOWN); // 0.12 echo BigDecimal::of(1)->dividedBy('8', 2, RoundingMode::HALF_UP); // 0.13 echo BigDecimal::of(1)->exactlyDividedBy(256); // 0.00390625 echo BigDecimal::of(1)->exactlyDividedBy(11); // RoundingNecessaryException ``` -------------------------------- ### Perform Division with BigRational in PHP Source: https://github.com/brick/math/blob/master/README.md Demonstrates how to perform division operations using the BigRational class. This method ensures exact representation of the division result, preventing floating-point inaccuracies. It supports division by both integers and other BigRational instances. ```php echo BigRational::of('123/456')->dividedBy('7'); // 123/3192 echo BigRational::of('123/456')->dividedBy('9/8'); // 984/4104 ``` -------------------------------- ### Chain Operations with Immutable Brick Math Objects in PHP Source: https://github.com/brick/math/blob/master/README.md Understand that BigInteger, BigDecimal, and BigRational objects are immutable; methods like plus() or multipliedBy() return new instances. This design ensures safe object passing and allows for method chaining, improving code readability and conciseness. ```PHP $ten = BigInteger::of(10); echo $ten->plus(5); // 15 echo $ten->multipliedBy(3); // 30 echo BigInteger::of(10)->plus(5)->multipliedBy(3); // 45 ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.