- System Requirements
- Bc
- Why Bc?
- Usage Examples
- About
num
method - About
scale
method - Supported Calculation Methods
- Supported Comparison Methods
- Runtime Exceptions
- How to contribute and test in same environment?
- What I want to say
- php 8.1 or later
- php bcmath extension
Bc is a small and simple but accurate tool for calculation. It uses bcmath functions internally.
You can get the exact result you want.
There are no rounding issues if you have enough memory.
It's like calculating to N decimal places using a pen and paper with your hands.
Do you think below test will pass?
it('shows that 0.1 + 0.2 = 0.3', function () {
expect(0.1 + 0.2)->toBe(0.3);
});
Unfortunately, 0.1 + 0.2 is not 0.3 in php
It fails for the following reason:
Failed asserting that 0.30000000000000004 is identical to 0.3.
That's why I made this.
After giving a scale value, you can calculate in order.
(new Bc)->scale(2)->num('1')->add('2')->mul('3')->value(); // '9.00'
It also can be used like below:
(new Bc('1'))->add(new Bc('2'))->mul(new Bc('3'))->value(); // '9'
(new Bc)->scale(2)->num((new Bc('1')))->add(new Bc('2'))->mul(new Bc('3'))->value(); // '9.00'
After giving a scale value, you can compare.
// true for '10.00' > '1.00'
(new Bc)->scale(2)->num('10')
->isGreaterThan('1');
It also can be used like below:
// true for '30.00' > '3.00'
(new Bc)->scale(2)->num('10')->add('20')
->isGreaterThan((new Bc)->scale(2)->num('1')->add('2'));
It specifies a number at which the calculation begins. It always returns Bc
instance.
(new Bc)->num('1')
Or you can use the below instead:
(new Bc('1'))
It specifies a scale value to be passed on to a next operation. The default value is 0.
(new Bc)->num('1')->add('2')->value(); // '3'
It supports chaining:
// With scale 0, '1' + '2' = '3'
// With scale 2, '3.00' * '3.00' = '9.00'
(new Bc)->num('1')->add('2')->scale(2)->mul('3')->value(); // '9.00'
Calculation methods expect string
or Bc
instance. And they always return Bc
instance.
It adds a number
(new Bc)->num('1')->add('2')->value(); // '3'
It subtracts a number
(new Bc)->num('2')->sub('1')->value(); // '1'
It multiplies a number
(new Bc)->num('2')->mul('3')->value(); // '6'
It divides a number
(new Bc)->num('6')->div('3')->value(); // '2'
It calculates the modulus of a number
(new Bc)->num('10')->mod('7')->value(); // '3'
It calculates the power of a number
(new Bc)->num('2')->pow('3')->value(); // '8'
It calculates the power of a number with modulus
(new Bc)->num('2')->powmod('5', '3')->value(); // '2'
It calculates the square root of a number
(new Bc)->num('9')->sqrt()->value(); // '3'
Comparison methods expect string
or Bc
instance. And they always return bool
.
It checks if a number is greater than another number
(new Bc)->num('10')->isGreaterThan('1'); // true
It checks if a number is greater than or equal to another number
(new Bc)->num('10')->isGreaterThanOrEqual('10'); // true
It checks if a number is less than another number
(new Bc)->num('1')->isLessThan('10'); // true
It checks if a number is less than or equal to another number
(new Bc)->num('10')->isLessThanOrEqual('10'); // true
It checks if a number is equal to another number
(new Bc)->num('10')->isEqual('10'); // true
It checks if a number is different from another number
(new Bc)->num('10')->isDifferent('1'); // true
Same as isGreaterThan
(new Bc)->num('10')->gt('1'); // true
Same as isGreaterThanOrEqual
(new Bc)->num('10')->gte('10'); // true
Same as isLessThan
(new Bc)->num('1')->lt('10'); // true
Same as isLessThanOrEqual
(new Bc)->num('10')->lte('10'); // true
Same as isEqual
(new Bc)->num('10')->is('10'); // true
Same as isDifferent
(new Bc)->num('10')->isNot('1'); // true
throw new \Tetthys\Bc\Exceptions\ScaleCannotBeUsedForOperation;
This is thrown when scale is less than 0.
throw new \Tetthys\Bc\Exceptions\ValueCannotBeUsedForOperation;
This is thrown when value is not a number.
docker-compose up
bash ./run/shell/phptestenv.sh
composer install
./vendor/bin/pest
Or, you can run test from host OS using
bash ./run/test.sh
It is currently June 2024.
I'm still learning commit and branch naming conventions and php composer rules.
I hope you understand even if some of the names are bad.
Thank you.