ILIAS  Release_5_0_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
ilMath Class Reference

Class ilMath. More...

+ Collaboration diagram for ilMath:

Public Member Functions

 exp2dec ($float_str)

Static Public Member Functions

static _add ($left_operand, $right_operand, $scale=50)
static _comp ($left_operand, $right_operand, $scale=50)
static _div ($left_operand, $right_operand, $scale=50)
static _mod ($left_operand, $modulus)
static _mul ($left_operand, $right_operand, $scale=50)
static _pow ($left_operand, $right_operand, $scale=50)
static _sqrt ($operand, $scale=50)
static _sub ($left_operand, $right_operand, $scale=50)
static isCoprimeFraction ($numerator, $denominator)
static getGreatestCommonDivisor ($a, $b)
static _round ($value, $precision=0)
static _equals ($value1, $value2, $scale)

Static Private Member Functions

static bcconv ($fNumber)

Detailed Description

Class ilMath.

Wrapper for mathematical operations

Author
Helmut Schottmüller helmu.nosp@m.t.sc.nosp@m.hottm.nosp@m.uell.nosp@m.er@ma.nosp@m.c.co.nosp@m.m
Id:
class.ilMath.php 59435 2015-06-09 13:51:01Z gitmgr

Definition at line 32 of file class.ilMath.php.

Member Function Documentation

static ilMath::_add (   $left_operand,
  $right_operand,
  $scale = 50 
)
static

Definition at line 37 of file class.ilMath.php.

References $res, and bcconv().

Referenced by assFormulaQuestionVariable\getRandomValue(), assFormulaQuestionResult\isInTolerance(), and EvalMath\pfx().

{
$left_operand=ilMath::bcconv($left_operand);
$right_operand=ilMath::bcconv($right_operand);
$scale=ilMath::bcconv($scale);
if (function_exists("bcadd"))
{
return bcadd($left_operand, $right_operand, $scale);
}
else
{
$res = $left_operand + $right_operand;
if (is_numeric($scale)) $res = round($res, $scale);
return $res;
}
}

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

static ilMath::_comp (   $left_operand,
  $right_operand,
  $scale = 50 
)
static

Definition at line 58 of file class.ilMath.php.

References bcconv().

Referenced by _equals().

{
$left_operand=ilMath::bcconv($left_operand);
$right_operand=ilMath::bcconv($right_operand);
$scale=ilMath::bcconv($scale);
if (function_exists("bccomp"))
{
return bccomp($left_operand, $right_operand, $scale);
}
else
{
if (is_numeric($scale))
{
$left_operand = round($left_operand, $scale);
$right_operand = round($right_operand, $scale);
}
if ($left_operand == $right_operand) return 0;
if ($left_operand > $right_operand) return 1;
return -1;
}
}

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

static ilMath::_div (   $left_operand,
  $right_operand,
  $scale = 50 
)
static

Definition at line 84 of file class.ilMath.php.

References $res, and bcconv().

Referenced by assFormulaQuestionResult\calculateFormula(), assFormulaQuestion\getBestSolution(), assFormulaQuestionVariable\getRandomValue(), assFormulaQuestionResult\getReachedPoints(), assFormulaQuestionResult\isCorrect(), assFormulaQuestionResult\isInTolerance(), EvalMath\pfx(), and assFormulaQuestionResult\suggestRange().

{
$left_operand=ilMath::bcconv($left_operand);
$right_operand=ilMath::bcconv($right_operand);
$scale=ilMath::bcconv($scale);
if (function_exists("bcdiv"))
{
return bcdiv($left_operand, $right_operand, $scale);
}
else
{
if ($right_operand == 0) return NULL;
$res = $left_operand / $right_operand;
if (is_numeric($scale)) $res = round($res, $scale);
return $res;
}
}

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

static ilMath::_equals (   $value1,
  $value2,
  $scale 
)
static

Definition at line 290 of file class.ilMath.php.

References _comp().

Referenced by assFormulaQuestionResult\getReachedPoints(), and assFormulaQuestionResult\isCorrect().

{
return self::_comp($value1, $value2, $scale) === 0;
}

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

static ilMath::_mod (   $left_operand,
  $modulus 
)
static

Definition at line 106 of file class.ilMath.php.

