Skip to content
This repository was archived by the owner on Dec 27, 2023. It is now read-only.

Commit 74826de

Browse files
committed
Added some numerical constants
1 parent 2f1d13e commit 74826de

File tree

4 files changed

+229
-0
lines changed

4 files changed

+229
-0
lines changed

src/NumConstants.php

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
<?php
2+
3+
namespace Litipk\BigNumbers;
4+
5+
use Litipk\BigNumbers\Decimal as Decimal;
6+
7+
8+
/**
9+
* Static class that holds many important numeric constants
10+
*
11+
* @author Andreu Correa Casablanca <castarco@litipk.com>
12+
*/
13+
class NumConstants
14+
{
15+
private static $PI = null;
16+
private static $E = null;
17+
private static $EulerMascheroni = null;
18+
19+
private static $GoldenRatio = null;
20+
21+
private static $LightSpeed = null;
22+
23+
/**
24+
* Private constructor
25+
*/
26+
private function __construct()
27+
{
28+
29+
}
30+
31+
/**
32+
* Private clone method
33+
*/
34+
private function __clone()
35+
{
36+
37+
}
38+
39+
/**
40+
* Returns the Pi number.
41+
*/
42+
public static function PI()
43+
{
44+
if (static::$PI === null) {
45+
static::$PI = Decimal::fromString(
46+
"3.14159265358979323846264338327950"
47+
);
48+
}
49+
return static::$PI;
50+
}
51+
52+
/**
53+
* Returns the Euler's E number.
54+
*/
55+
public static function E()
56+
{
57+
if (static::$E === null) {
58+
static::$E = Decimal::fromString(
59+
"2.71828182845904523536028747135266"
60+
);
61+
}
62+
return static::$E;
63+
}
64+
65+
/**
66+
* Returns the Euler-Mascheroni constant.
67+
*/
68+
public static function EulerMascheroni()
69+
{
70+
if (static::$EulerMascheroni === null) {
71+
static::$EulerMascheroni = Decimal::fromString(
72+
"0.57721566490153286060651209008240"
73+
);
74+
}
75+
return static::$EulerMascheroni;
76+
}
77+
78+
/**
79+
* Returns the Golden Ration, also named Phi.
80+
*/
81+
public static function GoldenRatio()
82+
{
83+
if (static::$GoldenRatio === null) {
84+
static::$GoldenRatio = Decimal::fromString(
85+
"1.61803398874989484820458683436564"
86+
);
87+
}
88+
return static::$GoldenRatio;
89+
}
90+
91+
/**
92+
* Returns the Light of Speed measured in meters / second.
93+
*/
94+
public static function LightSpeed()
95+
{
96+
if (static::$LightSpeed === null) {
97+
static::$LightSpeed = Decimal::fromInteger(299792458);
98+
}
99+
return static::$LightSpeed;
100+
}
101+
}

tests/Decimal/DecimalCeilTest.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
use Litipk\BigNumbers\Decimal as Decimal;
4+
5+
6+
date_default_timezone_set('UTC');
7+
8+
9+
class DecimalCeilTest extends PHPUnit_Framework_TestCase
10+
{
11+
public function testIntegerCeil()
12+
{
13+
$this->assertTrue(Decimal::fromFloat(0.00)->ceil()->isZero());
14+
$this->assertTrue(Decimal::fromFloat(0.00)->ceil()->equals(Decimal::fromInteger(0)));
15+
16+
$this->assertFalse(Decimal::fromFloat(0.01)->ceil()->isZero());
17+
$this->assertFalse(Decimal::fromFloat(0.40)->ceil()->isZero());
18+
$this->assertFalse(Decimal::fromFloat(0.50)->ceil()->isZero());
19+
20+
$this->assertTrue(Decimal::fromFloat(0.01)->ceil()->equals(Decimal::fromInteger(1)));
21+
$this->assertTrue(Decimal::fromFloat(0.40)->ceil()->equals(Decimal::fromInteger(1)));
22+
$this->assertTrue(Decimal::fromFloat(0.50)->ceil()->equals(Decimal::fromInteger(1)));
23+
}
24+
25+
public function testCeilWithDecimals()
26+
{
27+
$this->assertTrue(Decimal::fromString('3.45')->ceil(1)->equals(Decimal::fromString('3.5')));
28+
$this->assertTrue(Decimal::fromString('3.44')->ceil(1)->equals(Decimal::fromString('3.5')));
29+
}
30+
31+
public function testNoUsefulCeil()
32+
{
33+
$this->assertTrue(Decimal::fromString('3.45')->ceil(2)->equals(Decimal::fromString('3.45')));
34+
$this->assertTrue(Decimal::fromString('3.45')->ceil(3)->equals(Decimal::fromString('3.45')));
35+
}
36+
37+
public function testInfiniteRound()
38+
{
39+
$pInf = Decimal::getPositiveInfinite();
40+
$nInf = Decimal::getNegativeInfinite();
41+
42+
$this->assertTrue($pInf->ceil()->equals($pInf));
43+
$this->assertTrue($nInf->ceil()->equals($nInf));
44+
}
45+
}

