Passed
Pull Request — dev (#56)
by Jordan
08:42
created

ConstantProvider::makePi()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 0
c 1
b 0
f 1
nc 1
nop 1
dl 0
loc 2
ccs 0
cts 2
cp 0
crap 2
rs 10
1
<?php
2
3
4
namespace Samsara\Fermat\Provider;
5
6
7
use Samsara\Exceptions\UsageError\IntegrityConstraint;
8
use Samsara\Fermat\Numbers;
9
use Samsara\Fermat\Types\Base\Selectable;
10
use Samsara\Fermat\Values\ImmutableDecimal;
11
12
class ConstantProvider
13
{
14
15
    public static function makePi(int $digits): string
0 ignored issues
show
Unused Code introduced by
The parameter $digits is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

15
    public static function makePi(/** @scrutinizer ignore-unused */ int $digits): string

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
16
    {
17
18
        // TODO: Implement https://en.wikipedia.org/wiki/Chudnovsky_algorithm
19
20
    }
0 ignored issues
show
Bug Best Practice introduced by
In this branch, the function will implicitly return null which is incompatible with the type-hinted return string. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
21
22
    /**
23
     * @param int $digits
24
     * @return string
25
     * @throws IntegrityConstraint
26
     */
27
    public static function makeE(int $digits): string
28
    {
29
30
        $internalPrecision = $digits + 5;
31
32
        $one = Numbers::makeOne($internalPrecision)->setMode(Selectable::CALC_MODE_PRECISION);
33
        $denominator = Numbers::make(Numbers::MUTABLE, '1', $internalPrecision)->setMode(Selectable::CALC_MODE_PRECISION);
34
        $e = Numbers::make(NUmbers::MUTABLE, '2', $internalPrecision)->setMode(Selectable::CALC_MODE_PRECISION);
35
        $n = Numbers::make(Numbers::MUTABLE, '2', $internalPrecision)->setMode(Selectable::CALC_MODE_PRECISION);
36
37
        $continue = true;
38
39
        while ($continue) {
40
            $denominator->multiply($n);
41
            $n->add($one);
42
            $term = $one->divide($denominator);
43
44
            if ($term->numberOfLeadingZeros() > $internalPrecision) {
45
                $continue = false;
46
            }
47
48
            $e->add($term);
49
        }
50
51
        return $e->truncateToPrecision($digits)->getValue();
52
53
    }
54
55
}