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

Math   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 0
dl 0
loc 29
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A gcd() 0 4 2
A isZero() 0 4 2
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