added version 1.0.0 of APIlite library,
added basic HTML template for help, added test class APIcalculator for demo
This commit is contained in:
126
test/APIcalculator.php
Normal file
126
test/APIcalculator.php
Normal file
@ -0,0 +1,126 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Class APIcalculator
|
||||
*
|
||||
* A calculator class that extends the functionality of TPsoft's APIlite.
|
||||
* This class provides basic arithmetic operations and a few additional
|
||||
* mathematical functions.
|
||||
*
|
||||
*/
|
||||
|
||||
require_once __DIR__ . '/../src/APIlite.php';
|
||||
|
||||
class APIcalculator extends \TPsoft\APIlite\APIlite
|
||||
{
|
||||
|
||||
/**
|
||||
* Add two numbers.
|
||||
* Standard plus operation.
|
||||
*
|
||||
* @param float $a The first number.
|
||||
* @param float $b The second number.
|
||||
* @return float The sum of the two numbers.
|
||||
*/
|
||||
public function add(float $a, float $b): float
|
||||
{
|
||||
return $a + $b;
|
||||
}
|
||||
|
||||
/**
|
||||
* Subtract two numbers.
|
||||
* Standard minus operation.
|
||||
*
|
||||
* @param float $a The first number.
|
||||
* @param float $b The second number.
|
||||
* @return float The difference of the two numbers.
|
||||
*/
|
||||
public function subtract(float $a, float $b): float
|
||||
{
|
||||
return $a - $b;
|
||||
}
|
||||
|
||||
/**
|
||||
* Multiply two numbers.
|
||||
* Standard multiplication operation.
|
||||
*
|
||||
* @param float $a The first number.
|
||||
* @param float $b The second number.
|
||||
* @return float The product of the two numbers.
|
||||
*/
|
||||
public function multiply(float $a, float $b): float
|
||||
{
|
||||
return $a * $b;
|
||||
}
|
||||
|
||||
/**
|
||||
* Divide two numbers.
|
||||
* Standard division operation. Throws an exception if dividing by zero.
|
||||
*
|
||||
* @param float $a The first number.
|
||||
* @param float $b The second number.
|
||||
* @return float The quotient of the two numbers. Throws an exception if dividing by zero.
|
||||
* @throws \Exception If dividing by zero.
|
||||
*/
|
||||
public function divide(float $a, float $b): float
|
||||
{
|
||||
if ($b == 0) {
|
||||
throw new \Exception('Division by zero');
|
||||
}
|
||||
return $a / $b;
|
||||
}
|
||||
|
||||
/**
|
||||
* Raise a number to a power.
|
||||
*
|
||||
* @param float $a The base number.
|
||||
* @param int $exponent The exponent.
|
||||
* @return float The result of raising the base number to the exponent.
|
||||
* @throws \Exception If the exponent is not an integer.
|
||||
*/
|
||||
public function power(float $a, int $exponent): float
|
||||
{
|
||||
$exponent = (int) $exponent;
|
||||
if ($exponent == 0) {
|
||||
return 1;
|
||||
}
|
||||
return pow($a, $exponent);
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate the square root of a number.
|
||||
*
|
||||
* @param int $a The number to calculate the square root of.
|
||||
* @return float The square root of the number.
|
||||
*/
|
||||
public function sqrt(int $a): float
|
||||
{
|
||||
return sqrt($a);
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate the absolute value of a number.
|
||||
*
|
||||
* @param float $a The number to calculate the absolute value of.
|
||||
* @return float The absolute value of the number.
|
||||
*/
|
||||
public function abs(float $a): float
|
||||
{
|
||||
return abs($a);
|
||||
}
|
||||
|
||||
/**
|
||||
* Round a number to the nearest integer.
|
||||
*
|
||||
* @param float $a The number to round.
|
||||
* @param int $precision The number of decimal places to round to.
|
||||
* @return int The rounded number.
|
||||
*/
|
||||
public function round(float $a, int $precision = 2): float
|
||||
{
|
||||
return round($a, $precision);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
new APIcalculator();
|
||||
Reference in New Issue
Block a user