3v4l.org

run code in 300+ PHP versions simultaneously
<?php declare(strict_types=1); function myBcRound(string $numstr, int $precision = 2): string { if ($precision >= 0) { $addition = '0.' . str_repeat('0', $precision) . '5'; } else { throw new Exception('bcround with negative precision is not implemented.'); } return bcdiv(bcadd($numstr, $addition, $precision + 1), '1', $precision); } $testCases = [ ['source' => '123.0454000', 'scale' => 3, 'result' => '123.045'], ['source' => '123.0454499', 'scale' => 3, 'result' => '123.045'], ['source' => '123.0454500', 'scale' => 3, 'result' => '123.045'], ['source' => '123.0454900', 'scale' => 3, 'result' => '123.045'], ['source' => '123.0454999999999', 'scale' => 3, 'result' => '123.045'], ['source' => '123.045499999999999', 'scale' => 3, 'result' => '123.045'], ['source' => '123.045499999999999999999', 'scale' => 3, 'result' => '123.045'], ['source' => '123.0455000', 'scale' => 3, 'result' => '123.046'], ['source' => '123.0456000', 'scale' => 3, 'result' => '123.046'], ['source' => '123.0456789', 'scale' => 3, 'result' => '123.046'], [], ['source' => '123.454000', 'scale' => 2, 'result' => '123.45'], ['source' => '123.454499', 'scale' => 2, 'result' => '123.45'], ['source' => '123.454500', 'scale' => 2, 'result' => '123.45'], ['source' => '123.454900', 'scale' => 2, 'result' => '123.45'], ['source' => '123.454999999999', 'scale' => 2, 'result' => '123.45'],// round() ok ['source' => '123.45499999999999', 'scale' => 2, 'result' => '123.45'],// round() ok (in php84+, not ok < 84) ['source' => '123.45499999999999999999', 'scale' => 2, 'result' => '123.45'],// round() not ok ['source' => '123.455000', 'scale' => 2, 'result' => '123.46'], ['source' => '123.456000', 'scale' => 2, 'result' => '123.46'], ['source' => '123.456789', 'scale' => 2, 'result' => '123.46'], [], ['source' => '123.54000', 'scale' => 1, 'result' => '123.5'], ['source' => '123.54499', 'scale' => 1, 'result' => '123.5'], ['source' => '123.54500', 'scale' => 1, 'result' => '123.5'], ['source' => '123.54900', 'scale' => 1, 'result' => '123.5'], ['source' => '123.54999999999', 'scale' => 1, 'result' => '123.5'], ['source' => '123.5499999999999', 'scale' => 1, 'result' => '123.5'], ['source' => '123.5499999999999999999', 'scale' => 1, 'result' => '123.5'], ['source' => '123.55000', 'scale' => 1, 'result' => '123.6'], ['source' => '123.56000', 'scale' => 1, 'result' => '123.6'], ['source' => '123.56789', 'scale' => 1, 'result' => '123.6'], [], ['source' => '123.4000', 'scale' => 0, 'result' => '123'], ['source' => '123.4499', 'scale' => 0, 'result' => '123'], ['source' => '123.4500', 'scale' => 0, 'result' => '123'], ['source' => '123.4900', 'scale' => 0, 'result' => '123'], ['source' => '123.4999999999', 'scale' => 0, 'result' => '123'], ['source' => '123.499999999999', 'scale' => 0, 'result' => '123'], ['source' => '123.499999999999999999', 'scale' => 0, 'result' => '123'], ['source' => '123.5000', 'scale' => 0, 'result' => '124'], ['source' => '123.6000', 'scale' => 0, 'result' => '124'], ['source' => '123.6789', 'scale' => 0, 'result' => '124'], ]; foreach ($testCases as $testCase) { if (count($testCase) === 0) { echo "\n"; continue; } $source = $testCase['source']; $scale = $testCase['scale']; $result = $testCase['result']; echo str_pad($source, 25) . ' => '; $output = []; // new function myBcRound $roundedNumber1 = myBcRound($source, $scale); $output['myBcRound'] = $roundedNumber1 . ' ' . ($roundedNumber1 === $result ? ' OK' : 'FAIL'); // PHP8.4 bcround (only if you run on git.master branch on 3v4l.org or local PHP8.4 version) $roundedNumber2 = ''; if (version_compare(PHP_VERSION, '8.4', '>=')) { $roundedNumber2 = bcround($source, $scale); } $output['bcround'] = $roundedNumber2 . ' ' . ($roundedNumber2 === $result ? ' OK' : 'FAIL'); // round(float) $roundedNumber3 = (string) round((float) $source, $scale); $output['round'] = $roundedNumber3 . ' ' . ($roundedNumber3 === $result ? ' OK' : 'FAIL'); foreach ($output as $key => $value) { echo $key . ' = ' . str_pad($value, 7) . ' '; } echo "\n"; }
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 2, Position 2 = 87
Branch analysis from position: 2
2 jumps found. (Code = 78) Position 1 = 3, Position 2 = 87
Branch analysis from position: 3
2 jumps found. (Code = 43) Position 1 = 6, Position 2 = 8
Branch analysis from position: 6
1 jumps found. (Code = 42) Position 1 = 2
Branch analysis from position: 2
Branch analysis from position: 8
2 jumps found. (Code = 43) Position 1 = 29, Position 2 = 31
Branch analysis from position: 29
1 jumps found. (Code = 42) Position 1 = 32
Branch analysis from position: 32
2 jumps found. (Code = 43) Position 1 = 42, Position 2 = 47
Branch analysis from position: 42
2 jumps found. (Code = 43) Position 1 = 50, Position 2 = 52
Branch analysis from position: 50
1 jumps found. (Code = 42) Position 1 = 53
Branch analysis from position: 53
2 jumps found. (Code = 43) Position 1 = 66, Position 2 = 68
Branch analysis from position: 66
1 jumps found. (Code = 42) Position 1 = 69
Branch analysis from position: 69
2 jumps found. (Code = 77) Position 1 = 73, Position 2 = 84
Branch analysis from position: 73
2 jumps found. (Code = 78) Position 1 = 74, Position 2 = 84
Branch analysis from position: 74
1 jumps found. (Code = 42) Position 1 = 73
Branch analysis from position: 73
Branch analysis from position: 84
1 jumps found. (Code = 42) Position 1 = 2
Branch analysis from position: 2
Branch analysis from position: 84
Branch analysis from position: 68
2 jumps found. (Code = 77) Position 1 = 73, Position 2 = 84
Branch analysis from position: 73
Branch analysis from position: 84
Branch analysis from position: 52
2 jumps found. (Code = 43) Position 1 = 66, Position 2 = 68
Branch analysis from position: 66
Branch analysis from position: 68
Branch analysis from position: 47
Branch analysis from position: 31
2 jumps found. (Code = 43) Position 1 = 42, Position 2 = 47
Branch analysis from position: 42
Branch analysis from position: 47
Branch analysis from position: 87
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 87
filename:       /in/dDrUO
function name:  (null)
number of ops:  89
compiled vars:  !0 = $testCases, !1 = $testCase, !2 = $source, !3 = $scale, !4 = $result, !5 = $output, !6 = $roundedNumber1, !7 = $roundedNumber2, !8 = $roundedNumber3, !9 = $value, !10 = $key
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   16     0  E >   ASSIGN                                                   !0, <array>
   62     1      > FE_RESET_R                                       $12     !0, ->87
          2    > > FE_FETCH_R                                               $12, !1, ->87
   63     3    >   COUNT                                            ~13     !1
          4        IS_IDENTICAL                                             ~13, 0
          5      > JMPZ                                                     ~14, ->8
   64     6    >   ECHO                                                     '%0A'
   65     7      > JMP                                                      ->2
   67     8    >   FETCH_DIM_R                                      ~15     !1, 'source'
          9        ASSIGN                                                   !2, ~15
   68    10        FETCH_DIM_R                                      ~17     !1, 'scale'
         11        ASSIGN                                                   !3, ~17
   69    12        FETCH_DIM_R                                      ~19     !1, 'result'
         13        ASSIGN                                                   !4, ~19
   71    14        INIT_FCALL                                               'str_pad'
         15        SEND_VAR                                                 !2
         16        SEND_VAL                                                 25
         17        DO_ICALL                                         $21     
         18        CONCAT                                           ~22     $21, '+%3D%3E+'
         19        ECHO                                                     ~22
   73    20        ASSIGN                                                   !5, <array>
   76    21        INIT_FCALL                                               'mybcround'
         22        SEND_VAR                                                 !2
         23        SEND_VAR                                                 !3
         24        DO_FCALL                                      0  $24     
         25        ASSIGN                                                   !6, $24
   77    26        CONCAT                                           ~27     !6, '+'
         27        IS_IDENTICAL                                             !6, !4
         28      > JMPZ                                                     ~28, ->31
         29    >   QM_ASSIGN                                        ~29     '++OK'
         30      > JMP                                                      ->32
         31    >   QM_ASSIGN                                        ~29     'FAIL'
         32    >   CONCAT                                           ~30     ~27, ~29
         33        ASSIGN_DIM                                               !5, 'myBcRound'
         34        OP_DATA                                                  ~30
   80    35        ASSIGN                                                   !7, ''
   81    36        INIT_FCALL                                               'version_compare'
         37        SEND_VAL                                                 '8.3.0'
         38        SEND_VAL                                                 '8.4'
         39        SEND_VAL                                                 '%3E%3D'
         40        DO_ICALL                                         $32     
         41      > JMPZ                                                     $32, ->47
   82    42    >   INIT_FCALL_BY_NAME                                       'bcround'
         43        SEND_VAR_EX                                              !2
         44        SEND_VAR_EX                                              !3
         45        DO_FCALL                                      0  $33     
         46        ASSIGN                                                   !7, $33
   84    47    >   CONCAT                                           ~36     !7, '+'
         48        IS_IDENTICAL                                             !7, !4
         49      > JMPZ                                                     ~37, ->52
         50    >   QM_ASSIGN                                        ~38     '++OK'
         51      > JMP                                                      ->53
         52    >   QM_ASSIGN                                        ~38     'FAIL'
         53    >   CONCAT                                           ~39     ~36, ~38
         54        ASSIGN_DIM                                               !5, 'bcround'
         55        OP_DATA                                                  ~39
   87    56        INIT_FCALL                                               'round'
         57        CAST                                          5  ~40     !2
         58        SEND_VAL                                                 ~40
         59        SEND_VAR                                                 !3
         60        DO_ICALL                                         $41     
         61        CAST                                          6  ~42     $41
         62        ASSIGN                                                   !8, ~42
   88    63        CONCAT                                           ~45     !8, '+'
         64        IS_IDENTICAL                                             !8, !4
         65      > JMPZ                                                     ~46, ->68
         66    >   QM_ASSIGN                                        ~47     '++OK'
         67      > JMP                                                      ->69
         68    >   QM_ASSIGN                                        ~47     'FAIL'
         69    >   CONCAT                                           ~48     ~45, ~47
         70        ASSIGN_DIM                                               !5, 'round'
         71        OP_DATA                                                  ~48
   90    72      > FE_RESET_R                                       $49     !5, ->84
         73    > > FE_FETCH_R                                       ~50     $49, !9, ->84
         74    >   ASSIGN                                                   !10, ~50
   91    75        CONCAT                                           ~52     !10, '+%3D+'
         76        INIT_FCALL                                               'str_pad'
         77        SEND_VAR                                                 !9
         78        SEND_VAL                                                 7
         79        DO_ICALL                                         $53     
         80        CONCAT                                           ~54     ~52, $53
         81        CONCAT                                           ~55     ~54, '+'
         82        ECHO                                                     ~55
   90    83      > JMP                                                      ->73
         84    >   FE_FREE                                                  $49
   94    85        ECHO                                                     '%0A'
   62    86      > JMP                                                      ->2
         87    >   FE_FREE                                                  $12
   95    88      > RETURN                                                   1

Function mybcround:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 4, Position 2 = 12
Branch analysis from position: 4
1 jumps found. (Code = 42) Position 1 = 16
Branch analysis from position: 16
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 12
1 jumps found. (Code = 108) Position 1 = -2
filename:       /in/dDrUO
function name:  myBcRound
number of ops:  31
compiled vars:  !0 = $numstr, !1 = $precision, !2 = $addition
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    5     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      2
    7     2        IS_SMALLER_OR_EQUAL                                      0, !1
          3      > JMPZ                                                     ~3, ->12
    8     4    >   INIT_FCALL                                               'str_repeat'
          5        SEND_VAL                                                 '0'
          6        SEND_VAR                                                 !1
          7        DO_ICALL                                         $4      
          8        CONCAT                                           ~5      '0.', $4
          9        CONCAT                                           ~6      ~5, '5'
         10        ASSIGN                                                   !2, ~6
    7    11      > JMP                                                      ->16
   10    12    >   NEW                                              $8      'Exception'
         13        SEND_VAL_EX                                              'bcround+with+negative+precision+is+not+implemented.'
         14        DO_FCALL                                      0          
         15      > THROW                                         0          $8
   13    16    >   INIT_FCALL_BY_NAME                                       'bcdiv'
         17        INIT_FCALL_BY_NAME                                       'bcadd'
         18        SEND_VAR_EX                                              !0
         19        SEND_VAR_EX                                              !2
         20        ADD                                              ~10     !1, 1
         21        SEND_VAL_EX                                              ~10
         22        DO_FCALL                                      0  $11     
         23        SEND_VAR_NO_REF_EX                                       $11
         24        SEND_VAL_EX                                              '1'
         25        SEND_VAR_EX                                              !1
         26        DO_FCALL                                      0  $12     
         27        VERIFY_RETURN_TYPE                                       $12
         28      > RETURN                                                   $12
   14    29*       VERIFY_RETURN_TYPE                                       
         30*     > RETURN                                                   null

End of function mybcround

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
148 ms | 1029 KiB | 18 Q