References bcconv().

{
$left_operand=ilMath::bcconv($left_operand);
$modulus=ilMath::bcconv($modulus);
if (function_exists("bcmod"))
{
return bcmod($left_operand, $modulus);
}
else
{
return $left_operand % $modulus;
}
}

+ Here is the call graph for this function:

static ilMath::_mul (   $left_operand,
  $right_operand,
  $scale = 50 
)
static

Definition at line 124 of file class.ilMath.php.

References $res, and bcconv().

Referenced by assFormulaQuestionVariable\getBaseValue(), assFormulaQuestionVariable\getRandomValue(), assFormulaQuestionResult\getRangeMaxBase(), assFormulaQuestionResult\getRangeMinBase(), assFormulaQuestionResult\getReachedPoints(), assFormulaQuestionResult\getResultInfo(), assFormulaQuestionResult\isCorrect(), assFormulaQuestionResult\isInTolerance(), EvalMath\pfx(), and assFormulaQuestionResult\suggestRange().

{
$left_operand=ilMath::bcconv($left_operand);
$right_operand=ilMath::bcconv($right_operand);
$scale=ilMath::bcconv($scale);
if (function_exists("bcmul"))
{
return bcmul($left_operand, $right_operand, $scale);
}
else
{
$res = $left_operand * $right_operand;
if (is_numeric($scale)) $res = round($res, $scale);
return $res;
}
}

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

static ilMath::_pow (   $left_operand,
  $right_operand,
  $scale = 50 
)
static

Definition at line 145 of file class.ilMath.php.

References $res, bcconv(), and exp2dec().

Referenced by assFormulaQuestionVariable\getRandomValue(), and EvalMath\pfx().

{
$left_operand=ilMath::bcconv($left_operand);
$right_operand=ilMath::bcconv($right_operand);
$scale=ilMath::bcconv($scale);
if(function_exists("bcpow"))
{
// bcpow() only supports exponents less than or equal to 2^31-1.
// Also, bcpow() does not support decimal numbers.
// If you have scale set to 0, then the exponent is converted to an integer; otherwise an error is generated.
$left_operand_dec = ilMath::exp2dec($left_operand);
$right_operand_dec = ilMath::exp2dec($right_operand);
$is_exponent_decimal_number = strpos($right_operand_dec, '.') !== false;
// bcpow does NOT support decimal exponents
if(!$is_exponent_decimal_number)
{
return bcpow($left_operand_dec, $right_operand_dec, $scale);
}
}
$res = pow($left_operand, $right_operand);
if (is_numeric($scale)) $res = round($res, $scale);
return $res;
}

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

static ilMath::_round (   $value,
  $precision = 0 
)
static

Definition at line 285 of file class.ilMath.php.

Referenced by assFormulaQuestionResult\calculateFormula(), and assFormulaQuestionResult\isCorrect().

{
return number_format($value, $precision, '.', '');
}

+ Here is the caller graph for this function:

static ilMath::_sqrt (   $operand,
  $scale = 50 
)
static

Definition at line 176 of file class.ilMath.php.

References $res, and bcconv().

{
$operand=ilMath::bcconv($operand);
$scale=ilMath::bcconv($scale);
if (function_exists("bcsqrt"))
{
return bcsqrt($operand, $scale);
}
else
{
$res = sqrt($operand);
if (is_numeric($scale)) $res = round($res, $scale);
return $res;
}
}

+ Here is the call graph for this function:

static ilMath::_sub (   $left_operand,
  $right_operand,
  $scale = 50 
)
static

Definition at line 196 of file class.ilMath.php.

References $res, and bcconv().

Referenced by assFormulaQuestionVariable\getRandomValue(), assFormulaQuestionResult\isInTolerance(), and EvalMath\pfx().

{
$left_operand=ilMath::bcconv($left_operand);
$right_operand=ilMath::bcconv($right_operand);
$scale=ilMath::bcconv($scale);
if (function_exists("bcsub"))
{
return bcsub($left_operand, $right_operand, $scale);
}
else
{
$res = $left_operand - $right_operand;
if (is_numeric($scale)) $res = round($res, $scale);
return $res;
}
}

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

static ilMath::bcconv (   $fNumber)
staticprivate
Parameters
$fNumber
Returns
string function fixes problem which occur when locale ist set to de_DE for example, because bc* function expecting strings

