ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
ilMathPhpAdapter 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 ilMathPhpAdapter:
+ Collaboration diagram for ilMathPhpAdapter:

Public Member Functions

 add ($left_operand, $right_operand, ?int $scale=null)
 @inheritDoc More...
 
 sub ($left_operand, $right_operand, ?int $scale=null)
 @inheritDoc More...
 
 mul ($left_operand, $right_operand, ?int $scale=null)
 @inheritDoc More...
 
 div ($left_operand, $right_operand, ?int $scale=null)
 @inheritDoc More...
 
 mod ($left_operand, $right_operand)
 @inheritDoc More...
 
 pow ($left_operand, $right_operand, ?int $scale=null)
 @inheritDoc More...
 
 sqrt ($operand, ?int $scale=null)
 @inheritDoc More...
 
 comp ($left_operand, $right_operand, ?int $scale=null)
 @inheritDoc More...
 
- Public Member Functions inherited from ilMathBaseAdapter
 applyScale ($left_operand, ?int $scale=null)
 @inheritDoc More...
 
 round ($value, int $precision=0)
 @inheritDoc More...
 
 equals ($left_operand, $right_operand, ?int $scale=null)
 @inheritDoc More...
 
 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...
 
 equals ($left_operand, $right_operand, ?int $scale=null)
 Checks whether or not two numbers are identical. More...
 
 applyScale ($left_operand, ?int $scale=null)
 This method adapts the behaviour of bcscale() More...
 
 round ($value, int $precision=0)
 

Private Member Functions

 transformToNumeric (?string $operand)
 

Additional Inherited Members

- Protected Member Functions inherited from ilMathBaseAdapter
 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 ilMathPhpAdapter

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

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

Member Function Documentation

◆ add()

ilMathPhpAdapter::add (   $left_operand,
  $right_operand,
?int  $scale = null 
)

@inheritDoc

Implements ilMathAdapter.

Definition at line 45 of file class.ilMathPhpAdapter.php.

46 {
47 $res = $this->normalize($left_operand) + $this->normalize($right_operand);
48
49 return $this->applyScale($res, $this->normalize($scale));
50 }
applyScale($left_operand, ?int $scale=null)
@inheritDoc
normalize($number)
This function fixes problems which occur when locale ist set to de_DE for example,...
$res
Definition: ltiservices.php:69

References $res, ilMathBaseAdapter\applyScale(), and ilMathBaseAdapter\normalize().

+ Here is the call graph for this function:

◆ comp()

ilMathPhpAdapter::comp (   $left_operand,
  $right_operand,
?int  $scale = null 
)

@inheritDoc

Implements ilMathAdapter.

Definition at line 154 of file class.ilMathPhpAdapter.php.

155 {
156 $left_operand = $this->normalize($left_operand);
157 $right_operand = $this->normalize($right_operand);
158 $scale = $this->normalize($scale);
159
160 if (is_numeric($scale)) {
161 $left_operand = $this->applyScale($left_operand, $scale);
162 $right_operand = $this->applyScale($right_operand, $scale);
163 }
164
165 if ($left_operand == $right_operand) {
166 return 0;
167 }
168
169 if ($left_operand > $right_operand) {
170 return 1;
171 }
172
173 return -1;
174 }

References ilMathBaseAdapter\applyScale(), and ilMathBaseAdapter\normalize().

+ Here is the call graph for this function:

◆ div()

ilMathPhpAdapter::div (   $left_operand,
  $right_operand,
?int  $scale = null 
)

@inheritDoc

Implements ilMathAdapter.

Definition at line 91 of file class.ilMathPhpAdapter.php.

92 {
93 if ($right_operand == 0) {
94 throw new ilMathDivisionByZeroException(sprintf("Division of %s by %s not possible!", $left_operand, $right_operand));
95 }
96
97 // This ensures the old PHP <= 7.0.x behaviour, see: #27785 / #26361
98 try {
99 $left_operand = $this->normalize($left_operand);
100 $right_operand = $this->normalize($right_operand);
101
102 $left_operand = $this->transformToNumeric($left_operand);
103 $right_operand = $this->transformToNumeric($right_operand);
104
105 $res = $left_operand / $right_operand;
106
107 $division = $this->applyScale($res, $this->normalize($scale));
108 } catch (Throwable $e) {
109 if (strpos($e->getMessage(), 'A non-numeric value encountered') !== false) {
110 $division = 0;
111 } else {
112 throw $e;
113 }
114 }
115
116 return $division;
117 }
transformToNumeric(?string $operand)

