-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9c43c8d
commit 9118f1e
Showing
110 changed files
with
2,487 additions
and
2,456 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
root = true | ||
|
||
[*] | ||
charset = utf-8 | ||
end_of_line = lf | ||
indent_size = 2 | ||
indent_style = space | ||
trim_trailing_whitespace = true | ||
insert_final_newline = true | ||
|
||
[*.php] | ||
indent_size = 4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,96 +10,125 @@ | |
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types = 1); | ||
declare (strict_types = 1); | ||
|
||
namespace UnitConverter\Calculator; | ||
|
||
/** | ||
* Internal math helper class for floating point decimal | ||
* percision | ||
* The abstract calculator class that all concrete calculators should | ||
* extend from. | ||
* | ||
* @version 1.0.0 | ||
* @since 0.4.1 | ||
* @author Jordan Brauer <[email protected]> | ||
*/ | ||
abstract class AbstractCalculator implements CalculatorInterface | ||
{ | ||
/** | ||
* @var int $precision The number of decimal places that will calculated | ||
*/ | ||
protected $precision; | ||
|
||
/** | ||
* Public constructor for the unit converter calculator. | ||
* | ||
* @param int $precision The number of decimal places that will be calculated | ||
*/ | ||
public function __construct (int $precision = 2) | ||
{ | ||
$this->setPrecision($precision); | ||
} | ||
|
||
public function setPrecision (int $precision): CalculatorInterface | ||
{ | ||
$this->precision = $precision; | ||
return $this; | ||
} | ||
|
||
public function getPrecision (): int | ||
{ | ||
return $this->precision; | ||
} | ||
|
||
abstract public function add ($leftOperand, $rightOperand); | ||
|
||
abstract public function sub ($leftOperand, $rightOperand); | ||
|
||
abstract public function mul ($leftOperand, $rightOperand); | ||
|
||
abstract public function div ($dividend, $divisor); | ||
|
||
abstract public function mod ($dividend, $modulus); | ||
|
||
abstract public function pow ($base, $exponent); | ||
|
||
|
||
/** | ||
* Syntacital sugar wrapper method for sub | ||
*/ | ||
public function subtract (...$params) | ||
{ | ||
return $this->sub(...$params); | ||
} | ||
|
||
/** | ||
* Syntacital sugar wrapper method for mul | ||
*/ | ||
public function multiply (...$params) | ||
{ | ||
return $this->mul(...$params); | ||
} | ||
|
||
/** | ||
* Syntacital sugar wrapper method for div | ||
*/ | ||
public function divide (...$params) | ||
{ | ||
return $this->div(...$params); | ||
} | ||
|
||
/** | ||
* Syntacital sugar wrapper method for mod | ||
*/ | ||
public function modulus (...$params) | ||
{ | ||
return $this->mod(...$params); | ||
} | ||
|
||
/** | ||
* Syntacital sugar wrapper method for pow | ||
*/ | ||
public function power (...$params) | ||
{ | ||
return $this->pow(...$params); | ||
} | ||
/** | ||
* @var int $precision The number of decimal places that will calculated | ||
*/ | ||
protected $precision; | ||
|
||
/** | ||
* @var int $roundingMode The mode in which rounding occurs. Use one of the PHP_ROUND_HALF_* constants. | ||
*/ | ||
protected $roundingMode; | ||
|
||
/** | ||
* Public constructor for the unit converter calculator. For a list of | ||
* valid $roundingMode arguments, see the PHP_ROUND_HALF_* constants. | ||
* | ||
* @link https://secure.php.net/manual/en/function.round.php | ||
* | ||
* @param int $precision The number of decimal digits to round to. | ||
* @param int $roundingMode The mode in which rounding occurs. | ||
*/ | ||
public function __construct (int $precision = null, int $roundingMode = null) | ||
{ | ||
$this->setPrecision(($precision ?? 2)); | ||
$this->setRoundingMode(($roundingMode ?? PHP_ROUND_HALF_UP)); | ||
} | ||
|
||
public function setPrecision (int $precision): CalculatorInterface | ||
{ | ||
$this->precision = $precision; | ||
return $this; | ||
} | ||
|
||
public function setRoundingMode (int $roundingMode): CalculatorInterface | ||
{ | ||
$this->roundingMode = $roundingMode; | ||
return $this; | ||
} | ||
|
||
public function getPrecision (): int | ||
{ | ||
return $this->precision; | ||
} | ||
|
||
public function getRoundingMode (): int | ||
{ | ||
return $this->roundingMode; | ||
} | ||
|
||
abstract public function add ($leftOperand, $rightOperand); | ||
|
||
abstract public function sub ($leftOperand, $rightOperand); | ||
|
||
abstract public function mul ($leftOperand, $rightOperand); | ||
|
||
abstract public function div ($dividend, $divisor); | ||
|
||
abstract public function mod ($dividend, $modulus); | ||
|
||
abstract public function pow ($base, $exponent); | ||
|
||
public function round ($value, int $precision = null): float | ||
{ | ||
return round( | ||
$value, | ||
($precision ?? $this->getPrecision()), | ||
$this->getRoundingMode() | ||
); | ||
} | ||
|
||
/** | ||
* Syntacital sugar wrapper method for sub | ||
*/ | ||
public function subtract (...$params) | ||
{ | ||
return $this->sub(...$params); | ||
} | ||
|
||
/** | ||
* Syntacital sugar wrapper method for mul | ||
*/ | ||
public function multiply (...$params) | ||
{ | ||
return $this->mul(...$params); | ||
} | ||
|
||
/** | ||
* Syntacital sugar wrapper method for div | ||
*/ | ||
public function divide (...$params) | ||
{ | ||
return $this->div(...$params); | ||
} | ||
|
||
/** | ||
* Syntacital sugar wrapper method for mod | ||
*/ | ||
public function modulus (...$params) | ||
{ | ||
return $this->mod(...$params); | ||
} | ||
|
||
/** | ||
* Syntacital sugar wrapper method for pow | ||
*/ | ||
public function power (...$params) | ||
{ | ||
return $this->pow(...$params); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,54 +10,56 @@ | |
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types = 1); | ||
declare (strict_types = 1); | ||
|
||
namespace UnitConverter\Calculator; | ||
|
||
/** | ||
* Internal math helper class for floating point decimal | ||
* percision | ||
* A concrete calculator calss that uses the bcmath library | ||
* to perform mathematical operations. | ||
* | ||
* @link http://php.net/manual/en/book.bc.php | ||
* | ||
* @version 1.0.0 | ||
* @since 0.4.1 | ||
* @author Jordan Brauer <[email protected]> | ||
*/ | ||
class BinaryCalculator extends AbstractCalculator | ||
{ | ||
public function setPrecision(int $precision): CalculatorInterface | ||
{ | ||
parent::setPrecision($precision); | ||
bcscale($precision); | ||
return $this; | ||
} | ||
|
||
public function add ($leftOperand, $rightOperand) | ||
{ | ||
return bcadd($leftOperand, $rightOperand); | ||
} | ||
|
||
public function sub ($leftOperand, $rightOperand) | ||
{ | ||
return bcsub($leftOperand, $rightOperand); | ||
} | ||
|
||
public function mul ($leftOperand, $rightOperand) | ||
{ | ||
return bcmul($leftOperand, $rightOperand); | ||
} | ||
|
||
public function div ($dividend, $divisor) | ||
{ | ||
return bcdiv($dividend, $divisor); | ||
} | ||
|
||
public function mod ($dividend, $modulus) | ||
{ | ||
return bcmod($dividend, $modulus); | ||
} | ||
|
||
public function pow ($base, $exponent) | ||
{ | ||
return bcpow($base, $exponent); | ||
} | ||
public function setPrecision(int $precision): CalculatorInterface | ||
{ | ||
parent::setPrecision($precision); | ||
bcscale($precision); | ||
return $this; | ||
} | ||
|
||
public function add ($leftOperand, $rightOperand) | ||
{ | ||
return bcadd($leftOperand, $rightOperand); | ||
} | ||
|
||
public function sub ($leftOperand, $rightOperand) | ||
{ | ||
return bcsub($leftOperand, $rightOperand); | ||
} | ||
|
||
public function mul ($leftOperand, $rightOperand) | ||
{ | ||
return bcmul($leftOperand, $rightOperand); | ||
} | ||
|
||
public function div ($dividend, $divisor) | ||
{ | ||
return bcdiv($dividend, $divisor); | ||
} | ||
|
||
public function mod ($dividend, $modulus) | ||
{ | ||
return bcmod($dividend, $modulus); | ||
} | ||
|
||
public function pow ($base, $exponent) | ||
{ | ||
return bcpow($base, $exponent); | ||
} | ||
} |
Oops, something went wrong.