Completed
Push — master ( b17898...75b5ce )
by Patrick
01:57
created

Math::isZero()   A

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 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ExtendedStrings\Strings;
6
7
/**
8
 * A class containing functions useful for floating-point calculations.
9
 */
10
class Math
11
{
12
    const PRECISION = 10;
13
14
    /**
15
     * Returns the greatest common divisor of two floats.
16
     *
17
     * @param float $a
18
     * @param float $b
19
     *
20
     * @return float
21
     */
22
    public static function gcd(float $a, float $b): float
23
    {
24
        return self::isZero($b) ? $a : self::gcd($b, fmod($a, $b));
25
    }
26
27
    /**
28
     * Tests whether a float is zero.
29
     *
30
     * @param float $x
31
     *
32
     * @return bool
33
     */
34
    public static function isZero(float $x): bool
35
    {
36
        return $x === 0 || abs($x) < pow(10, - self::PRECISION);
37
    }
38
}
39