ILIAS  release_8 Revision v8.19-1-g4e8f2f9140c
All Data Structures Namespaces Files Functions Variables Modules Pages
ilMathBaseAdapter Class Reference

This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Learning e.V. More...

+ Inheritance diagram for ilMathBaseAdapter:
+ Collaboration diagram for ilMathBaseAdapter:

Public Member Functions

 applyScale ($left_operand, int $scale=null)
 
 round ($value, int $precision=0)
 
 equals ($left_operand, $right_operand, int $scale=null)
 
- Public Member Functions inherited from ilMathAdapter
 add ($left_operand, $right_operand, int $scale=null)
 Adds two numbers. More...
 
 sub ($left_operand, $right_operand, int $scale=null)
 Subtracts two numbers. More...
 
 mul ($left_operand, $right_operand, int $scale=null)
 Multiplies two numbers. More...
 
 div ($left_operand, $right_operand, int $scale=null)
 Divides two numbers. More...
 
 mod ($left_operand, $right_operand)
 Gets modulus of two numbers. More...
 
 pow ($left_operand, $right_operand, int $scale=null)
 Raises a number to another. More...
 
 sqrt ($operand, int $scale=null)
 Gets the square root of a number. More...
 
 comp ($left_operand, $right_operand, int $scale=null)
 Compares two numbers. More...
 

Protected Member Functions

 normalize ($number)
 This function fixes problems which occur when locale ist set to de_DE for example, because bc* function expecting strings. More...
 
 exp2dec ($float_str)
 

Detailed Description

This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Learning e.V.

ILIAS is licensed with the GPL-3.0, see https://www.gnu.org/licenses/gpl-3.0.en.html You should have received a copy of said license along with the source code, too.

If this is not the case or you just want to try ILIAS, you'll find us at: https://www.ilias.de https://github.com/ILIAS-eLearning Class ilMathBaseAdapter

Author
Michael Jansen mjans.nosp@m.en@d.nosp@m.ataba.nosp@m.y.de

Definition at line 23 of file class.ilMathBaseAdapter.php.

Member Function Documentation

◆ applyScale()

ilMathBaseAdapter::applyScale (   $left_operand,
int  $scale = null 
)

Implements ilMathAdapter.

Definition at line 28 of file class.ilMathBaseAdapter.php.

References exp2dec(), and ILIAS\Repository\int().

Referenced by ilMathPhpAdapter\add(), ilMathPhpAdapter\comp(), ilMathPhpAdapter\div(), ilMathPhpAdapter\mul(), ilMathBCMathAdapter\pow(), ilMathPhpAdapter\pow(), ilMathPhpAdapter\sqrt(), and ilMathPhpAdapter\sub().

28  : string
29  {
30  if (is_numeric($left_operand)) {
31  $scale = (int) $scale;
32 
33  $left_operand = $this->exp2dec($left_operand);
34  if (strpos($left_operand, '.') === false) {
35  $number_of_decimals = 0;
36  } else {
37  $number_of_decimals = strlen(substr($left_operand, strpos($left_operand, '.') + 1));
38  }
39 
40  if ($number_of_decimals > 0 && $number_of_decimals < $scale) {
41  $left_operand = str_pad($left_operand, $scale - $number_of_decimals, '0');
42  } elseif ($number_of_decimals > $scale) {
43  $left_operand = substr($left_operand, 0, -($number_of_decimals - $scale));
44  }
45  }
46 
47  return $left_operand;
48  }
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ equals()

ilMathBaseAdapter::equals (   $left_operand,
  $right_operand,
int  $scale = null 
)

Implements ilMathAdapter.

Definition at line 61 of file class.ilMathBaseAdapter.php.

References ilMathAdapter\comp().

61  : bool
62  {
63  return $this->comp($left_operand, $right_operand, $scale) === 0;
64  }
comp($left_operand, $right_operand, int $scale=null)
Compares two numbers.
+ Here is the call graph for this function:

◆ exp2dec()

ilMathBaseAdapter::exp2dec (   $float_str)
protected
Parameters
float | string | int$float_str
Returns
string

Definition at line 101 of file class.ilMathBaseAdapter.php.

Referenced by applyScale(), normalize(), and ilMathBCMathAdapter\pow().

