This repository was archived by the owner on Dec 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathDecimalConstants.php
More file actions
148 lines (130 loc) · 3.37 KB
/
DecimalConstants.php
File metadata and controls
148 lines (130 loc) · 3.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<?php
declare(strict_types=1);
namespace Litipk\BigNumbers;
use Litipk\BigNumbers\Decimal as Decimal;
/**
* Class that holds many important numeric constants
*
* @author Andreu Correa Casablanca <castarco@litipk.com>
*/
class DecimalConstants
{
/** @var Decimal */
private static $ZERO = null;
/** @var Decimal */
private static $ONE = null;
/** @var Decimal */
private static $NEGATIVE_ONE = null;
/** @var Decimal */
private static $PI = null;
/** @var Decimal */
private static $EulerMascheroni = null;
/** @var Decimal */
private static $GoldenRatio = null;
/** @var Decimal */
private static $SilverRatio = null;
/** @var Decimal */
private static $LightSpeed = null;
private function __construct()
{
}
private function __clone()
{
}
public static function zero(): Decimal
{
if (null === self::$ZERO) {
self::$ZERO = Decimal::fromInteger(0);
}
return self::$ZERO;
}
public static function one(): Decimal
{
if (null === self::$ONE) {
self::$ONE = Decimal::fromInteger(1);
}
return self::$ONE;
}
public static function negativeOne(): Decimal
{
if (null === self::$NEGATIVE_ONE) {
self::$NEGATIVE_ONE = Decimal::fromInteger(-1);
}
return self::$NEGATIVE_ONE;
}
/**
* Returns the Pi number.
* @return Decimal
*/
public static function pi(): Decimal
{
if (null === self::$PI) {
self::$PI = Decimal::fromString(
"3.14159265358979323846264338327950"
);
}
return self::$PI;
}
/**
* Returns the Euler's E number.
* @param integer $scale
* @return Decimal
*/
public static function e(int $scale = 32): Decimal
{
if ($scale < 0) {
throw new \InvalidArgumentException("\$scale must be positive.");
}
return static::one()->exp($scale);
}
/**
* Returns the Euler-Mascheroni constant.
* @return Decimal
*/
public static function eulerMascheroni(): Decimal
{
if (null === self::$EulerMascheroni) {
self::$EulerMascheroni = Decimal::fromString(
"0.57721566490153286060651209008240"
);
}
return self::$EulerMascheroni;
}
/**
* Returns the Golden Ration, also named Phi.
* @return Decimal
*/
public static function goldenRatio(): Decimal
{
if (null === self::$GoldenRatio) {
self::$GoldenRatio = Decimal::fromString(
"1.61803398874989484820458683436564"
);
}
return self::$GoldenRatio;
}
/**
* Returns the Silver Ratio.
* @return Decimal
*/
public static function silverRatio(): Decimal
{
if (null === self::$SilverRatio) {
self::$SilverRatio = Decimal::fromString(
"2.41421356237309504880168872420970"
);
}
return self::$SilverRatio;
}
/**
* Returns the Light of Speed measured in meters / second.
* @return Decimal
*/
public static function lightSpeed(): Decimal
{
if (null === self::$LightSpeed) {
self::$LightSpeed = Decimal::fromInteger(299792458);
}
return self::$LightSpeed;
}
}