Skip to content

Commit 6106253

Browse files
authored
Merge pull request #68 from julienfalque/nan
Fix `isPositive()` with `NaN`
2 parents 49cd96e + 5313518 commit 6106253

File tree

3 files changed

+118
-2
lines changed

3 files changed

+118
-2
lines changed

php_decimal.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2373,7 +2373,10 @@ PHP_DECIMAL_ARGINFO_END()
23732373
PHP_DECIMAL_METHOD(isPositive)
23742374
{
23752375
PHP_DECIMAL_PARAMS_PARSE_NONE();
2376-
RETURN_BOOL(mpd_ispositive(THIS_MPD()));
2376+
2377+
mpd_t *mpd = THIS_MPD();
2378+
2379+
RETURN_BOOL(!mpd_isnan(mpd) && mpd_ispositive(mpd));
23772380
}
23782381

23792382
/**
@@ -2384,7 +2387,10 @@ PHP_DECIMAL_ARGINFO_END()
23842387
PHP_DECIMAL_METHOD(isNegative)
23852388
{
23862389
PHP_DECIMAL_PARAMS_PARSE_NONE();
2387-
RETURN_BOOL(mpd_isnegative(THIS_MPD()));
2390+
2391+
mpd_t *mpd = THIS_MPD();
2392+
2393+
RETURN_BOOL(!mpd_isnan(mpd) && mpd_isnegative(mpd));
23882394
}
23892395

23902396
/**

tests/php7/methods/isNegative.phpt

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
--TEST--
2+
Decimal::isNegative
3+
--SKIPIF--
4+
<?php
5+
if (!extension_loaded("decimal")) echo "skip";
6+
?>
7+
--FILE--
8+
<?php
9+
use Decimal\Decimal;
10+
11+
/**
12+
* Shortcut to construct a new decimal.
13+
*/
14+
function decimal(...$args) { return new Decimal(...$args); }
15+
16+
$tests = [
17+
/* Number, expected */
18+
19+
[ "1E-50", false],
20+
["-1E-50", true],
21+
22+
["0", false],
23+
["-0", true],
24+
[1, false],
25+
[2, false],
26+
[3, false],
27+
28+
[-1, true],
29+
[-2, true],
30+
[-3, true],
31+
32+
["1.5", false],
33+
["2.5", false],
34+
["3.5", false],
35+
36+
["-1.5", true],
37+
["-2.5", true],
38+
["-3.5", true],
39+
40+
[ "NAN", false],
41+
[ "INF", false],
42+
["-INF", true],
43+
];
44+
45+
foreach ($tests as $pair) {
46+
$number = $pair[0];
47+
$expect = $pair[1];
48+
$result = decimal($number)->isNegative();
49+
50+
if ((string) $result !== (string) $expect) {
51+
print_r(compact("number", "result", "expect"));
52+
}
53+
}
54+
?>
55+
--EXPECT--

tests/php7/methods/isPositive.phpt

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
--TEST--
2+
Decimal::isPositive
3+
--SKIPIF--
4+
<?php
5+
if (!extension_loaded("decimal")) echo "skip";
6+
?>
7+
--FILE--
8+
<?php
9+
use Decimal\Decimal;
10+
11+
/**
12+
* Shortcut to construct a new decimal.
13+
*/
14+
function decimal(...$args) { return new Decimal(...$args); }
15+
16+
$tests = [
17+
/* Number, expected */
18+
19+
[ "1E-50", true],
20+
["-1E-50", false],
21+
22+
["0", true],
23+
["-0", false],
24+
[1, true],
25+
[2, true],
26+
[3, true],
27+
28+
[-1, false],
29+
[-2, false],
30+
[-3, false],
31+
32+
["1.5", true],
33+
["2.5", true],
34+
["3.5", true],
35+
36+
["-1.5", false],
37+
["-2.5", false],
38+
["-3.5", false],
39+
40+
[ "NAN", false],
41+
[ "INF", true],
42+
["-INF", false],
43+
];
44+
45+
foreach ($tests as $pair) {
46+
$number = $pair[0];
47+
$expect = $pair[1];
48+
$result = decimal($number)->isPositive();
49+
50+
if ((string) $result !== (string) $expect) {
51+
print_r(compact("number", "result", "expect"));
52+
}
53+
}
54+
?>
55+
--EXPECT--

0 commit comments

Comments
 (0)