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
|
--TEST--
Decimal::sub
--SKIPIF--
<?php
if (!extension_loaded("decimal")) echo "skip";
?>
--FILE--
<?php
use Decimal\Decimal;
/**
* OP1, OP2, Expected Result, Expected Precision
*/
$tests = [
[
new Decimal("8E+90000" /* 28 */),
new Decimal("5E+90000" /* 28 */),
"3E+90000", 28,
],
[
new Decimal("4.3333", 1), // 4
new Decimal("3.3333", 2), // 3.3
"0.7", 2
],
[
new Decimal("0.3" /* 28 */),
new Decimal("0.1" /* 28 */),
"0.2", 28
],
[
new Decimal("-0.3" /* 28 */),
new Decimal( "0.1" /* 28 */),
"-0.4", 28
],
[
new Decimal( "0.1" /* 28 */),
new Decimal("-0.1" /* 28 */),
"0.2", 28
],
[
new Decimal("-0.1" /* 28 */),
new Decimal("-0.1" /* 28 */),
"0.0", 28
],
[
new Decimal("0.33333333333333333333333333333", 20), // 0.33333333333333333333
new Decimal("0.22222222222222222222222222222", 12), // 0.222222222222
"0.11111111111133333333", 20,
],
[
new Decimal("0.33333333333333333333333333333", 2), // 0.33
new Decimal("0.22222222222222222222222222222", 16), // 0.2222222222222222
"0.1077777777777778", 16,
],
[
new Decimal("15"), 10, "5", 28
],
[
new Decimal("15", 2), 10, "5", 2
],
[
new Decimal("15", 50), 10, "5", 50
],
[
$a = new Decimal("7", 50), $a, "0", 50
],
/* Special numbers */
[new Decimal( "NAN"), "NAN", (string) (NAN - NAN), 28],
[new Decimal( "NAN"), "INF", (string) (NAN - INF), 28],
[new Decimal( "NAN"), "-INF", (string) (NAN - -INF), 28],
[new Decimal( "INF"), "NAN", (string) (INF - NAN), 28],
[new Decimal( "INF"), "INF", (string) (INF - INF), 28],
[new Decimal( "INF"), "-INF", (string) (INF - -INF), 28],
[new Decimal("-INF"), "NAN", (string) (-INF - NAN), 28],
[new Decimal("-INF"), "INF", (string) (-INF - INF), 28],
[new Decimal("-INF"), "-INF", (string) (-INF - -INF), 28],
];
foreach ($tests as $index => $test) {
list($op1, $op2, $expect, $precision) = $test;
$results = [
$op1->sub($op2),
$op1 - $op2,
];
foreach ($results as $result) {
if ([(string) $result, $result->precision()] !== [$expect, $precision]) {
print_r(compact("index", "op1", "op2", "result", "expect", "precision"));
break;
}
}
}
/* Test that op does not modify the original */
$number = new Decimal("2");
$number->sub(5);
$number - 5;
if ((string) $number !== "2") {
var_dump("Mutated!", compact("number"));
}
?>
--EXPECTF--
Warning: Loss of data on string conversion in %s on line 14
Warning: Loss of data on string conversion in %s on line 15
Warning: Loss of data on string conversion in %s on line 39
Warning: Loss of data on string conversion in %s on line 40
Warning: Loss of data on string conversion in %s on line 44
Warning: Loss of data on string conversion in %s on line 45
|