Download the PHP package tetthys/bc without Composer
On this page you can find all versions of the php package tetthys/bc. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package bc
- System Requirements
- Bc
- Why Bc?
- Usage Examples
- Calculation
- Comparison
- About
num
method - About
scale
method - Supported Calculation Methods
- add
- sub
- mul
- div
- mod
- pow
- powmod
- sqrt
- Supported Comparison Methods
- isGreaterThan
- isGreaterThanOrEqual
- isLessThan
- isLessThanOrEqual
- isEqual
- isDifferent
- gt
- gte
- lt
- lte
- is
- isNot
- Runtime Exceptions
- ScaleCannotBeUsedForOperation
- ValueCannotBeUsedForOperation
- How to contribute and test in same environment?
- docker-compose up
- attach shell to phptestenv container
- install dependencies with composer
- run test
- What I want to say
System Requirements
- php 8.1 or later
- php bcmath extension
Bc
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.
Why Bc?
Do you think below test will pass?
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.
Usage Examples
Calculation
After giving a scale value, you can calculate in order.
It also can be used like below:
Comparison
After giving a scale value, you can compare.
It also can be used like below:
About num
method
It specifies a number at which the calculation begins. It always returns Bc
instance.
Or you can use the below instead:
About scale
method
It specifies a scale value to be passed on to a next operation. The default value is 0.
It supports chaining:
Supported Calculation Methods
Calculation methods expect string
or Bc
instance. And they always return Bc
instance.
add
It adds a number
sub
It subtracts a number
mul
It multiplies a number
div
It divides a number
mod
It calculates the modulus of a number
pow
It calculates the power of a number
powmod
It calculates the power of a number with modulus
sqrt
It calculates the square root of a number
Supported Comparison Methods
Comparison methods expect string
or Bc
instance. And they always return bool
.
isGreaterThan
It checks if a number is greater than another number
isGreaterThanOrEqual
It checks if a number is greater than or equal to another number
isLessThan
It checks if a number is less than another number
isLessThanOrEqual
It checks if a number is less than or equal to another number
isEqual
It checks if a number is equal to another number
isDifferent
It checks if a number is different from another number
gt
Same as isGreaterThan
gte
Same as isGreaterThanOrEqual
lt
Same as isLessThan
lte
Same as isLessThanOrEqual
is
Same as isEqual
isNot
Same as isDifferent
Runtime Exceptions
ScaleCannotBeUsedForOperation
This is thrown when scale is less than 0.
ValueCannotBeUsedForOperation
This is thrown when value is not a number.
How to contribute and test in same environment?
docker-compose up
attach shell to phptestenv container
install dependencies with composer
run test
Or, you can run test from host OS using
What I want to say
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.