ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
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$

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

Member Function Documentation

◆ _add()

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

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

38 {
39 $left_operand=ilMath::bcconv($left_operand);
40 $right_operand=ilMath::bcconv($right_operand);
41 $scale=ilMath::bcconv($scale);
42
43 if (function_exists("bcadd"))
44 {
45 return bcadd($left_operand, $right_operand, $scale);
46 }
47 else
48 {
49 $res = $left_operand + $right_operand;
50 if (is_numeric($scale)) $res = round($res, $scale);
51 return $res;
52 }
53 }
static bcconv($fNumber)

References $res, and bcconv().

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

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ _comp()

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

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

59 {
60 $left_operand=ilMath::bcconv($left_operand);
61 $right_operand=ilMath::bcconv($right_operand);
62 $scale=ilMath::bcconv($scale);
63
64 if (function_exists("bccomp"))
65 {
66 return bccomp($left_operand, $right_operand, $scale);
67 }
68 else
69 {
70 if (is_numeric($scale))
71 {
72 $left_operand = round($left_operand, $scale);
73 $right_operand = round($right_operand, $scale);
74 }
75 if ($left_operand == $right_operand) return 0;
76 if ($left_operand > $right_operand) return 1;
77 return -1;
78 }
79 }

References bcconv().

Referenced by _equals().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ _div()

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

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

85 {
86 $left_operand=ilMath::bcconv($left_operand);
87 $right_operand=ilMath::bcconv($right_operand);
88 $scale=ilMath::bcconv($scale);
89
90 if (function_exists("bcdiv"))
91 {
92 return bcdiv($left_operand, $right_operand, $scale);
93 }
94 else
95 {
96 if ($right_operand == 0) return NULL;
97 $res = $left_operand / $right_operand;
98 if (is_numeric($scale)) $res = round($res, $scale);
99 return $res;
100 }
101 }

References $res, and bcconv().

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

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ _equals()

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

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

291 {
292 return self::_comp($value1, $value2, $scale) === 0;
293 }
static _comp($left_operand, $right_operand, $scale=50)

References _comp().

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

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ _mod()

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

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

107 {
108 $left_operand=ilMath::bcconv($left_operand);
109 $modulus=ilMath::bcconv($modulus);
110
111 if (function_exists("bcmod"))
112 {
113 return bcmod($left_operand, $modulus);
114 }
115 else
116 {
117 return $left_operand % $modulus;
118 }
119 }

References bcconv().

+ Here is the call graph for this function:

◆ _mul()

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

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

125 {
126 $left_operand=ilMath::bcconv($left_operand);
127 $right_operand=ilMath::bcconv($right_operand);
128 $scale=ilMath::bcconv($scale);
129
130 if (function_exists("bcmul"))
131 {
132 return bcmul($left_operand, $right_operand, $scale);
133 }
134 else
135 {
136 $res = $left_operand * $right_operand;
137 if (is_numeric($scale)) $res = round($res, $scale);
138 return $res;
139 }
140 }

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().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ _pow()

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

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

146 {
147 $left_operand=ilMath::bcconv($left_operand);
148 $right_operand=ilMath::bcconv($right_operand);
149 $scale=ilMath::bcconv($scale);
150
151 if(function_exists("bcpow"))
152 {
153 // bcpow() only supports exponents less than or equal to 2^31-1.
154 // Also, bcpow() does not support decimal numbers.
155 // If you have scale set to 0, then the exponent is converted to an integer; otherwise an error is generated.
156 $left_operand_dec = ilMath::exp2dec($left_operand);
157 $right_operand_dec = ilMath::exp2dec($right_operand);
158
159 $is_exponent_decimal_number = strpos($right_operand_dec, '.') !== false;
160
161 // bcpow does NOT support decimal exponents
162 if(!$is_exponent_decimal_number)
163 {
164 return bcpow($left_operand_dec, $right_operand_dec, $scale);
165 }
166 }
167
168 $res = pow($left_operand, $right_operand);
169 if (is_numeric($scale)) $res = round($res, $scale);
170 return $res;
171 }
exp2dec($float_str)

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

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

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ _round()

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

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

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

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

+ Here is the caller graph for this function:

◆ _sqrt()

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

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

177 {
178 $operand=ilMath::bcconv($operand);
179 $scale=ilMath::bcconv($scale);
180
181 if (function_exists("bcsqrt"))
182 {
183 return bcsqrt($operand, $scale);
184 }
185 else
186 {
187 $res = sqrt($operand);
188 if (is_numeric($scale)) $res = round($res, $scale);
189 return $res;
190 }
191 }

