PHP class for computing arbitrary-length integers
Just download and include calculator.php in your project:
include 'calculator.php';
Class contains 8 static methods:
-
Calculator::add( string $number_1, string $number_2, [bool $num_verification = true] )
Function calculate addition and returns a string ( number_1 + number_2 ) -
Calculator::subtr( string $number_1, string $number_2, [bool $num_verification = true] )
Function calculate subtraction and returns a string ( number_1 - number_2 ) -
Calculator::diff( string $number_1, string $number_2, [bool $num_verification = true] )
Function calculate difference and returns a string ( ABS(number_1 - number_2) ) -
Calculator::multip( string $number_1, string $number_2, [bool $num_verification = true] )
Function calculate multiplication and returns a string ( number_1 * number_2 ) -
Calculator::div( string $number_1, string $number_2, [int $precision = 10], [bool $num_verification = true] )
Function calculate division and returns a string ( number_1 / number_2 )
$precision represent number of digits after decimal point ( it is set default to 10 ) -
Calculator::mod( string $number_1, string $number_2, [bool $num_verification = true] )
Function calculate modulo and returns a string ( number_1 % number_2 )
- __Calculator::compare__( string $number_1, string $number_2, [bool $num_verification = true] )
Function compares number_1 and number_2 and returns:
( 1 ) if number_1 > number_2
( 0 ) if number_1 = number_2
(-1 ) if number_1 < number_2
-
valid_number(&$number)
Function checks if number is in the proper format and returns bool -
$num_verification = true
Each function has one optional parameter, and by defauld it is set to TRUE.
It checks if entered numers are in proper format.
It is a performance parameter, and if you make this chek using valid_number function, this parameter can be set to FALSE -
Proper format for numbers
All numbers must be entered as strings
If variable is int it can be casted to string ( $i = 13; $str = (string)$i; )
Numbers can not contain decimal points nor notations ( +/- )
All preceding zeros will be omitted $n = '12555'; and $n = '00012555'; are valid numbers
$n = '-4456'; and $n = '1.234567'; are invalid numbers -
Special cases
In some cases functions will return ERROR ( string 'E' )
Example:
dividing by zero ( Calculator::div('15', '0') ) will return string 'E'