ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilMathPhpAdapter.php
Go to the documentation of this file.
1 <?php
2 
24 {
29  private function transformToNumeric(?string $operand)
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  }
41 
45  public function add($left_operand, $right_operand, int $scale = null)
46  {
47  $res = $this->normalize($left_operand) + $this->normalize($right_operand);
48 
49  return $this->applyScale($res, $this->normalize($scale));
50  }
51 
55  public function sub($left_operand, $right_operand, int $scale = null)
56  {
57  $res = $this->normalize($left_operand) - $this->normalize($right_operand);
58 
59  return $this->applyScale($res, $this->normalize($scale));
60  }
61 
65  public function mul($left_operand, $right_operand, int $scale = null)
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  }
87 
91  public function div($left_operand, $right_operand, int $scale = null)
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  }
118 
122  public function mod($left_operand, $right_operand): 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  }
130 
134  public function pow($left_operand, $right_operand, int $scale = null)
135  {
136  $res = pow($this->normalize($left_operand), $this->normalize($right_operand));
137 
138  return $this->applyScale($res, $this->normalize($scale));
139  }
140 
144  public function sqrt($operand, int $scale = null)
145  {
146  $res = sqrt($this->normalize($operand));
147 
148  return $this->applyScale($res, $this->normalize($scale));
149  }
150 
154  public function comp($left_operand, $right_operand, int $scale = null)
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  }
175 }
mod($left_operand, $right_operand)
$res
Definition: ltiservices.php:69
comp($left_operand, $right_operand, int $scale=null)
pow($left_operand, $right_operand, int $scale=null)
sqrt($operand, int $scale=null)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
normalize($number)
This function fixes problems which occur when locale ist set to de_DE for example, because bc* function expecting strings.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
mul($left_operand, $right_operand, int $scale=null)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
applyScale($left_operand, int $scale=null)
add($left_operand, $right_operand, int $scale=null)
transformToNumeric(?string $operand)
sub($left_operand, $right_operand, int $scale=null)
div($left_operand, $right_operand, int $scale=null)