tests/Decimal/DecimalFloorTest.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
use Litipk\BigNumbers\Decimal as Decimal;
4+
5+
6+
date_default_timezone_set('UTC');
7+
8+
9+
class DecimalFloorTest extends PHPUnit_Framework_TestCase
10+
{
11+
public function testIntegerFloor()
12+
{
13+
$this->assertTrue(Decimal::fromFloat(0.00)->floor()->isZero());
14+
$this->assertTrue(Decimal::fromFloat(0.00)->floor()->equals(Decimal::fromInteger(0)));
15+
16+
$this->assertTrue(Decimal::fromFloat(0.01)->floor()->isZero());
17+
$this->assertTrue(Decimal::fromFloat(0.40)->floor()->isZero());
18+
$this->assertTrue(Decimal::fromFloat(0.50)->floor()->isZero());
19+
20+
$this->assertTrue(Decimal::fromFloat(0.01)->floor()->equals(Decimal::fromInteger(0)));
21+
$this->assertTrue(Decimal::fromFloat(0.40)->floor()->equals(Decimal::fromInteger(0)));
22+
$this->assertTrue(Decimal::fromFloat(0.50)->floor()->equals(Decimal::fromInteger(0)));
23+
24+
$this->assertTrue(Decimal::fromFloat(1.01)->floor()->equals(Decimal::fromInteger(1)));
25+
$this->assertTrue(Decimal::fromFloat(1.40)->floor()->equals(Decimal::fromInteger(1)));
26+
$this->assertTrue(Decimal::fromFloat(1.50)->floor()->equals(Decimal::fromInteger(1)));
27+
}
28+
29+
public function testFloorWithDecimals()
30+
{
31+
$this->assertTrue(Decimal::fromString('3.45')->floor(1)->equals(Decimal::fromString('3.4')));
32+
$this->assertTrue(Decimal::fromString('3.44')->floor(1)->equals(Decimal::fromString('3.4')));
33+
}
34+
35+
public function testNoUsefulFloor()
36+
{
37+
$this->assertTrue(Decimal::fromString('3.45')->floor(2)->equals(Decimal::fromString('3.45')));
38+
$this->assertTrue(Decimal::fromString('3.45')->floor(3)->equals(Decimal::fromString('3.45')));
39+
}
40+
41+
public function testInfiniteRound()
42+
{
43+
$pInf = Decimal::getPositiveInfinite();
44+
$nInf = Decimal::getNegativeInfinite();
45+
46+
$this->assertTrue($pInf->floor()->equals($pInf));
47+
$this->assertTrue($nInf->floor()->equals($nInf));
48+
}
49+
}

tests/NumConstantsTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
use Litipk\BigNumbers\NumConstants as NumConstants;
4+
use Litipk\BigNumbers\Decimal as Decimal;
5+
6+
7+
date_default_timezone_set('UTC');
8+
9+
10+
class NumConstantsTest extends PHPUnit_Framework_TestCase
11+
{
12+
public function testFiniteAbs()
13+
{
14+
$this->assertTrue(NumConstants::PI()->equals(
15+
Decimal::fromString("3.14159265358979323846264338327950")
16+
));
17+
18+
$this->assertTrue(NumConstants::E()->equals(
19+
Decimal::fromString("2.71828182845904523536028747135266")
20+
));
21+
22+
$this->assertTrue(NumConstants::EulerMascheroni()->equals(
23+
Decimal::fromString("0.57721566490153286060651209008240")
24+
));
25+
26+
$this->assertTrue(NumConstants::GoldenRatio()->equals(
27+
Decimal::fromString("1.61803398874989484820458683436564")
28+
));
29+
30+
$this->assertTrue(NumConstants::LightSpeed()->equals(
31+
Decimal::fromInteger(299792458)
32+
));
33+
}
34+
}

0 commit comments

Comments
 (0)