3v4l.org

run code in 300+ PHP versions simultaneously
<?php var_dump(encodeFloat(1.1234567890123456, 13)); function encodeFloat($float, $precision) { $precision = max(1, (int) $precision); $log = (int) floor(log(abs($float), 10)); if (abs($float) < 9007199254740992.0 && $log > -5 && abs($log) < $precision) { return $this->formatFloat($float, $precision - $log - 1); } // Deal with overflow that results from rounding $log += (int) (round(abs($float) / pow(10, $log), $precision - 1) / 10); $string = $this->formatFloat($float / pow(10, $log), $precision - 1); return sprintf('%sE%+d', $string, $log); } function formatFloat($float, $digits) { $digits = max((int) $digits, 1); $string = rtrim(number_format($float, $digits, '.', ''), '0'); return substr($string, -1) === '.' ? $string . '0' : $string; }
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/9G9cX
function name:  (null)
number of ops:  8
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    2     0  E >   INIT_FCALL                                               'var_dump'
          1        INIT_FCALL_BY_NAME                                       'encodeFloat'
          2        SEND_VAL_EX                                              1.12346
          3        SEND_VAL_EX                                              13
          4        DO_FCALL                                      0  $0      
          5        SEND_VAR                                                 $0
          6        DO_ICALL                                                 
   26     7      > RETURN                                                   1

Function encodefloat:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 46) Position 1 = 25, Position 2 = 27
Branch analysis from position: 25
2 jumps found. (Code = 46) Position 1 = 28, Position 2 = 33
Branch analysis from position: 28
2 jumps found. (Code = 43) Position 1 = 34, Position 2 = 42
Branch analysis from position: 34
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 42
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 33
Branch analysis from position: 27
filename:       /in/9G9cX
function name:  encodeFloat
number of ops:  77
compiled vars:  !0 = $float, !1 = $precision, !2 = $log, !3 = $string
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    4     0  E >   RECV                                             !0      
          1        RECV                                             !1      
    6     2        INIT_FCALL                                               'max'
          3        SEND_VAL                                                 1
          4        CAST                                          4  ~4      !1
          5        SEND_VAL                                                 ~4
          6        DO_ICALL                                         $5      
          7        ASSIGN                                                   !1, $5
    7     8        INIT_FCALL                                               'floor'
          9        INIT_FCALL                                               'log'
         10        INIT_FCALL                                               'abs'
         11        SEND_VAR                                                 !0
         12        DO_ICALL                                         $7      
         13        SEND_VAR                                                 $7
         14        SEND_VAL                                                 10
         15        DO_ICALL                                         $8      
         16        SEND_VAR                                                 $8
         17        DO_ICALL                                         $9      
         18        CAST                                          4  ~10     $9
         19        ASSIGN                                                   !2, ~10
    9    20        INIT_FCALL                                               'abs'
         21        SEND_VAR                                                 !0
         22        DO_ICALL                                         $12     
         23        IS_SMALLER                                       ~13     $12, 9.0072e+15
         24      > JMPZ_EX                                          ~13     ~13, ->27
         25    >   IS_SMALLER                                       ~14     -5, !2
         26        BOOL                                             ~13     ~14
         27    > > JMPZ_EX                                          ~13     ~13, ->33
         28    >   INIT_FCALL                                               'abs'
         29        SEND_VAR                                                 !2
         30        DO_ICALL                                         $15     
         31        IS_SMALLER                                       ~16     $15, !1
         32        BOOL                                             ~13     ~16
         33    > > JMPZ                                                     ~13, ->42
   10    34    >   FETCH_THIS                                       $17     
         35        INIT_METHOD_CALL                                         $17, 'formatFloat'
         36        SEND_VAR_EX                                              !0
         37        SUB                                              ~18     !1, !2
         38        SUB                                              ~19     ~18, 1
         39        SEND_VAL_EX                                              ~19
         40        DO_FCALL                                      0  $20     
         41      > RETURN                                                   $20
   14    42    >   INIT_FCALL                                               'round'
         43        INIT_FCALL                                               'abs'
         44        SEND_VAR                                                 !0
         45        DO_ICALL                                         $21     
         46        INIT_FCALL                                               'pow'
         47        SEND_VAL                                                 10
         48        SEND_VAR                                                 !2
         49        DO_ICALL                                         $22     
         50        DIV                                              ~23     $21, $22
         51        SEND_VAL                                                 ~23
         52        SUB                                              ~24     !1, 1
         53        SEND_VAL                                                 ~24
         54        DO_ICALL                                         $25     
         55        DIV                                              ~26     $25, 10
         56        CAST                                          4  ~27     ~26
         57        ASSIGN_OP                                     1          !2, ~27
   15    58        FETCH_THIS                                       $29     
         59        INIT_METHOD_CALL                                         $29, 'formatFloat'
         60        INIT_FCALL                                               'pow'
         61        SEND_VAL                                                 10
         62        SEND_VAR                                                 !2
         63        DO_ICALL                                         $30     
         64        DIV                                              ~31     !0, $30
         65        SEND_VAL_EX                                              ~31
         66        SUB                                              ~32     !1, 1
         67        SEND_VAL_EX                                              ~32
         68        DO_FCALL                                      0  $33     
         69        ASSIGN                                                   !3, $33
   17    70        INIT_FCALL                                               'sprintf'
         71        SEND_VAL                                                 '%25sE%25%2Bd'
         72        SEND_VAR                                                 !3
         73        SEND_VAR                                                 !2
         74        DO_ICALL                                         $35     
         75      > RETURN                                                   $35
   18    76*     > RETURN                                                   null

End of function encodefloat

Function formatfloat:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 25, Position 2 = 28
Branch analysis from position: 25
1 jumps found. (Code = 42) Position 1 = 29
Branch analysis from position: 29
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 28
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/9G9cX
function name:  formatFloat
number of ops:  31
compiled vars:  !0 = $float, !1 = $digits, !2 = $string
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   20     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   22     2        INIT_FCALL                                               'max'
          3        CAST                                          4  ~3      !1
          4        SEND_VAL                                                 ~3
          5        SEND_VAL                                                 1
          6        DO_ICALL                                         $4      
          7        ASSIGN                                                   !1, $4
   23     8        INIT_FCALL                                               'rtrim'
          9        INIT_FCALL                                               'number_format'
         10        SEND_VAR                                                 !0
         11        SEND_VAR                                                 !1
         12        SEND_VAL                                                 '.'
         13        SEND_VAL                                                 ''
         14        DO_ICALL                                         $6      
         15        SEND_VAR                                                 $6
         16        SEND_VAL                                                 '0'
         17        DO_ICALL                                         $7      
         18        ASSIGN                                                   !2, $7
   25    19        INIT_FCALL                                               'substr'
         20        SEND_VAR                                                 !2
         21        SEND_VAL                                                 -1
         22        DO_ICALL                                         $9      
         23        IS_IDENTICAL                                             $9, '.'
         24      > JMPZ                                                     ~10, ->28
         25    >   CONCAT                                           ~11     !2, '0'
         26        QM_ASSIGN                                        ~12     ~11
         27      > JMP                                                      ->29
         28    >   QM_ASSIGN                                        ~12     !2
         29    > > RETURN                                                   ~12
   26    30*     > RETURN                                                   null

End of function formatfloat

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
161.88 ms | 1404 KiB | 35 Q