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 numbers of any size:
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`
20
+
21
+
It works with or without the GMP or BCMath extensions, falling back to a pure-PHP implementation if necessary.
22
+
13
23
### Installation
14
24
15
25
This library is installable via [Composer](https://getcomposer.org/):
@@ -22,42 +32,35 @@ composer require brick/math
22
32
23
33
This library requires PHP 8.2 or later.
24
34
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
35
Although the library can work seamlessly on any PHP installation, it is highly recommended that you install the
28
36
[GMP](http://php.net/manual/en/book.gmp.php) or [BCMath](http://php.net/manual/en/book.bc.php) extension
29
37
to speed up calculations. The fastest available calculator implementation will be automatically selected at runtime.
30
38
31
-
### Project status & release process
39
+
##Number classes
32
40
33
-
While this library is still under development, it is well tested and considered stable enough to use in production
34
-
environments.
41
+
The three number classes all extend the same `BigNumber` class:
35
42
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.
43
+
```
44
+
Brick\Math\BigNumber
45
+
├── Brick\Math\BigInteger
46
+
├── Brick\Math\BigDecimal
47
+
└── Brick\Math\BigRational
48
+
```
45
49
46
-
### Package contents
50
+
`BigNumber` is an abstract class that defines the common behaviour of all number classes:
47
51
48
-
This library provides the following public classes in the `Brick\Math` namespace:
52
+
-`of()` — to obtain an instance
53
+
- sign methods: `isZero()`, `isPositive()`, etc.
54
+
- comparison methods: `isEqualTo()`, `isGreaterThan()`, etc.
55
+
-`min()`, `max()`, `sum()`, `toString()` etc.
49
56
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.
57
+
Comparison methods work across all number classes, you can for example compare a `BigInteger` to a `BigDecimal`, or a `BigDecimal` to a `BigRational`.
55
58
56
-
And [exceptions](#exceptions) in the `Brick\Math\Exception` namespace.
59
+
All classes work with a virtually unlimited number of digits, and are limited only by available memory and CPU time.
57
60
58
-
### Overview
61
+
`BigRational` numbers are always simplified to lowest terms, for example `2/6` is automatically simplified to `1/3`.
59
62
60
-
####Instantiation
63
+
## Instantiation
61
64
62
65
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
257
225
-
and bit shifting:
258
+
```php
259
+
echo BigInteger::of(123)->toString();
260
+
echo (string) BigInteger::of(123);
261
+
```
226
262
227
-
-`shiftedLeft()`
228
-
-`shiftedRight()`
263
+
Different number classes produce different outputs, but will all fold to plain digit strings if they represent a whole number:
264
+
265
+
```php
266
+
echo BigInteger::of(-123)->toString(); // -123
267
+
268
+
echo BigDecimal::of('1.0')->toString(); // 1.0
269
+
echo BigDecimal::of('1')->toString(); // 1
270
+
271
+
echo BigRational::of('2/3')->toString(); // 2/3
272
+
echo BigRational::of('1/1')->toString(); // 1
273
+
```
274
+
275
+
All string outputs are parseable by the `of()` factory method. The following is guaranteed to work:
276
+
277
+
```php
278
+
BigNumber::of($bigNumber->toString());
279
+
```
229
280
230
-
#### Exceptions
281
+
> [!IMPORTANT]
282
+
> Because `BigDecimal::toString()` and `BigRational::toString()` can return whole numbers, some numbers can be returned
283
+
> as `BigInteger` when parsed using `BigNumber::of()`. If you want to retain the original type when reparsing numbers,
284
+
> be sure to use `BigDecimal::of()` or `BigRational::of()`.
285
+
286
+
### BigRational to decimal string
287
+
288
+
In addition to the standard rational representation such as `2/3`, rational numbers can be represented as decimal numbers
289
+
with a potentially repeating sequence of digits. You can use `toRepeatingDecimalString()` to get this representation:
0 commit comments