update 2023-08-18 07:49:54

This commit is contained in:
Robot 2023-08-18 07:49:54 +02:00
parent 6a59495c48
commit 3d5c34a922
Signed by untrusted user: Robot
GPG Key ID: 14DECD44E7E1BB95
3 changed files with 97 additions and 38 deletions

View File

@ -11,7 +11,8 @@
```uml ```uml
@startuml @startuml
abstract MathHelper #Orange { abstract MathHelper #Orange {
+ {static} bc(string $type, int $val1, ...) : int + {static} bc(string $type, int $val1, ...) : string|int|null|bool
+ {static} sum(array $array, int $scale = 4) : float
+ {static} sum(array $array, int $scale = 4) : float + {static} sum(array $array, int $scale = 4) : float
} }
@ -19,7 +20,7 @@ note right of MathHelper::bc
bc math wrapper (very basic not for accounting) bc math wrapper (very basic not for accounting)
since: 3.0.9 since: 3.0.9
return: int return: string|int|null|bool
arguments: arguments:
string $type string $type
@ -31,6 +32,10 @@ end note
note right of MathHelper::sum note right of MathHelper::sum
Basic sum of an array with more precision Basic sum of an array with more precision
since: 3.0.9
return: float
Basic sum of an array with more precision
since: 3.0.9 since: 3.0.9
return: float return: float
end note end note

View File

@ -27,48 +27,75 @@ abstract class MathHelper
* @param int $val2 The second value * @param int $val2 The second value
* @param int $scale The scale value * @param int $scale The scale value
* *
* @return int * @return string|int|null|bool
* *
* @since 3.0.9 * @since 3.0.9
*/ */
public static function bc($type, $val1, $val2, $scale = 0) public static function bc($type, $val1, $val2, $scale = 0)
{ {
// build function name // Validate input
if (!is_numeric($val1) || !is_numeric($val2))
{
return null;
}
// Build function name
$function = 'bc' . $type; $function = 'bc' . $type;
// use the bcmath function if available
if (function_exists($function)) // Use the bcmath function if available
if (is_callable($function))
{ {
return $function($val1, $val2, $scale); return $function($val1, $val2, $scale);
} }
// if function does not exist we use +-*/ operators (fallback - not ideal) // if function does not exist we use +-*/ operators (fallback - not ideal)
switch ($type) switch ($type)
{ {
// Multiply two numbers
case 'mul': case 'mul':
return (string) round($val1 * $val2, $scale); return (string) round($val1 * $val2, $scale);
break;
// Divide of two numbers
case 'div': case 'div':
if ($val2 == 0) return null; // Avoid division by zero
return (string) round($val1 / $val2, $scale); return (string) round($val1 / $val2, $scale);
break;
// Adding two numbers
case 'add': case 'add':
return (string) round($val1 + $val2, $scale); return (string) round($val1 + $val2, $scale);
break;
// Subtract one number from the other
case 'sub': case 'sub':
return (string) round($val1 - $val2, $scale); return (string) round($val1 - $val2, $scale);
break;
// Raise an arbitrary precision number to another
case 'pow': case 'pow':
return (string) round(pow($val1, $val2), $scale); return (string) round(pow($val1, $val2), $scale);
break;
// Compare two arbitrary precision numbers
case 'comp': case 'comp':
return (round($val1,2) == round($val2,2)); $diff = round($val1 - $val2, $scale);
break; return ($diff > 0) ? 1 : (($diff < 0) ? -1 : 0);
} }
return false;
return null;
}
/**
* Basic sum of an array with more precision
*
* @param array $array The values to sum
* @param int $scale The scale value
*
* @return float
*
* @since 3.0.9
*/
public static function sum($array, $scale = 4)
{
// use the bcadd function if available
if (function_exists('bcadd'))
{
// set the start value
$value = 0.0;
// loop the values and run bcadd
foreach($array as $val)
{
$value = bcadd($value, (string) $val, $scale);
}
return $value;
}
// fall back on array sum
return array_sum($array);
} }
/** /**

View File

@ -6,48 +6,75 @@
* @param int $val2 The second value * @param int $val2 The second value
* @param int $scale The scale value * @param int $scale The scale value
* *
* @return int * @return string|int|null|bool
* *
* @since 3.0.9 * @since 3.0.9
*/ */
public static function bc($type, $val1, $val2, $scale = 0) public static function bc($type, $val1, $val2, $scale = 0)
{ {
// build function name // Validate input
if (!is_numeric($val1) || !is_numeric($val2))
{
return null;
}
// Build function name
$function = 'bc' . $type; $function = 'bc' . $type;
// use the bcmath function if available
if (function_exists($function)) // Use the bcmath function if available
if (is_callable($function))
{ {
return $function($val1, $val2, $scale); return $function($val1, $val2, $scale);
} }
// if function does not exist we use +-*/ operators (fallback - not ideal) // if function does not exist we use +-*/ operators (fallback - not ideal)
switch ($type) switch ($type)
{ {
// Multiply two numbers
case 'mul': case 'mul':
return (string) round($val1 * $val2, $scale); return (string) round($val1 * $val2, $scale);
break;
// Divide of two numbers
case 'div': case 'div':
if ($val2 == 0) return null; // Avoid division by zero
return (string) round($val1 / $val2, $scale); return (string) round($val1 / $val2, $scale);
break;
// Adding two numbers
case 'add': case 'add':
return (string) round($val1 + $val2, $scale); return (string) round($val1 + $val2, $scale);
break;
// Subtract one number from the other
case 'sub': case 'sub':
return (string) round($val1 - $val2, $scale); return (string) round($val1 - $val2, $scale);
break;
// Raise an arbitrary precision number to another
case 'pow': case 'pow':
return (string) round(pow($val1, $val2), $scale); return (string) round(pow($val1, $val2), $scale);
break;
// Compare two arbitrary precision numbers
case 'comp': case 'comp':
return (round($val1,2) == round($val2,2)); $diff = round($val1 - $val2, $scale);
break; return ($diff > 0) ? 1 : (($diff < 0) ? -1 : 0);
} }
return false;
return null;
}
/**
* Basic sum of an array with more precision
*
* @param array $array The values to sum
* @param int $scale The scale value
*
* @return float
*
* @since 3.0.9
*/
public static function sum($array, $scale = 4)
{
// use the bcadd function if available
if (function_exists('bcadd'))
{
// set the start value
$value = 0.0;
// loop the values and run bcadd
foreach($array as $val)
{
$value = bcadd($value, (string) $val, $scale);
}
return $value;
}
// fall back on array sum
return array_sum($array);
} }
/** /**