-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMath.php
93 lines (76 loc) · 1.77 KB
/
Math.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<?php declare(strict_types=1);
namespace h4kuna\DataType\Number;
use DateTimeInterface;
use h4kuna\DataType;
use h4kuna\DataType\Exceptions\InvalidArgumentsException;
use Nette\StaticClass;
final class Math
{
use StaticClass;
/**
* Allow number in interval and correct it.
* @template T of float|int|DateTimeInterface
* @param T $number
* @param T|null $max
* @param T|null $min
*
* @return T
*/
public static function interval(
float|int|DateTimeInterface $number,
float|int|DateTimeInterface|null $max,
float|int|DateTimeInterface|null $min = 0
): float|int|DateTimeInterface
{
if ($max !== null && $min !== null && $max < $min) {
throw new InvalidArgumentsException('Maximum is less than minimum.');
}
if ($min === null && $max === null) {
return $number;
} elseif ($min === null) {
return min($max, $number);
} elseif ($max === null) {
return max($min, $number);
}
return max($min, min($max, $number));
}
/**
* Round method to zero point five.
* @example 1.24 -> 1.0, 1.25 -> 1.5, 1.74 -> 1.5, 1.75 -> 2.0
*/
public static function round5(float|int $num): float|int
{
if ($num < 0) {
$floor = ceil($num);
$i = -1;
} else {
$floor = floor($num);
$i = 1;
}
$decimal = abs($num - $floor);
if ($decimal < 0.25) {
return $floor;
}
if (0.25 <= $decimal && $decimal < 0.75) {
return $floor + (0.5 * $i);
}
return $floor + $i;
}
public static function safeDivision(float|int $up, float|int $down): ?float
{
if ($down == 0) {
return null;
}
return $up / $down;
}
public static function factorial(int $n): int
{
if ($n == 0) {
return 1;
}
if ($n < 0) {
throw new InvalidArgumentsException('The number can\'t negative number.');
}
return $n * self::factorial($n - 1);
}
}