BinaryCalculator extends AbstractCalculator
A concrete calculator calss that uses the bcmath library to perform mathematical operations.
Tags
Table of Contents
- DEFAULT_PRECISION = 2
- The default precision value.
- DEFAULT_ROUNDING_MODE = self::ROUND_HALF_UP
- The default rounding mode for calculators.
- ROUND_HALF_DOWN = PHP_ROUND_HALF_DOWN
- Makes 1.5 into 1 and -1.5 into -1.
- ROUND_HALF_EVEN = PHP_ROUND_HALF_EVEN
- Rounds to the nearest even value.
- ROUND_HALF_ODD = PHP_ROUND_HALF_ODD
- Rounds to the nearest odd value.
- ROUND_HALF_UP = PHP_ROUND_HALF_UP
- Makes 1.5 into 2 and -1.5 into -2.
- SCALAR = 'string'
- String value representation of the allowed scalar type(s) for the calculator's inputs.
- $history : array<string|int, mixed>
- A non-persitent stack of events for the current calculator's calculations
- $precision : int
- The number of decimal places that will calculated
- $roundingMode : int
- The mode in which rounding occurs. Use one of the PHP_ROUND_HALF_* constants.
- __construct() : self
- Public constructor for the unit converter calculator. For a list of valid $roundingMode arguments, see the PHP_ROUND_HALF_* constants.
- add() : int|float|string
- Add two arbitrary precision numbers
- div() : int|float|string
- Divide two arbitrary precision numbers
- divide() : mixed
- Syntacital sugar wrapper method for div.
- dump() : array<string|int, mixed>
- Dump the calculator's history. Optionally clear the calculator afterwards.
- exec() : int|float|string
- A robust calculator method to run formulaic operations, abstracting the logic to a single, contained class.
- getPrecision() : int
- Returns the current decimal precision for the calculator
- getRoundingMode() : int
- Return the current rounding mode
- mod() : int|float|string
- Get modulus of an arbitrary precision number
- modulus() : mixed
- Syntacital sugar wrapper method for mod
- mul() : int|float|string
- Multiply two arbitrary precision numbers
- multiply() : mixed
- Syntacital sugar wrapper method for mul
- pow() : int|float|string
- Raise an arbitrary precision number to another
- power() : mixed
- Syntacital sugar wrapper method for pow
- round() : int|float|string
- Overwrites the default implementation for rounding. Simply casts the result to a string.
- setPrecision() : Calculator
- Overwrites the default implementation for setting the precision to round values to.
- setRoundingMode() : CalculatorInterface
- Use one of the PHP_ROUND_HALF_* constants to specify the mode in which rounding occurs.
- sub() : int|float|string
- Subtract one arbitrary precision number from another
- subtract() : mixed
- Syntacital sugar wrapper method for sub
- invariant() : void
- Throw a type error if the given closure does not evaluate to true for one of given values. The name of the method and allowed type string are used to create an error message.
- expandScientific() : string
- Expand a scientific notation number to it's whole form as a string.
- init() : void
- Helper method to initialize the calculator's settings.
- isScientific() : bool
- Check if the given number is scientific notation.
- operand() : string
- Sanitize operands for use with BC math.
- product() : string
- Produce a valid calculation result, ensuring no trailing zeros.
Constants
DEFAULT_PRECISION
The default precision value.
public
mixed
DEFAULT_PRECISION
= 2
Tags
DEFAULT_ROUNDING_MODE
The default rounding mode for calculators.
public
mixed
DEFAULT_ROUNDING_MODE
= self::ROUND_HALF_UP
Tags
ROUND_HALF_DOWN
Makes 1.5 into 1 and -1.5 into -1.
public
mixed
ROUND_HALF_DOWN
= PHP_ROUND_HALF_DOWN
Tags
ROUND_HALF_EVEN
Rounds to the nearest even value.
public
mixed
ROUND_HALF_EVEN
= PHP_ROUND_HALF_EVEN
Tags
ROUND_HALF_ODD
Rounds to the nearest odd value.
public
mixed
ROUND_HALF_ODD
= PHP_ROUND_HALF_ODD
Tags
ROUND_HALF_UP
Makes 1.5 into 2 and -1.5 into -2.
public
mixed
ROUND_HALF_UP
= PHP_ROUND_HALF_UP
Tags
SCALAR
String value representation of the allowed scalar type(s) for the calculator's inputs.
protected
mixed
SCALAR
= 'string'
Properties
$history
A non-persitent stack of events for the current calculator's calculations
protected
array<string|int, mixed>
$history
$precision
The number of decimal places that will calculated
protected
int
$precision
$roundingMode
The mode in which rounding occurs. Use one of the PHP_ROUND_HALF_* constants.
protected
int
$roundingMode
Methods
__construct()
Public constructor for the unit converter calculator. For a list of valid $roundingMode arguments, see the PHP_ROUND_HALF_* constants.
public
__construct([int $precision = null ][, int $roundingMode = null ]) : self
Parameters
- $precision : int = null
-
The number of decimal digits to round to.
- $roundingMode : int = null
-
The mode in which rounding occurs.
Return values
self —add()
Add two arbitrary precision numbers
public
add(mixed $leftOperand, mixed $rightOperand) : int|float|string
Parameters
- $leftOperand : mixed
-
The number left of the operator
- $rightOperand : mixed
-
The number right of the operator
Return values
int|float|string —div()
Divide two arbitrary precision numbers
public
div(mixed $dividend, mixed $divisor) : int|float|string
Parameters
- $dividend : mixed
-
The number beind divided
- $divisor : mixed
-
The number that is doing the dividing
Return values
int|float|string —divide()
Syntacital sugar wrapper method for div.
public
divide(mixed ...$params) : mixed
Parameters
- $params : mixed
Tags
Return values
mixed —dump()
Dump the calculator's history. Optionally clear the calculator afterwards.
public
dump([bool $clear = false ]) : array<string|int, mixed>
Parameters
- $clear : bool = false
-
(optional) Clear the calculator after dumping.
Return values
array<string|int, mixed> —exec()
A robust calculator method to run formulaic operations, abstracting the logic to a single, contained class.
public
exec(FormulaInterface $formula, mixed ...$parameters) : int|float|string
Parameters
- $formula : FormulaInterface
-
The formula to run.
- $parameters : mixed
-
A variadic set of arguments to pass to the formula.
Return values
int|float|string —getPrecision()
Returns the current decimal precision for the calculator
public
getPrecision() : int
Return values
int —getRoundingMode()
Return the current rounding mode
public
getRoundingMode() : int
Return values
int —mod()
Get modulus of an arbitrary precision number
public
mod(mixed $dividend, mixed $modulus) : int|float|string
Parameters
- $dividend : mixed
-
The dividend as a string
- $modulus : mixed
-
The modulus as a string
Return values
int|float|string —modulus()
Syntacital sugar wrapper method for mod
public
modulus(mixed ...$params) : mixed
Parameters
- $params : mixed
Tags
Return values
mixed —mul()
Multiply two arbitrary precision numbers
public
mul(mixed $leftOperand, mixed $rightOperand) : int|float|string
Parameters
- $leftOperand : mixed
-
The number left of the operator
- $rightOperand : mixed
-
The number right of the operator
Return values
int|float|string —multiply()
Syntacital sugar wrapper method for mul
public
multiply(mixed ...$params) : mixed
Parameters
- $params : mixed
Tags
Return values
mixed —pow()
Raise an arbitrary precision number to another
public
pow(mixed $base, mixed $exponent) : int|float|string
Parameters
- $base : mixed
-
The base, as a string.
- $exponent : mixed
-
The exponent, as a string. If the exponent is non-integral, it is truncated. The valid range of the exponent is platform specific, but is at least -2147483648 to 2147483647.
Return values
int|float|string —power()
Syntacital sugar wrapper method for pow
public
power(mixed ...$params) : mixed
Parameters
- $params : mixed
Tags
Return values
mixed —round()
Overwrites the default implementation for rounding. Simply casts the result to a string.
public
round(mixed $value[, int $precision = null ]) : int|float|string
Parameters
- $value : mixed
-
The value to round
- $precision : int = null
-
The number of decimal digits to round to.
Return values
int|float|string —setPrecision()
Overwrites the default implementation for setting the precision to round values to.
public
setPrecision(int $precision) : Calculator
Parameters
- $precision : int
-
The amount of decimal places that will be returned
Return values
Calculator —setRoundingMode()
Use one of the PHP_ROUND_HALF_* constants to specify the mode in which rounding occurs.
public
setRoundingMode(int $roundingMode) : CalculatorInterface
Parameters
- $roundingMode : int
Return values
CalculatorInterface —sub()
Subtract one arbitrary precision number from another
public
sub(mixed $leftOperand, mixed $rightOperand) : int|float|string
Parameters
- $leftOperand : mixed
-
The number left of the operator
- $rightOperand : mixed
-
The number right of the operator
Return values
int|float|string —subtract()
Syntacital sugar wrapper method for sub
public
subtract(mixed ...$params) : mixed
Parameters
- $params : mixed
Tags
Return values
mixed —invariant()
Throw a type error if the given closure does not evaluate to true for one of given values. The name of the method and allowed type string are used to create an error message.
protected
static invariant(Closure $assert, string $method, string $allowed, mixed ...$value) : void
Parameters
- $assert : Closure
-
Type check that must return true, otherwise an error is thrown
- $method : string
-
The name of the method that is calling this method, used for error message
- $allowed : string
-
The allowed type of the value, used for error message
- $value : mixed
-
One or more values to test with the given closure
Tags
Return values
void —expandScientific()
Expand a scientific notation number to it's whole form as a string.
private
static expandScientific(string $operand) : string
Parameters
- $operand : string
-
The scientifix number to be expanded.
Return values
string —init()
Helper method to initialize the calculator's settings.
private
init(int $precision, int $roundingMode) : void
Parameters
- $precision : int
-
The number of decimal digits to round to.
- $roundingMode : int
-
The mode in which rounding occurs.
Return values
void —isScientific()
Check if the given number is scientific notation.
private
static isScientific(int|string|float $operand) : bool
Parameters
- $operand : int|string|float
-
The value to check for scientific-ness
Return values
bool —operand()
Sanitize operands for use with BC math.
private
static operand(string $value) : string
Parameters
- $value : string
-
The operand value part of the calculation
Return values
string —product()
Produce a valid calculation result, ensuring no trailing zeros.
private
static product(string $value) : string
Parameters
- $value : string
-
The value to denormalize