Completed
Push — master ( ef221c...1a4395 )
by Andreu
07:51 queued 05:04
created

DecimalConstants::silverRatio()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 9.6666
cc 2
eloc 5
nc 2
nop 0
crap 2
1
<?php
2
3
namespace Litipk\BigNumbers;
4
5
use Litipk\BigNumbers\Decimal as Decimal;
6
use Litipk\Exceptions\InvalidArgumentTypeException;
7
8
9
/**
10
 * git statu class that holds many important numeric constants
11
 *
12
 * @author Andreu Correa Casablanca <[email protected]>
13
 */
14
final class DecimalConstants
15
{
16
    private static $ZERO = null;
17
    private static $ONE = null;
18
    private static $NEGATIVE_ONE = null;
19
20
    private static $PI = null;
21
    private static $EulerMascheroni = null;
22
23
    private static $GoldenRatio = null;
24
25
    private static $SilverRatio = null;
26
27
    private static $LightSpeed = null;
28
29
    /**
30
     * Private constructor
31
     */
32
    private function __construct()
33
    {
34
35
    }
36
37
    /**
38
     * Private clone method
39
     */
40
    private function __clone()
41
    {
42
43
    }
44
45 42
    public static function zero()
46
    {
47 42
        if (self::$ZERO === null) {
48 1
            self::$ZERO = Decimal::fromInteger(0);
49
        }
50 42
        return self::$ZERO;
51
    }
52
53 61
    public static function one()
54
    {
55 61
        if (self::$ONE === null) {
56 1
            self::$ONE = Decimal::fromInteger(1);
57
        }
58 61
        return self::$ONE;
59
    }
60
61 40
    public static function negativeOne()
62
    {
63 40
        if (self::$NEGATIVE_ONE === null) {
64 1
            self::$NEGATIVE_ONE = Decimal::fromInteger(-1);
65
        }
66 40
        return self::$NEGATIVE_ONE;
67
    }
68
69
    /**
70
     * Returns the Pi number.
71
     * @return Decimal
72
     */
73 38
    public static function pi()
74
    {
75 38
        if (self::$PI === null) {
76 1
            self::$PI = Decimal::fromString(
77 1
                "3.14159265358979323846264338327950"
78
            );
79
        }
80 38
        return self::$PI;
81
    }
82
83
    /**
84
     * Returns the Euler's E number.
85
     * @param  integer $scale
86
     * @return Decimal
87
     */
88 3
    public static function e($scale = 32)
89
    {
90 3
        if (!is_int($scale)) {
91 1
            throw new InvalidArgumentTypeException(['integer'], gettype($scale));
92
        }
93 2
        if ($scale < 0) {
94 1
            throw new \InvalidArgumentException("\$scale must be positive.");
95
        }
96
97 1
        return self::$ONE->exp($scale);
98
    }
99
100
    /**
101
     * Returns the Euler-Mascheroni constant.
102
     * @return Decimal
103
     */
104 1
    public static function eulerMascheroni()
105
    {
106 1
        if (self::$EulerMascheroni === null) {
107 1
            self::$EulerMascheroni = Decimal::fromString(
108 1
                "0.57721566490153286060651209008240"
109
            );
110
        }
111 1
        return self::$EulerMascheroni;
112
    }
113
114
    /**
115
     * Returns the Golden Ration, also named Phi.
116
     * @return Decimal
117
     */
118 1
    public static function goldenRatio()
119
    {
120 1
        if (self::$GoldenRatio === null) {
121 1
            self::$GoldenRatio = Decimal::fromString(
122 1
                "1.61803398874989484820458683436564"
123
            );
124
        }
125 1
        return self::$GoldenRatio;
126
    }
127
128
    /**
129
     * Returns the Silver Ratio.
130
     * @return Decimal
131
     */
132 1
    public static function silverRatio()
133
    {
134 1
        if (self::$SilverRatio === null) {
135 1
            self::$SilverRatio = Decimal::fromString(
136 1
                "2.41421356237309504880168872420970"
137
            );
138
        }
139 1
        return self::$SilverRatio;
140
    }
141
142
    /**
143
     * Returns the Light of Speed measured in meters / second.
144
     * @return Decimal
145
     */
146 1
    public static function lightSpeed()
147
    {
148 1
        if (self::$LightSpeed === null) {
149 1
            self::$LightSpeed = Decimal::fromInteger(299792458);
150
        }
151 1
        return self::$LightSpeed;
152
    }
153
}
154