You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This library provides immutable classes to work with three types of numbers:
16
+
17
+
-`BigInteger` — an integer number such as `123`
18
+
-`BigDecimal` — a decimal number such as `1.23`
19
+
-`BigRational` — a fraction such as `2/3` — always reduced to lowest terms, e.g. `2/6` becomes `1/3`
20
+
21
+
It automatically uses GMP or BCMath when available, and falls back to a pure-PHP implementation otherwise.
22
+
23
+
All classes work with a virtually **unlimited number of digits**, and are limited only by available memory and CPU time.
24
+
13
25
### Installation
14
26
15
27
This library is installable via [Composer](https://getcomposer.org/):
@@ -22,42 +34,31 @@ composer require brick/math
22
34
23
35
This library requires PHP 8.2 or later.
24
36
25
-
For PHP 8.1 compatibility, you can use version `0.13`. For PHP 8.0, you can use version `0.11`. For PHP 7.4, you can use version `0.10`. For PHP 7.1, 7.2 & 7.3, you can use version `0.9`. Note that [PHP versions < 8.1 are EOL](http://php.net/supported-versions.php) and not supported anymore. If you're still using one of these PHP versions, you should consider upgrading as soon as possible.
26
-
27
37
Although the library can work seamlessly on any PHP installation, it is highly recommended that you install the
28
38
[GMP](http://php.net/manual/en/book.gmp.php) or [BCMath](http://php.net/manual/en/book.bc.php) extension
29
39
to speed up calculations. The fastest available calculator implementation will be automatically selected at runtime.
30
40
31
-
### Project status & release process
32
-
33
-
While this library is still under development, it is well tested and considered stable enough to use in production
34
-
environments.
35
-
36
-
The current releases are numbered `0.x.y`. When a non-breaking change is introduced (adding new methods, optimizing
37
-
existing code, etc.), `y` is incremented.
38
-
39
-
**When a breaking change is introduced, a new `0.x` version cycle is always started.**
40
-
41
-
It is therefore safe to lock your project to a given release cycle, such as `^0.14`.
42
-
43
-
If you need to upgrade to a newer release cycle, check the [release history](https://github.com/brick/math/releases)
44
-
for a list of changes introduced by each further `0.x.0` version.
41
+
## Number classes
45
42
46
-
### Package contents
43
+
The three number classes all extend the same `BigNumber` class:
47
44
48
-
This library provides the following public classes in the `Brick\Math` namespace:
45
+
```
46
+
Brick\Math\BigNumber
47
+
├── Brick\Math\BigInteger
48
+
├── Brick\Math\BigDecimal
49
+
└── Brick\Math\BigRational
50
+
```
49
51
50
-
-[BigNumber](https://github.com/brick/math/blob/0.14.7/src/BigNumber.php): base class for `BigInteger`, `BigDecimal` and `BigRational`
51
-
-[BigInteger](https://github.com/brick/math/blob/0.14.7/src/BigInteger.php): represents an arbitrary-precision integer number.
52
-
-[BigDecimal](https://github.com/brick/math/blob/0.14.7/src/BigDecimal.php): represents an arbitrary-precision decimal number.
53
-
-[BigRational](https://github.com/brick/math/blob/0.14.7/src/BigRational.php): represents an arbitrary-precision rational number (fraction), always reduced to lowest terms.
54
-
-[RoundingMode](https://github.com/brick/math/blob/0.14.7/src/RoundingMode.php): enum representing all available rounding modes.
52
+
`BigNumber` is an abstract class that defines the common behaviour of all number classes:
55
53
56
-
And [exceptions](#exceptions) in the `Brick\Math\Exception` namespace.
54
+
-`of()` — to obtain an instance
55
+
- sign methods: `isZero()`, `isPositive()`, etc.
56
+
- comparison methods: `isEqualTo()`, `isGreaterThan()`, etc.
57
+
-`min()`, `max()`, `sum()`, `toString()`, etc.
57
58
58
-
### Overview
59
+
`BigRational` numbers are always simplified to lowest terms, for example `2/6` is automatically simplified to `1/3`.
59
60
60
-
####Instantiation
61
+
## Instantiation
61
62
62
63
The constructors of the classes are not public, you must use a factory method to obtain an instance.
All number classes can be converted to string using either the `toString()` method, or the `(string)` cast. For example, the following lines are equivalent:
224
255
225
-
and bit shifting:
256
+
```php
257
+
echo BigInteger::of(123)->toString();
258
+
echo (string) BigInteger::of(123);
259
+
```
226
260
227
-
-`shiftedLeft()`
228
-
-`shiftedRight()`
261
+
Different number classes produce different outputs, but will all fold to plain digit strings if they represent a whole number:
262
+
263
+
```php
264
+
echo BigInteger::of(-123)->toString(); // -123
265
+
266
+
echo BigDecimal::of('1.0')->toString(); // 1.0
267
+
echo BigDecimal::of('1')->toString(); // 1
229
268
230
-
#### Exceptions
269
+
echo BigRational::of('2/3')->toString(); // 2/3
270
+
echo BigRational::of('1/1')->toString(); // 1
271
+
```
272
+
273
+
All string outputs are parseable by the `of()` factory method. The following is guaranteed to work:
274
+
275
+
```php
276
+
BigNumber::of($bigNumber->toString());
277
+
```
278
+
279
+
> [!IMPORTANT]
280
+
> Because `BigDecimal::toString()` and `BigRational::toString()` can return whole numbers, some numbers can be returned
281
+
> as `BigInteger` when parsed using `BigNumber::of()`. If you want to retain the original type when reparsing numbers,
282
+
> be sure to use `BigDecimal::of()` or `BigRational::of()`.
283
+
284
+
### BigRational to decimal string
285
+
286
+
In addition to the standard rational representation such as `2/3`, rational numbers can be represented as decimal numbers
287
+
with a potentially repeating sequence of digits. You can use `toRepeatingDecimalString()` to get this representation:
0 commit comments