Skip to content

Commit

Permalink
added math functions
Browse files Browse the repository at this point in the history
  • Loading branch information
uestla committed Oct 11, 2024
1 parent 3008b62 commit e8929d0
Show file tree
Hide file tree
Showing 4 changed files with 629 additions and 0 deletions.
125 changes: 125 additions & 0 deletions Simplex/Math/math.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
<?php

declare(strict_types = 1);

/**
* This file is part of the Simplex-Calculator library
*
* Copyright (c) 2014 Petr Kessler (https://kesspess.cz)
*
* @license MIT
* @link https://github.com/uestla/Simplex-Calculator
*/

namespace Simplex\Math;

use Simplex\ZeroGcdArgumentsException;


function add(string $a, string $b): string {
return bcadd($a, $b, 0);
}


function sub(string $a, string $b): string {
return bcsub($a, $b, 0);
}


function mul(string $a, string $b): string {
if ($b === '-1') { // micro-optimization for multiplying by -1
if ($a === '0') {
return '0';
}

if (isNegative($a)) {
return substr($a, 1);
}

return '-' . $a;
}

return bcmul($a, $b, 0);
}


function div(string $a, string $b, int $precision = 0): string {
return bcdiv($a, $b, $precision);
}


function pow(string $a, string $b): string {
return bcpow($a, $b, 0);
}


function mod(string $a, string $b): string {
return bcmod($a, $b, 0);
}


function abs(string $n): string {
if (isNegative($n)) {
return substr($n, 1);
}

return $n;
}


function isNegative(string $n): bool {
return strncmp($n, '-', 1) === 0;
}


function comp(string $a, string $b): int {
return bccomp($a, $b, 0);
}


function round(string $n, int $precision): string {
if (isNegative($n)) {
$result = bcsub($n, '0.' . str_repeat('0', $precision) . '5', $precision);

} else {
$result = bcadd($n, '0.' . str_repeat('0', $precision) . '5', $precision);
}

if (!str_contains($result, '.')) {
return $result;
}

return rtrim(rtrim($result, '0'), '.');
}


function gcd(string $a, string $b): string {
$aZero = $a === '0';
$bZero = $b === '0';

if ($aZero && $bZero) {
throw new ZeroGcdArgumentsException;
}

if ($aZero) {
return $b;
}

if ($bZero) {
return $a;
}

while (true) {
$mod = mod($a, $b);

if ($mod === '0') {
$gcd = $b;
break;
}

$a = $b;
$b = $mod;
}

return abs($gcd ?? $b);
}
27 changes: 27 additions & 0 deletions Simplex/exceptions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types = 1);

/**
* This file is part of the Simplex-Calculator library
*
* Copyright (c) 2014 Petr Kessler (https://kesspess.cz)
*
* @license MIT
* @link https://github.com/uestla/Simplex-Calculator
*/

namespace Simplex;


final class ZeroGcdArgumentsException extends SimplexException
{
public function __construct()
{
parent::__construct('At least one number must not be a zero.');
}
}


abstract class SimplexException extends \Exception
{}
2 changes: 2 additions & 0 deletions Simplex/simplex.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@
namespace Simplex;


require_once __DIR__ . '/exceptions.php';
require_once __DIR__ . '/Math/math.php';
Loading

0 comments on commit e8929d0

Please sign in to comment.