Update #4
@ -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
|
||||||
|
@ -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);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -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);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -132,7 +132,7 @@ abstract class FileHelper
|
|||||||
elseif (!self::$curlError)
|
elseif (!self::$curlError)
|
||||||
{
|
{
|
||||||
// set the notice
|
// set the notice
|
||||||
Factory::getApplication()->enqueueMessage(Text::_('COM_GETBIBLE_HTWOCURL_NOT_FOUNDHTWOPPLEASE_SETUP_CURL_ON_YOUR_SYSTEM_OR_BGETBIBLEB_WILL_NOT_FUNCTION_CORRECTLYP'), 'Error');
|
Factory::getApplication()->enqueueMessage('<h2>Curl Not Found!</h2><p>Please setup curl on your system, or the <b>Joomla Component</b> will not function correctly!</p>', 'Error');
|
||||||
// load this notice only once
|
// load this notice only once
|
||||||
self::$curlError = true;
|
self::$curlError = true;
|
||||||
}
|
}
|
||||||
|
@ -101,7 +101,7 @@
|
|||||||
elseif (!self::$curlError)
|
elseif (!self::$curlError)
|
||||||
{
|
{
|
||||||
// set the notice
|
// set the notice
|
||||||
Factory::getApplication()->enqueueMessage(Text::_('<h2>Curl Not Found!</h2><p>Please setup curl on your system, or <b>[[[component]]]</b> will not function correctly!</p>'), 'Error');
|
Factory::getApplication()->enqueueMessage('<h2>Curl Not Found!</h2><p>Please setup curl on your system, or the <b>Joomla Component</b> will not function correctly!</p>', 'Error');
|
||||||
// load this notice only once
|
// load this notice only once
|
||||||
self::$curlError = true;
|
self::$curlError = true;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user