Definition at line 301 of file class.ilMath.php.

References exp2dec().

Referenced by _add(), _comp(), _div(), _mod(), _mul(), _pow(), _sqrt(), and _sub().

{
$fNumber=ilMath::exp2dec($fNumber);
$locale_info = localeconv();
if($locale_info["decimal_point"] != ".")
{
$sAppend = '';
$iDecimals = ini_get('precision') - floor(log10(abs($fNumber)));
if (0 > $iDecimals)
{
$fNumber *= pow(10, $iDecimals);
$sAppend = str_repeat('0', -$iDecimals);
$iDecimals = 0;
}
return number_format($fNumber, $iDecimals, '.', '').$sAppend;
}
return $fNumber;
}

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

ilMath::exp2dec (   $float_str)

Definition at line 218 of file class.ilMath.php.

Referenced by _pow(), and bcconv().

{
// make sure its a standard php float string (i.e. change 0.2e+2 to 20)
// php will automatically format floats decimally if they are within a certain range
$original = $float_str; // store original float, so we can return a float keeping the pecision when possible
$float_str = (string)((float)($float_str));
$float_str = str_replace(",", ".", $float_str); // convert ',' to '.' (float casting was locale sensitive)
// if there is an E in the float string
if(($pos = strpos(strtolower($float_str), 'e')) !== false)
{
// get either side of the E, e.g. 1.6E+6 => exp E+6, num 1.6
$exp = substr($float_str, $pos+1);
$num = substr($float_str, 0, $pos);
// strip off num sign, if there is one, and leave it off if its + (not required)
if((($num_sign = $num[0]) === '+') || ($num_sign === '-')) $num = substr($num, 1);
else $num_sign = '';
if($num_sign === '+') $num_sign = '';
// strip off exponential sign ('+' or '-' as in 'E+6') if there is one, otherwise throw error, e.g. E+6 => '+'
if((($exp_sign = $exp[0]) === '+') || ($exp_sign === '-')) $exp = substr($exp, 1);
else trigger_error("Could not convert exponential notation to decimal notation: invalid float string '$float_str'", E_USER_ERROR);
// get the number of decimal places to the right of the decimal point (or 0 if there is no dec point), e.g., 1.6 => 1
$right_dec_places = (($dec_pos = strpos($num, '.')) === false) ? 0 : strlen(substr($num, $dec_pos+1));
// get the number of decimal places to the left of the decimal point (or the length of the entire num if there is no dec point), e.g. 1.6 => 1
$left_dec_places = ($dec_pos === false) ? strlen($num) : strlen(substr($num, 0, $dec_pos));
// work out number of zeros from exp, exp sign and dec places, e.g. exp 6, exp sign +, dec places 1 => num zeros 5
if($exp_sign === '+') $num_zeros = $exp - $right_dec_places;
else $num_zeros = $exp - $left_dec_places;
// build a string with $num_zeros zeros, e.g. '0' 5 times => '00000'
$zeros = str_pad('', $num_zeros, '0');
// strip decimal from num, e.g. 1.6 => 16
if($dec_pos !== false) $num = str_replace('.', '', $num);
// if positive exponent, return like 1600000
if($exp_sign === '+') return $num_sign.$num.$zeros;
// if negative exponent, return like 0.0000016
else return $num_sign.'0.'.$zeros.$num;
}
// otherwise, assume already in decimal notation and return
else return $original;
}

+ Here is the caller graph for this function:

static ilMath::getGreatestCommonDivisor (   $a,
  $b 
)
static

Definition at line 273 of file class.ilMath.php.

Referenced by isCoprimeFraction().

{
if ($b > 0)
{
return self::getGreatestCommonDivisor($b, $a % $b);
}
else
{
return $a;
}
}

+ Here is the caller graph for this function:

static ilMath::isCoprimeFraction (   $numerator,
  $denominator 
)
static

Definition at line 266 of file class.ilMath.php.

References getGreatestCommonDivisor().

{
$gcd = self::getGreatestCommonDivisor(abs($numerator), abs($denominator));
return $gcd == 1 ? true : false;
}

+ Here is the call graph for this function:


The documentation for this class was generated from the following file: