ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
class.ilMath.php
Go to the documentation of this file.
1<?php
2/* Copyright (c) 1998-2016 ILIAS open source, Extended GPL, see docs/LICENSE */
3
11class ilMath
12{
16 protected static $default_adapter = null;
17
24 public static function _add($left_operand, $right_operand, $scale = 50)
25 {
26 $adapter = static::getDefaultAdapter();
27
28 return $adapter->add($left_operand, $right_operand, $scale);
29 }
30
37 public static function _div($left_operand, $right_operand, $scale = 50)
38 {
39 $adapter = static::getDefaultAdapter();
40
41 return $adapter->div($left_operand, $right_operand, $scale);
42 }
43
49 public static function _mod($operand, $modulu)
50 {
51 $adapter = static::getDefaultAdapter();
52
53 return $adapter->mod($operand, $modulu);
54 }
55
62 public static function _mul($left_operand, $right_operand, $scale = 50)
63 {
64 $adapter = static::getDefaultAdapter();
65
66 return $adapter->mul($left_operand, $right_operand, $scale);
67 }
68
75 public static function _pow($left_operand, $right_operand, $scale = 50)
76 {
77 $adapter = static::getDefaultAdapter();
78
79 return $adapter->pow($left_operand, $right_operand, $scale);
80 }
81
87 public static function _sqrt($operand, $scale = 50)
88 {
89 $adapter = static::getDefaultAdapter();
90
91 return $adapter->sqrt($operand, $scale);
92 }
93
100 public static function _sub($left_operand, $right_operand, $scale = 50)
101 {
102 $adapter = static::getDefaultAdapter();
103
104 return $adapter->sub($left_operand, $right_operand, $scale);
105 }
106
107 public static function isCoprimeFraction($numerator, $denominator)
108 {
109 $gcd = self::getGreatestCommonDivisor(abs($numerator), abs($denominator));
110
111 return $gcd == 1 ? true : false;
112 }
113
119 public static function getGreatestCommonDivisor($a, $b)
120 {
121 if ($b > 0) {
122 return self::getGreatestCommonDivisor($b, $a % $b);
123 } else {
124 return $a;
125 }
126 }
127
132 {
133 static::$default_adapter = $adapter;
134 }
135
139 public static function getDefaultAdapter()
140 {
141 if (null === static::$default_adapter) {
142 static::$default_adapter = static::getFirstValidAdapter();
143 }
144
145 return static::$default_adapter;
146 }
147
153 public static function getInstance($adapter = null)
154 {
155 if (null === $adapter) {
156 return static::getFirstValidAdapter();
157 }
158
159 $class_name = 'ilMath' . $adapter . 'Adapter';
160 $path_to_class = realpath('Services/Math/classes/class.' . $class_name . '.php');
161
162 if (!is_file($path_to_class) || !is_readable($path_to_class)) {
163 require_once 'Services/Math/exceptions/class.ilMathException.php';
164 throw new ilMathException(sprintf(
165 'The math adapter %s is not valid, please refer to a class implementing %s',
166 $adapter,
167 ilMathAdapter::class
168 ));
169 }
170
171 require_once $path_to_class;
172 if (!class_exists($class_name) || !is_subclass_of($class_name, ilMathAdapter::class)) {
173 require_once 'Services/Math/exceptions/class.ilMathException.php';
174 throw new ilMathException(sprintf(
175 'The math adapter class %s is not valid, please refer to a class implementing %s',
176 $class_name,
177 ilMathAdapter::class
178 ));
179 }
180
181 return new $class_name();
182 }
183
187 public static function getFirstValidAdapter()
188 {
189 if (extension_loaded('bcmath')) {
190 return static::getInstance('BCMath');
191 }
192
193 return static::getInstance('Php');
194 }
195
202 public static function __callStatic($method, $args)
203 {
204 if (strpos($method, '_') === 0) {
205 $method = substr($method, 1);
206 }
207
208 $adapter = static::getDefaultAdapter();
209
210 return call_user_func_array([$adapter, $method], $args);
211 }
212}
sprintf('%.4f', $callTime)
An exception for terminatinating execution or to throw for unit testing.
Class ilMathException.
Class ilMath Wrapper for mathematical operations.
static isCoprimeFraction($numerator, $denominator)
static _mod($operand, $modulu)
static _mul($left_operand, $right_operand, $scale=50)
static getDefaultAdapter()
static getFirstValidAdapter()
static _sqrt($operand, $scale=50)
static __callStatic($method, $args)
Backward compatibility: Map all static calls to an equivalent instance method of the adapter.
static $default_adapter
static setDefaultAdapter(ilMathAdapter $adapter)
static _div($left_operand, $right_operand, $scale=50)
static _pow($left_operand, $right_operand, $scale=50)
static getGreatestCommonDivisor($a, $b)
static _sub($left_operand, $right_operand, $scale=50)
static _add($left_operand, $right_operand, $scale=50)
static getInstance($adapter=null)
Interface ilMathAdapter.