101  : string
102  {
103  // make sure its a standard php float string (i.e. change 0.2e+2 to 20)
104  // php will automatically format floats decimally if they are within a certain range
105  $original = $float_str; // store original float, so we can return a float keeping the pecision when possible
106  $float_str = (string) ((float) ($float_str));
107  $float_str = str_replace(",", ".", $float_str); // convert ',' to '.' (float casting was locale sensitive)
108 
109  // if there is an E in the float string
110  if (($pos = stripos($float_str, 'e')) !== false) {
111  // get either side of the E, e.g. 1.6E+6 => exp E+6, num 1.6
112  $exp = substr($float_str, $pos + 1);
113  $num = substr($float_str, 0, $pos);
114 
115  // strip off num sign, if there is one, and leave it off if its + (not required)
116  if ((($num_sign = $num[0]) === '+') || ($num_sign === '-')) {
117  $num = substr($num, 1);
118  } else {
119  $num_sign = '';
120  }
121  if ($num_sign === '+') {
122  $num_sign = '';
123  }
124 
125  // strip off exponential sign ('+' or '-' as in 'E+6') if there is one, otherwise throw error, e.g. E+6 => '+'
126  if ((($exp_sign = $exp[0]) === '+') || ($exp_sign === '-')) {
127  $exp = substr($exp, 1);
128  } else {
129  trigger_error("Could not convert exponential notation to decimal notation: invalid float string '$float_str'", E_USER_ERROR);
130  }
131 
132  // 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
133  $right_dec_places = (($dec_pos = strpos($num, '.')) === false) ? 0 : strlen(substr($num, $dec_pos + 1));
134  // 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
135  $left_dec_places = ($dec_pos === false) ? strlen($num) : strlen(substr($num, 0, $dec_pos));
136 
137  // work out number of zeros from exp, exp sign and dec places, e.g. exp 6, exp sign +, dec places 1 => num zeros 5
138  if ($exp_sign === '+') {
139  $num_zeros = $exp - $right_dec_places;
140  } else {
141  $num_zeros = $exp - $left_dec_places;
142  }
143 
144  // build a string with $num_zeros zeros, e.g. '0' 5 times => '00000'
145  $zeros = str_pad('', $num_zeros, '0');
146 
147  // strip decimal from num, e.g. 1.6 => 16
148  if ($dec_pos !== false) {
149  $num = str_replace('.', '', $num);
150  }
151 
152  // if positive exponent, return like 1600000
153  if ($exp_sign === '+') {
154  return $num_sign . $num . $zeros;
155  }
156 
157  // if negative exponent, return like 0.0000016
158  return $num_sign . '0.' . $zeros . $num;
159  }
160 
161  // otherwise, assume already in decimal notation and return
162  return $original;
163  }
+ Here is the caller graph for this function:

◆ normalize()

ilMathBaseAdapter::normalize (   $number)
protected

This function fixes problems which occur when locale ist set to de_DE for example, because bc* function expecting strings.

Parameters
mixed$number
Returns
string

Definition at line 72 of file class.ilMathBaseAdapter.php.

References exp2dec(), and ILIAS\Repository\int().

Referenced by ilMathBCMathAdapter\add(), ilMathPhpAdapter\add(), ilMathPhpAdapter\comp(), ilMathBCMathAdapter\div(), ilMathPhpAdapter\div(), ilMathBCMathAdapter\mod(), ilMathPhpAdapter\mod(), ilMathBCMathAdapter\mul(), ilMathPhpAdapter\mul(), ilMathBCMathAdapter\pow(), ilMathPhpAdapter\pow(), ilMathPhpAdapter\sqrt(), ilMathBCMathAdapter\sub(), and ilMathPhpAdapter\sub().

72  : ?string
73  {
74  if (null === $number) {
75  return null;
76  }
77 
78  $number = trim((string) $number);
79  $number = $this->exp2dec($number);
80  $locale_info = localeconv();
81 
82  if ($locale_info['decimal_point'] !== '.') {
83  $append = '';
84  $number_of_decimals = (int) ini_get('precision') - (int) floor(log10(abs($number)));
85  if (0 > $number_of_decimals) {
86  $number *= 10 ** $number_of_decimals;
87  $append = str_repeat('0', -$number_of_decimals);
88  $number_of_decimals = 0;
89  }
90 
91  return number_format($number, $number_of_decimals, '.', '') . $append;
92  }
93 
94  return $number;
95  }
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ round()

ilMathBaseAdapter::round (   $value,
int  $precision = 0 
)

Implements ilMathAdapter.

Definition at line 53 of file class.ilMathBaseAdapter.php.

53  : string
54  {
55  return number_format($value, $precision, '.', '');
56  }

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