ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilMathBCMathAdapter.php
Go to the documentation of this file.
1<?php
2
24{
29 public function __construct($scale = 0)
30 {
31 bcscale($scale);
32 }
33
37 public function add($left_operand, $right_operand, ?int $scale = null)
38 {
39 return bcadd($this->normalize($left_operand), $this->normalize($right_operand), $this->normalize($scale));
40 }
41
45 public function sub($left_operand, $right_operand, ?int $scale = null)
46 {
47 return bcsub($this->normalize($left_operand), $this->normalize($right_operand), $this->normalize($scale));
48 }
49
53 public function mul($left_operand, $right_operand, ?int $scale = null)
54 {
55 return bcmul($this->normalize($left_operand), $this->normalize($right_operand), $this->normalize($scale));
56 }
57
61 public function div($left_operand, $right_operand, ?int $scale = null)
62 {
63 if ($right_operand == 0) {
64 throw new ilMathDivisionByZeroException(sprintf("Division of %s by %s not possible!", $left_operand, $right_operand));
65 }
66
67 return bcdiv($this->normalize($left_operand), $this->normalize($right_operand), $this->normalize($scale));
68 }
69
73 public function mod($left_operand, $right_operand)
74 {
75 if ($right_operand == 0) {
76 throw new ilMathDivisionByZeroException(sprintf("Division of %s by %s not possible!", $left_operand, $right_operand));
77 }
78
79 return bcmod($this->normalize($left_operand), $this->normalize($right_operand));
80 }
81
85 public function pow($left_operand, $right_operand, ?int $scale = null)
86 {
87 $left_operand = $this->normalize($left_operand);
88 $right_operand = $this->normalize($right_operand);
89 $scale = $this->normalize($scale);
90
91 // bcpow() only supports exponents less than or equal to 2^31-1.
92 // Also, bcpow() does not support decimal numbers.
93 // If you have scale set to 0, then the exponent is converted to an integer; otherwise an error is generated.
94 $left_operand_dec = $this->exp2dec($left_operand);
95 $right_operand_dec = $this->exp2dec($right_operand);
96
97 // bcpow does NOT support decimal exponents
98 if (strpos($right_operand_dec, '.') === false) {
99 return bcpow($left_operand_dec, $right_operand_dec, $scale);
100 }
101
102 return $this->applyScale($left_operand ** $right_operand, $scale);
103 }
104
108 public function sqrt($operand, ?int $scale = null)
109 {
110 return bcsqrt($operand, $scale);
111 }
112
116 public function comp($left_operand, $right_operand, ?int $scale = null): int
117 {
118 return bccomp($left_operand, $right_operand, $scale);
119 }
120}
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
comp($left_operand, $right_operand, ?int $scale=null)
@inheritDoc
mod($left_operand, $right_operand)
@inheritDoc
sqrt($operand, ?int $scale=null)
@inheritDoc
__construct($scale=0)
ilMathBcMathAdapter constructor.
add($left_operand, $right_operand, ?int $scale=null)
@inheritDoc
pow($left_operand, $right_operand, ?int $scale=null)
@inheritDoc
div($left_operand, $right_operand, ?int $scale=null)
@inheritDoc
mul($left_operand, $right_operand, ?int $scale=null)
@inheritDoc
sub($left_operand, $right_operand, ?int $scale=null)
@inheritDoc
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
applyScale($left_operand, ?int $scale=null)
@inheritDoc
normalize($number)
This function fixes problems which occur when locale ist set to de_DE for example,...