Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions php_decimal.c
Original file line number Diff line number Diff line change
Expand Up @@ -2373,7 +2373,10 @@ PHP_DECIMAL_ARGINFO_END()
PHP_DECIMAL_METHOD(isPositive)
{
PHP_DECIMAL_PARAMS_PARSE_NONE();
RETURN_BOOL(mpd_ispositive(THIS_MPD()));

mpd_t *mpd = THIS_MPD();

RETURN_BOOL(!mpd_isnan(mpd) && mpd_ispositive(mpd));
}

/**
Expand All @@ -2384,7 +2387,10 @@ PHP_DECIMAL_ARGINFO_END()
PHP_DECIMAL_METHOD(isNegative)
{
PHP_DECIMAL_PARAMS_PARSE_NONE();
RETURN_BOOL(mpd_isnegative(THIS_MPD()));

mpd_t *mpd = THIS_MPD();

RETURN_BOOL(!mpd_isnan(mpd) && mpd_isnegative(mpd));
}

/**
Expand Down
55 changes: 55 additions & 0 deletions tests/php7/methods/isNegative.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
--TEST--
Decimal::isNegative
--SKIPIF--
<?php
if (!extension_loaded("decimal")) echo "skip";
?>
--FILE--
<?php
use Decimal\Decimal;

/**
* Shortcut to construct a new decimal.
*/
function decimal(...$args) { return new Decimal(...$args); }

$tests = [
/* Number, expected */

[ "1E-50", false],
["-1E-50", true],

["0", false],
["-0", true],
[1, false],
[2, false],
[3, false],

[-1, true],
[-2, true],
[-3, true],

["1.5", false],
["2.5", false],
["3.5", false],

["-1.5", true],
["-2.5", true],
["-3.5", true],

[ "NAN", false],
[ "INF", false],
["-INF", true],
];

foreach ($tests as $pair) {
$number = $pair[0];
$expect = $pair[1];
$result = decimal($number)->isNegative();

if ((string) $result !== (string) $expect) {
print_r(compact("number", "result", "expect"));
}
}
?>
--EXPECT--
55 changes: 55 additions & 0 deletions tests/php7/methods/isPositive.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
--TEST--
Decimal::isPositive
--SKIPIF--
<?php
if (!extension_loaded("decimal")) echo "skip";
?>
--FILE--
<?php
use Decimal\Decimal;

/**
* Shortcut to construct a new decimal.
*/
function decimal(...$args) { return new Decimal(...$args); }

$tests = [
/* Number, expected */

[ "1E-50", true],
["-1E-50", false],

["0", true],
["-0", false],
[1, true],
[2, true],
[3, true],

[-1, false],
[-2, false],
[-3, false],

["1.5", true],
["2.5", true],
["3.5", true],

["-1.5", false],
["-2.5", false],
["-3.5", false],

[ "NAN", false],
[ "INF", true],
["-INF", false],
];

foreach ($tests as $pair) {
$number = $pair[0];
$expect = $pair[1];
$result = decimal($number)->isPositive();

if ((string) $result !== (string) $expect) {
print_r(compact("number", "result", "expect"));
}
}
?>
--EXPECT--