ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilMath.php
Go to the documentation of this file.
1<?php
2
23class ilMath
24{
25 protected static ?ilMathAdapter $default_adapter = null;
26
32 public static function _add($left_operand, $right_operand, int $scale = 50)
33 {
34 return static::getDefaultAdapter()->add($left_operand, $right_operand, $scale);
35 }
36
43 public static function _div($left_operand, $right_operand, int $scale = 50)
44 {
45 return static::getDefaultAdapter()->div($left_operand, $right_operand, $scale);
46 }
47
53 public static function _mod($operand, $modulu): int
54 {
55 return static::getDefaultAdapter()->mod($operand, $modulu);
56 }
57
63 public static function _mul($left_operand, $right_operand, int $scale = 50)
64 {
65 return static::getDefaultAdapter()->mul($left_operand, $right_operand, $scale);
66 }
67
73 public static function _pow($left_operand, $right_operand, int $scale = 50)
74 {
75 return static::getDefaultAdapter()->pow($left_operand, $right_operand, $scale);
76 }
77
82 public static function _sqrt($operand, int $scale = 50)
83 {
84 return static::getDefaultAdapter()->sqrt($operand, $scale);
85 }
86
92 public static function _sub($left_operand, $right_operand, int $scale = 50)
93 {
94 return static::getDefaultAdapter()->sub($left_operand, $right_operand, $scale);
95 }
96
101 public static function isCoprimeFraction($numerator, $denominator): bool
102 {
103 $gcd = self::getGreatestCommonDivisor(abs($numerator), abs($denominator));
104
105 return $gcd == 1;
106 }
107
113 public static function getGreatestCommonDivisor($a, $b)
114 {
115 if ($b > 0) {
117 }
118
119 return $a;
120 }
121
122 public static function setDefaultAdapter(ilMathAdapter $adapter): void
123 {
124 static::$default_adapter = $adapter;
125 }
126
127 public static function getDefaultAdapter(): ilMathAdapter
128 {
129 if (null === static::$default_adapter) {
130 static::$default_adapter = static::getFirstValidAdapter();
131 }
132
133 return static::$default_adapter;
134 }
135
139 public static function getInstance(?string $adapter = null): \ilMathAdapter
140 {
141 if (null === $adapter) {
142 return static::getFirstValidAdapter();
143 }
144
145 $class_name = 'ilMath' . $adapter . 'Adapter';
146 $path_to_class = __DIR__ . '/class.' . $class_name . '.php';
147
148 if (!is_file($path_to_class) || !is_readable($path_to_class)) {
149 throw new ilMathException(sprintf(
150 'The math adapter %s is not valid, please refer to a class implementing %s',
151 $adapter,
152 ilMathAdapter::class
153 ));
154 }
155 if (!class_exists($class_name) || !is_subclass_of($class_name, ilMathAdapter::class)) {
156 throw new ilMathException(sprintf(
157 'The math adapter class %s is not valid, please refer to a class implementing %s',
158 $class_name,
159 ilMathAdapter::class
160 ));
161 }
162
163 return new $class_name();
164 }
165
169 public static function getFirstValidAdapter(): ilMathAdapter
170 {
171 if (extension_loaded('bcmath')) {
172 return static::getInstance('BCMath');
173 }
174
175 return static::getInstance('Php');
176 }
177
184 public static function __callStatic(string $method, $args)
185 {
186 if (strpos($method, '_') === 0) {
187 $method = substr($method, 1);
188 }
189
190 $adapter = static::getDefaultAdapter();
191
192 return call_user_func_array([$adapter, $method], $args);
193 }
194}
Class ilMathException.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static isCoprimeFraction($numerator, $denominator)
static _sub($left_operand, $right_operand, int $scale=50)
static _mod($operand, $modulu)
static _pow($left_operand, $right_operand, int $scale=50)
static getInstance(?string $adapter=null)
static _sqrt($operand, int $scale=50)
static _add($left_operand, $right_operand, int $scale=50)
static ilMathAdapter $default_adapter
static getDefaultAdapter()
static getFirstValidAdapter()
static __callStatic(string $method, $args)
Backward compatibility: Map all static calls to an equivalent instance method of the adapter.
static _div($left_operand, $right_operand, int $scale=50)
static setDefaultAdapter(ilMathAdapter $adapter)
static _mul($left_operand, $right_operand, int $scale=50)
static getGreatestCommonDivisor($a, $b)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$a
thx to https://mlocati.github.io/php-cs-fixer-configurator for the examples