File: bcround_early_return.phpt

package info (click to toggle)
php8.4 8.4.11-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 208,108 kB
  • sloc: ansic: 1,060,628; php: 35,345; sh: 11,866; cpp: 7,201; pascal: 4,913; javascript: 3,091; asm: 2,810; yacc: 2,411; makefile: 689; xml: 446; python: 301; awk: 148
file content (77 lines) | stat: -rw-r--r-- 2,734 bytes parent folder | download
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
--TEST--
bcround() function with early return
--EXTENSIONS--
bcmath
--FILE--
<?php

$early_return_cases = [
    ['123', 1],
    ['123.5', 1],
    ['123.5', 2],
    ['123.0000000000000000000001', 22],
    ['123.0000000000000000000001', 23],
    ['-123', 1],
    ['-123.5', 1],
    ['-123.5', 2],
    ['-123.0000000000000000000001', 22],
    ['-123.0000000000000000000001', 23],
    ['0', 0],
    ['0.0', 0],
    ['0.0000', 0],
    ['-0', 0],
    ['-0.0', 0],
    ['-0.0000', 0],
];

$results = [
    RoundingMode::HalfAwayFromZero->name => [],
];
foreach ($early_return_cases as [$num, $precision]) {
    $result = str_pad("[{$num}, {$precision}]", 33, ' ', STR_PAD_LEFT) . ' => ' . bcround($num, $precision, RoundingMode::HalfAwayFromZero) . "\n";
    echo $result;
    $results[RoundingMode::HalfAwayFromZero->name][] = $result;
}

echo "\n";

foreach (RoundingMode::cases() as $mode) {
    $results[$mode->name] = [];
    foreach ($early_return_cases as [$num, $precision]) {
        $result = str_pad("[{$num}, {$precision}]", 33, ' ', STR_PAD_LEFT) . ' => ' . bcround($num, $precision, $mode) . "\n";
        $results[$mode->name][] = $result;
    }

    if ($results[RoundingMode::HalfAwayFromZero->name] === $results[$mode->name]) {
        echo str_pad($mode->name, 24, ' ', STR_PAD_LEFT) . ": result is same to HalfAwayFromZero\n";
    } else {
        echo str_pad($mode->name, 24, ' ', STR_PAD_LEFT) . ": result is not same to HalfAwayFromZero, failed\n";
    }
}
?>
--EXPECT--
                         [123, 1] => 123.0
                       [123.5, 1] => 123.5
                       [123.5, 2] => 123.50
 [123.0000000000000000000001, 22] => 123.0000000000000000000001
 [123.0000000000000000000001, 23] => 123.00000000000000000000010
                        [-123, 1] => -123.0
                      [-123.5, 1] => -123.5
                      [-123.5, 2] => -123.50
[-123.0000000000000000000001, 22] => -123.0000000000000000000001
[-123.0000000000000000000001, 23] => -123.00000000000000000000010
                           [0, 0] => 0
                         [0.0, 0] => 0
                      [0.0000, 0] => 0
                          [-0, 0] => 0
                        [-0.0, 0] => 0
                     [-0.0000, 0] => 0

        HalfAwayFromZero: result is same to HalfAwayFromZero
         HalfTowardsZero: result is same to HalfAwayFromZero
                HalfEven: result is same to HalfAwayFromZero
                 HalfOdd: result is same to HalfAwayFromZero
             TowardsZero: result is same to HalfAwayFromZero
            AwayFromZero: result is same to HalfAwayFromZero
        NegativeInfinity: result is same to HalfAwayFromZero
        PositiveInfinity: result is same to HalfAwayFromZero