PHP Classes

PHP Dependency Injection Calculator: Perform math calculations using an injected object

Recommend this page to a friend!
  Info   Documentation   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 36 All time: 10,984 This week: 560Up
Version License PHP version Categories
dependency-calculato 1.0.0MIT/X Consortium ...7Math, Design Patterns, PHP 7
Description 

Author

This package can perform math calculations using an injected object.

It provides an abstract calculator class that can perform several math calculations with numbers, like adding, subtracting, multiplying, and dividing.

The calculator class takes an injected object that will perform the actual calculations.

The package provides several classes that implement the math operations using different approaches to perform those operations.

Currently, it provides math operations classes that can be used to create objects to inject in the calculator class, like:

- GMP (GNU Multi Precision)

- BCMath (Arbitrary Precision Mathematics)

Innovation Award
PHP Programming Innovation award nominee
June 2023
Number 6
Several solutions can work as alternatives when a developer needs to solve a problem.

For instance, a developer may store and retrieve the application data using different databases.

In this case, using different classes that implement the same database access operations with other codes is recommended for accessing different types of databases.

Then the application may write code that works well with different databases.

If, in the future, the developer needs to change the application to use a different type of database, he needs to use an object to access that type of database without needing to change the code of the rest of the application.

In this case, the application uses a database access object and injects it into the code that implements the application.

This approach to separate the application code from the implementation class code that the application depends on is called dependency injection.

This package implements this approach to perform math operations. It provides a main class that implements several types of math operations. It also offers several courses that implement those math operations with different PHP extensions that provide various types of precision.

Manuel Lemos
Picture of Chun-Sheng, Li
  Performance   Level  
Name: Chun-Sheng, Li <contact>
Classes: 37 packages by
Country: Taiwan Taiwan
Age: 31
All time rank: 21046 in Taiwan Taiwan
Week rank: 4 Up1 in Taiwan Taiwan Up
Innovation award
Innovation award
Nominee: 17x

Winner: 1x

Documentation

Dependency Calculator

build Coverage Status

Introduction

Usage

Here is some examples about how to calculate two numbers.

Add two numbers

use Lee\Calculator\Calculator;
use Lee\Calculator\modules\GmpCalculator;
use Lee\Calculator\modules\BcMathCalculator;

$gmpCalculator = new Calculator(new GmpCalculator());
$bcMathCalculator = new Calculator(new BcMathCalculator());

echo $gmpCalculator->add('123456789', '123456789000000'); // 123456912456789
echo $bcMathCalculator->add('123456789', '123456789000000'); // 123456912456789

Minus two numbers

use Lee\Calculator\Calculator;
use Lee\Calculator\modules\GmpCalculator;
use Lee\Calculator\modules\BcMathCalculator;

$gmpCalculator = new Calculator(new GmpCalculator());
$bcMathCalculator = new Calculator(new BcMathCalculator());

echo $gmpCalculator->minus('123456789', '123456789000000'); // -123456665543211
echo $bcMathCalculator->minus('123456789', '123456789000000'); // -123456665543211

Multiple two numbers


use Lee\Calculator\Calculator;
use Lee\Calculator\modules\GmpCalculator;
use Lee\Calculator\modules\BcMathCalculator;

$gmpCalculator = new Calculator(new GmpCalculator());
$bcMathCalculator = new Calculator(new BcMathCalculator());

echo $gmpCalculator->mul('123456789', '123456789000000'); // 15241578750190521000000
echo $bcMathCalculator->mul('123456789', '123456789000000'); // 15241578750190521000000

Divide two numbers


use Lee\Calculator\Calculator;
use Lee\Calculator\modules\GmpCalculator;
use Lee\Calculator\modules\BcMathCalculator;

$gmpCalculator = new Calculator(new GmpCalculator());
$bcMathCalculator = new Calculator(new BcMathCalculator());

echo $gmpCalculator->divide('123456789000000', '123456789'); // 1000000
echo $bcMathCalculator->divide('123456789000000', '123456789'); // 1000000

  Files folder image Files (9)  
File Role Description
Files folder image.github (1 directory)
Files folder imagesrc (1 file, 2 directories)
Files folder imagetests (1 file)
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file phpunit.xml.dist Data Auxiliary data
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files (9)  /  .github  
File Role Description
Files folder imageworkflows (1 file)

  Files folder image Files (9)  /  .github  /  workflows  
File Role Description
  Accessible without login Plain text file build.yml Data Auxiliary data

  Files folder image Files (9)  /  src  
File Role Description
Files folder imageinterfaces (1 file)
Files folder imagemodules (2 files)
  Plain text file Calculator.php Class Class source

  Files folder image Files (9)  /  src  /  interfaces  
File Role Description
  Plain text file Calculator.php Class Class source

  Files folder image Files (9)  /  src  /  modules  
File Role Description
  Plain text file BcMathCalculator.php Class Class source
  Plain text file GmpCalculator.php Class Class source

  Files folder image Files (9)  /  tests  
File Role Description
  Plain text file CalculatorTest.php Class Class source

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:36
This week:0
All time:10,984
This week:560Up