Upgrading from 1.x

This version makes BigNumber immutable and renames the namespace.

Summary of changes#

This version is a breaking release. These are the changes that affect your code.

Step by step#

Follow these steps to move an existing project from 1.x.

1. Update the requirements and namespace#

Make sure you run PHP 8.4 or newer, then update the imports.

// before
use PHP\Math\BigNumber\BigNumber;

// after
use PHPMath\BigNumber\BigNumber;

2. Remove the mutable flag#

In 1.x the third constructor argument made a number immutable, and numbers were mutable unless you passed it. Delete that argument wherever you create a number. Every number is immutable now, and isMutable() no longer exists, so remove any code that checks it.

3. Assign results instead of relying on in-place changes#

In 1.x a mutable number changed itself when you called a method on it. Now every method returns a new number, so assign the result.

// before
$number = new BigNumber('10');
$number->add('5');

// after
$number = new BigNumber('10');
$number = $number->add('5');

4. Replace setValue() and setScale()#

setValue() changed the value of an existing number. Create a new number instead. setScale() is replaced by withScale(), which returns a copy.

// before
$number->setValue('42');
$number->setScale(2);

// after
$number = new BigNumber('42', 2);
$other = $number->withScale(4);

5. Replace removed Utils methods#

Utils::getPlainNumber() and Utils::multiply() no longer exist. Utils::toNumericString() replaces the first, and BigNumber::multiply() the second.

// before
$plain = Utils::getPlainNumber($value);
$product = Utils::multiply($left, $right, 2);

// after
$plain = Utils::toNumericString($value);
$product = (new BigNumber($left, 2))->multiply($right);

6. Re-check edge cases#

Truncation to the scale did not change: 1.x already used bcmath, which cuts digits off instead of rounding them, see Scale and truncation. What did change are bug fixes, so re-run your tests for mod(), numbers written in exponent notation such as 1.5e3, and base conversion with the Utils class. 2.x still needs the bcmath extension.