Skip to content

Commit 611eedc

Browse files
committed
Detect num. decimals, math constants, nan/inf for floats
1 parent 3cdbebc commit 611eedc

File tree

3 files changed

+112
-1
lines changed

3 files changed

+112
-1
lines changed

examples/simple_variables.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
$var3 = 15.5;
88
$var4 = 'hello world!';
99
$var5 = false;
10+
$var6 = pi();
1011

1112
$ladybug = new \Ladybug\Dumper();
12-
echo $ladybug->dump($var1, $var2, $var3, $var4, $var5);
13+
echo $ladybug->dump($var1, $var2, $var3, $var4, $var5, $var6);

src/Ladybug/Type/FloatType.php

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,18 @@ class FloatType extends AbstractType
2121

2222
const TYPE_ID = 'float';
2323

24+
protected $decimals = 0;
25+
26+
/** @var boolean $nan Whether the variable is NaN or not */
27+
protected $nan = false;
28+
29+
/** @var boolean $infinite Whether the variable is infinite or not */
30+
protected $infinite = false;
31+
32+
/** @var string $mathConstant Mathematical constant name */
33+
protected $mathConstant = null;
34+
35+
2436
/**
2537
* Constructor.
2638
*/
@@ -41,6 +53,77 @@ public function load($var, $level = 1)
4153
}
4254

4355
parent::load($var, $level);
56+
57+
$this->decimals = strlen(substr(strrchr($var, "."), 1));
58+
$this->infinite = is_infinite($var);
59+
$this->nan = is_nan($var);
60+
$this->mathConstant = $this->detectMathConstant($var, $this->decimals);
61+
}
62+
63+
/**
64+
* Checks whether the variable is NaN.
65+
*
66+
* @return bool
67+
*/
68+
public function isNan()
69+
{
70+
return $this->nan;
71+
}
72+
73+
/**
74+
* Checks whether the variable is infinite.
75+
*
76+
* @return bool
77+
*/
78+
public function isInfinite()
79+
{
80+
return $this->infinite;
4481
}
4582

83+
/**
84+
* Gets the mathematical constant name, if any.
85+
*
86+
* @return string
87+
*/
88+
public function getMathConstant()
89+
{
90+
return $this->mathConstant;
91+
}
92+
93+
public function detectMathConstant($var, $precision)
94+
{
95+
$constants = array(
96+
'pi' => array(
97+
'value' => M_PI,
98+
'min_precision' => 4
99+
),
100+
'e' => array(
101+
'value' => M_E,
102+
'min_precision' => 4
103+
),
104+
'euler_mascheroni' => array(
105+
'value' => M_EULER,
106+
'min_precision' => 4
107+
),
108+
'conway' => array(
109+
'value' => 1.303577269,
110+
'min_precision' => 4
111+
)
112+
);
113+
114+
foreach ($constants as $name => $item) {
115+
if (0 === bccomp($var, $item['value'], max($precision - 1, $item['min_precision']))) {
116+
return $name;
117+
}
118+
}
119+
120+
return null;
121+
}
122+
123+
public function getDecimals()
124+
{
125+
return $this->decimals;
126+
}
127+
128+
46129
}

tests/Ladybug/Tests/Type/FloatTypeTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ public function testLoaderForValidValues()
2222
$this->type->load($var);
2323
$this->assertEquals($var, $this->type->getValue());
2424
$this->assertEquals(1, $this->type->getLevel());
25+
$this->assertEquals(3, $this->type->getDecimals());
26+
$this->assertFalse($this->type->isNan());
27+
$this->assertFalse($this->type->isInfinite());
2528
}
2629

2730
public function testLoaderForOtherType()
@@ -33,4 +36,28 @@ public function testLoaderForOtherType()
3336
$this->type->load($var);
3437
}
3538

39+
public function testNanValue()
40+
{
41+
$var = acos(1.01);
42+
43+
$this->type->load($var);
44+
$this->assertTrue($this->type->isNan());
45+
}
46+
47+
public function testInfiniteValue()
48+
{
49+
$var = log(0);
50+
51+
$this->type->load($var);
52+
$this->assertTrue($this->type->isInfinite());
53+
}
54+
55+
public function testPiMathConstantDetection()
56+
{
57+
$var = pi();
58+
59+
$this->type->load($var);
60+
$this->assertEquals('pi', $this->type->getMathConstant());
61+
}
62+
3663
}

0 commit comments

Comments
 (0)