References $res, and bcconv().

+ Here is the call graph for this function:

◆ _sub()

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

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

197 {
198 $left_operand=ilMath::bcconv($left_operand);
199 $right_operand=ilMath::bcconv($right_operand);
200 $scale=ilMath::bcconv($scale);
201
202 if (function_exists("bcsub"))
203 {
204 return bcsub($left_operand, $right_operand, $scale);
205 }
206 else
207 {
208 $res = $left_operand - $right_operand;
209 if (is_numeric($scale)) $res = round($res, $scale);
210 return $res;
211 }
212 }

References $res, and bcconv().

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

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ bcconv()

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.

302 {
303 $fNumber=ilMath::exp2dec($fNumber);
304 $locale_info = localeconv();
305 if($locale_info["decimal_point"] != ".")
306 {
307 $sAppend = '';
308 $iDecimals = ini_get('precision') - floor(log10(abs($fNumber)));
309 if (0 > $iDecimals)
310 {
311 $fNumber *= pow(10, $iDecimals);
312 $sAppend = str_repeat('0', -$iDecimals);
313 $iDecimals = 0;
314 }
315 return number_format($fNumber, $iDecimals, '.', '').$sAppend;
316 }
317 return $fNumber;
318 }

References exp2dec().

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

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ exp2dec()

ilMath::exp2dec (   $float_str)

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

219 {
220 // make sure its a standard php float string (i.e. change 0.2e+2 to 20)
221 // php will automatically format floats decimally if they are within a certain range
222 $original = $float_str; // store original float, so we can return a float keeping the pecision when possible
223 $float_str = (string)((float)($float_str));
224 $float_str = str_replace(",", ".", $float_str); // convert ',' to '.' (float casting was locale sensitive)
225
226 // if there is an E in the float string
227 if(($pos = strpos(strtolower($float_str), 'e')) !== false)
228 {
229 // get either side of the E, e.g. 1.6E+6 => exp E+6, num 1.6
230 $exp = substr($float_str, $pos+1);
231 $num = substr($float_str, 0, $pos);
232
233 // strip off num sign, if there is one, and leave it off if its + (not required)
234 if((($num_sign = $num[0]) === '+') || ($num_sign === '-')) $num = substr($num, 1);
235 else $num_sign = '';
236 if($num_sign === '+') $num_sign = '';
237
238 // strip off exponential sign ('+' or '-' as in 'E+6') if there is one, otherwise throw error, e.g. E+6 => '+'
239 if((($exp_sign = $exp[0]) === '+') || ($exp_sign === '-')) $exp = substr($exp, 1);
240 else trigger_error("Could not convert exponential notation to decimal notation: invalid float string '$float_str'", E_USER_ERROR);
241
242 // 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
243 $right_dec_places = (($dec_pos = strpos($num, '.')) === false) ? 0 : strlen(substr($num, $dec_pos+1));
244 // 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
245 $left_dec_places = ($dec_pos === false) ? strlen($num) : strlen(substr($num, 0, $dec_pos));
246
247 // work out number of zeros from exp, exp sign and dec places, e.g. exp 6, exp sign +, dec places 1 => num zeros 5
248 if($exp_sign === '+') $num_zeros = $exp - $right_dec_places;
249 else $num_zeros = $exp - $left_dec_places;
250
251 // build a string with $num_zeros zeros, e.g. '0' 5 times => '00000'
252 $zeros = str_pad('', $num_zeros, '0');
253
254 // strip decimal from num, e.g. 1.6 => 16
255 if($dec_pos !== false) $num = str_replace('.', '', $num);
256
257 // if positive exponent, return like 1600000
258 if($exp_sign === '+') return $num_sign.$num.$zeros;
259 // if negative exponent, return like 0.0000016
260 else return $num_sign.'0.'.$zeros.$num;
261 }
262 // otherwise, assume already in decimal notation and return
263 else return $original;
264 }

Referenced by _pow(), and bcconv().

+ Here is the caller graph for this function:

◆ getGreatestCommonDivisor()

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

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

274 {
275 if ($b > 0)
276 {
277 return self::getGreatestCommonDivisor($b, $a % $b);
278 }
279 else
280 {
281 return $a;
282 }
283 }
static getGreatestCommonDivisor($a, $b)

References getGreatestCommonDivisor().

Referenced by getGreatestCommonDivisor(), and isCoprimeFraction().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ isCoprimeFraction()

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

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

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

References getGreatestCommonDivisor().

+ Here is the call graph for this function:

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