Math::gcd()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 2
eloc 2
nc 2
nop 2
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace ExtendedStrings\Strings;
6
7
class Math
8
{
9
    const EPSILON = 0.0000000001;
10
11
    /**
12
     * Returns the greatest common divisor of two floats.
13
     *
14
     * @param float $a
15
     * @param float $b
16
     *
17
     * @return float
18
     */
19
    public static function gcd(float $a, float $b): float
20
    {
21
        return self::isZero($b) ? $a : self::gcd($b, fmod($a, $b));
22
    }
23
24
    /**
25
     * Tests whether a float is zero.
26
     *
27
     * @param float $x
28
     *
29
     * @return bool
30
     */
31
    public static function isZero(float $x): bool
32
    {
33
        return $x === 0 || abs($x) < self::EPSILON;
34
    }
35
36
    /**
37
     * Tests whether a number is greater than another.
38
     *
39
     * @param float $a
40
     * @param float $b
41
     *
42
     * @return bool
43
     */
44
    public static function isGreaterThan(float $a, float $b): bool
45
    {
46
        return $a > $b && $a - $b > self::EPSILON;
47
    }
48
}
49