From 3d5c34a922fe985d1dc1f0b00f1be8ac6bec4866 Mon Sep 17 00:00:00 2001 From: aB0t Date: Fri, 18 Aug 2023 07:49:54 +0200 Subject: [PATCH] update 2023-08-18 07:49:54 --- .../README.md | 9 ++- .../code.php | 63 +++++++++++++------ .../code.power | 63 +++++++++++++------ 3 files changed, 97 insertions(+), 38 deletions(-) diff --git a/src/152c8793-8b75-4715-996a-257b9f65451c/README.md b/src/152c8793-8b75-4715-996a-257b9f65451c/README.md index 44df714..f240c11 100644 --- a/src/152c8793-8b75-4715-996a-257b9f65451c/README.md +++ b/src/152c8793-8b75-4715-996a-257b9f65451c/README.md @@ -11,7 +11,8 @@ ```uml @startuml 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 } @@ -19,7 +20,7 @@ note right of MathHelper::bc bc math wrapper (very basic not for accounting) since: 3.0.9 - return: int + return: string|int|null|bool arguments: string $type @@ -31,6 +32,10 @@ end note note right of MathHelper::sum 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 return: float end note diff --git a/src/152c8793-8b75-4715-996a-257b9f65451c/code.php b/src/152c8793-8b75-4715-996a-257b9f65451c/code.php index 60bc44b..a40f191 100644 --- a/src/152c8793-8b75-4715-996a-257b9f65451c/code.php +++ b/src/152c8793-8b75-4715-996a-257b9f65451c/code.php @@ -27,48 +27,75 @@ abstract class MathHelper * @param int $val2 The second value * @param int $scale The scale value * - * @return int + * @return string|int|null|bool * * @since 3.0.9 */ 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; - // 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); } + // if function does not exist we use +-*/ operators (fallback - not ideal) switch ($type) { - // Multiply two numbers case 'mul': return (string) round($val1 * $val2, $scale); - break; - // Divide of two numbers case 'div': + if ($val2 == 0) return null; // Avoid division by zero return (string) round($val1 / $val2, $scale); - break; - // Adding two numbers case 'add': return (string) round($val1 + $val2, $scale); - break; - // Subtract one number from the other case 'sub': return (string) round($val1 - $val2, $scale); - break; - // Raise an arbitrary precision number to another case 'pow': return (string) round(pow($val1, $val2), $scale); - break; - // Compare two arbitrary precision numbers case 'comp': - return (round($val1,2) == round($val2,2)); - break; + $diff = round($val1 - $val2, $scale); + 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); } /** diff --git a/src/152c8793-8b75-4715-996a-257b9f65451c/code.power b/src/152c8793-8b75-4715-996a-257b9f65451c/code.power index 68c8863..e33904d 100644 --- a/src/152c8793-8b75-4715-996a-257b9f65451c/code.power +++ b/src/152c8793-8b75-4715-996a-257b9f65451c/code.power @@ -6,48 +6,75 @@ * @param int $val2 The second value * @param int $scale The scale value * - * @return int + * @return string|int|null|bool * * @since 3.0.9 */ 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; - // 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); } + // if function does not exist we use +-*/ operators (fallback - not ideal) switch ($type) { - // Multiply two numbers case 'mul': return (string) round($val1 * $val2, $scale); - break; - // Divide of two numbers case 'div': + if ($val2 == 0) return null; // Avoid division by zero return (string) round($val1 / $val2, $scale); - break; - // Adding two numbers case 'add': return (string) round($val1 + $val2, $scale); - break; - // Subtract one number from the other case 'sub': return (string) round($val1 - $val2, $scale); - break; - // Raise an arbitrary precision number to another case 'pow': return (string) round(pow($val1, $val2), $scale); - break; - // Compare two arbitrary precision numbers case 'comp': - return (round($val1,2) == round($val2,2)); - break; + $diff = round($val1 - $val2, $scale); + 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); } /**