References Vendor\Package\$e, $res, ilMathBaseAdapter\applyScale(), ilMathBaseAdapter\normalize(), and transformToNumeric().

+ Here is the call graph for this function:

◆ mod()

ilMathPhpAdapter::mod (   $left_operand,
  $right_operand 
)

@inheritDoc

Implements ilMathAdapter.

Definition at line 122 of file class.ilMathPhpAdapter.php.

122 : int
123 {
124 if ($right_operand == 0) {
125 throw new ilMathDivisionByZeroException(sprintf("Division of %s by %s not possible!", $left_operand, $right_operand));
126 }
127
128 return $this->normalize($left_operand) % $this->normalize($right_operand);
129 }

References ilMathBaseAdapter\normalize().

+ Here is the call graph for this function:

◆ mul()

ilMathPhpAdapter::mul (   $left_operand,
  $right_operand,
?int  $scale = null 
)

@inheritDoc

Implements ilMathAdapter.

Definition at line 65 of file class.ilMathPhpAdapter.php.

66 {
67 try {
68 $left_operand = $this->normalize($left_operand);
69 $right_operand = $this->normalize($right_operand);
70
71 $left_operand = $this->transformToNumeric($left_operand);
72 $right_operand = $this->transformToNumeric($right_operand);
73
74 $res = $left_operand * $right_operand;
75
76 $multiplication = $this->applyScale($res, $this->normalize($scale));
77 } catch (Throwable $e) {
78 if (strpos($e->getMessage(), 'A non-numeric value encountered') !== false) {
79 $multiplication = 0;
80 } else {
81 throw $e;
82 }
83 }
84
85 return $multiplication;
86 }

References Vendor\Package\$e, $res, ilMathBaseAdapter\applyScale(), ilMathBaseAdapter\normalize(), and transformToNumeric().

+ Here is the call graph for this function:

◆ pow()

ilMathPhpAdapter::pow (   $left_operand,
  $right_operand,
?int  $scale = null 
)

@inheritDoc

Implements ilMathAdapter.

Definition at line 134 of file class.ilMathPhpAdapter.php.

135 {
136 $res = pow($this->normalize($left_operand), $this->normalize($right_operand));
137
138 return $this->applyScale($res, $this->normalize($scale));
139 }
pow($left_operand, $right_operand, ?int $scale=null)
@inheritDoc

References $res, ilMathBaseAdapter\applyScale(), ilMathBaseAdapter\normalize(), and pow().

Referenced by pow().

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

◆ sqrt()

ilMathPhpAdapter::sqrt (   $operand,
?int  $scale = null 
)

@inheritDoc

Implements ilMathAdapter.

Definition at line 144 of file class.ilMathPhpAdapter.php.

145 {
146 $res = sqrt($this->normalize($operand));
147
148 return $this->applyScale($res, $this->normalize($scale));
149 }
sqrt($operand, ?int $scale=null)
@inheritDoc

References $res, ilMathBaseAdapter\applyScale(), ilMathBaseAdapter\normalize(), and sqrt().

Referenced by sqrt().

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

◆ sub()

ilMathPhpAdapter::sub (   $left_operand,
  $right_operand,
?int  $scale = null 
)

@inheritDoc

Implements ilMathAdapter.

Definition at line 55 of file class.ilMathPhpAdapter.php.

56 {
57 $res = $this->normalize($left_operand) - $this->normalize($right_operand);
58
59 return $this->applyScale($res, $this->normalize($scale));
60 }

References $res, ilMathBaseAdapter\applyScale(), and ilMathBaseAdapter\normalize().

+ Here is the call graph for this function:

◆ transformToNumeric()

ilMathPhpAdapter::transformToNumeric ( ?string  $operand)
private
Parameters
string | null$operand
Returns
float|int|string|null

Definition at line 29 of file class.ilMathPhpAdapter.php.

30 {
31 if (is_string($operand)) {
32 if (strpos($operand, '.') !== false) {
33 $operand = (float) $operand;
34 } else {
35 $operand = (int) $operand;
36 }
37 }
38
39 return $operand;
40 }

References ILIAS\Repository\int().

Referenced by div(), and mul().

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

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