3v4l.org

run code in 300+ PHP versions simultaneously
<?php ini_set('serialize_precision', 13); $encoder = new FloatEncoder(); var_dump($encoder->encode(1.1234567890123456, 1, [ 'float.integers' => false, 'float.precision' => false, ], function () { })); class FloatEncoder { /** The maximum value that can be accurately represented by a float */ const FLOAT_MAX = 9007199254740992.0; /** @var array Default values for options in the encoder */ private static $defaultOptions = [ 'float.integers' => false, 'float.precision' => 17, ]; public function getDefaultOptions() { return self::$defaultOptions; } public function supports($value) { return is_float($value); } public function encode($value, $depth, array $options, callable $encode) { if (is_nan($value)) { return 'NAN'; } elseif (is_infinite($value)) { return $value < 0 ? '-INF' : 'INF'; } return $this->encodeNumber($value, $options); } /** * Encodes the number as a PHP number representation. * @param float $float The number to encode * @param array $options The float encoding options * @return string The PHP code representation for the number */ private function encodeNumber($float, array $options) { if ($this->isInteger($float, $options['float.integers'])) { return number_format($float, 0, '.', ''); } elseif ($float === 0.0) { return '0.0'; } $precision = $options['float.precision']; if ($precision === false) { $precision = ini_get('serialize_precision'); } return $this->encodeFloat($float, $precision); } /** * Tells if the number can be encoded as an integer. * @param float $float The number to test * @param bool|string $allowIntegers Whether integers should be allowed * @return bool True if the number can be encoded as an integer, false if not */ private function isInteger($float, $allowIntegers) { if (!$allowIntegers || round($float) !== $float) { return false; } elseif (abs($float) < self::FLOAT_MAX) { return true; } return $allowIntegers === 'all'; } /** * Encodes the number using a floating point representation. * @param float $float The number to encode * @param int $precision The maximum precision of encoded floats * @return string The PHP code representation for the number */ private function encodeFloat($float, $precision) { $precision = max(1, (int) $precision); $log = (int) floor(log(abs($float), 10)); if (abs($float) < self::FLOAT_MAX && $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); } /** * Formats the number as a decimal number. * @param float $float The number to format * @param int $digits The maximum number of decimal digits * @return string The number formatted as a decimal number */ private 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/2EWaZ
function name:  (null)
number of ops:  18
compiled vars:  !0 = $encoder
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   INIT_FCALL                                               'ini_set'
          1        SEND_VAL                                                 'serialize_precision'
          2        SEND_VAL                                                 13
          3        DO_ICALL                                                 
    5     4        NEW                                              $2      'FloatEncoder'
          5        DO_FCALL                                      0          
          6        ASSIGN                                                   !0, $2
    6     7        INIT_FCALL                                               'var_dump'
          8        INIT_METHOD_CALL                                         !0, 'encode'
          9        SEND_VAL_EX                                              1.12346
         10        SEND_VAL_EX                                              1
    7    11        SEND_VAL_EX                                              <array>
    9    12        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2F2EWaZ%3A9%240'
         13        SEND_VAL_EX                                              ~5
         14        DO_FCALL                                      0  $6      
         15        SEND_VAR                                                 $6
         16        DO_ICALL                                                 
  118    17      > RETURN                                                   1

Function %00%7Bclosure%7D%2Fin%2F2EWaZ%3A9%240:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/2EWaZ
function name:  {closure}
number of ops:  1
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    9     0  E > > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2F2EWaZ%3A9%240

Class FloatEncoder:
Function getdefaultoptions:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/2EWaZ
function name:  getDefaultOptions
number of ops:  3
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   24     0  E >   FETCH_STATIC_PROP_R          unknown             ~0      'defaultOptions'
          1      > RETURN                                                   ~0
   25     2*     > RETURN                                                   null

End of function getdefaultoptions

Function supports:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/2EWaZ
function name:  supports
number of ops:  4
compiled vars:  !0 = $value
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   27     0  E >   RECV                                             !0      
   29     1        TYPE_CHECK                                   32  ~1      !0
          2      > RETURN                                                   ~1
   30     3*     > RETURN                                                   null

End of function supports

Function encode:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 8, Position 2 = 10
Branch analysis from position: 8
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 10
2 jumps found. (Code = 43) Position 1 = 14, Position 2 = 20
Branch analysis from position: 14
2 jumps found. (Code = 43) Position 1 = 16, Position 2 = 18
Branch analysis from position: 16
1 jumps found. (Code = 42) Position 1 = 19
Branch analysis from position: 19
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 18
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 20
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/2EWaZ
function name:  encode
number of ops:  26
compiled vars:  !0 = $value, !1 = $depth, !2 = $options, !3 = $encode
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   32     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV                                             !2      
          3        RECV                                             !3      
   34     4        INIT_FCALL                                               'is_nan'
          5        SEND_VAR                                                 !0
          6        DO_ICALL                                         $4      
          7      > JMPZ                                                     $4, ->10
   35     8    > > RETURN                                                   'NAN'
          9*       JMP                                                      ->20
   36    10    >   INIT_FCALL                                               'is_infinite'
         11        SEND_VAR                                                 !0
         12        DO_ICALL                                         $5      
         13      > JMPZ                                                     $5, ->20
   37    14    >   IS_SMALLER                                               !0, 0
         15      > JMPZ                                                     ~6, ->18
         16    >   QM_ASSIGN                                        ~7      '-INF'
         17      > JMP                                                      ->19
         18    >   QM_ASSIGN                                        ~7      'INF'
         19    > > RETURN                                                   ~7
   40    20    >   INIT_METHOD_CALL                                         'encodeNumber'
         21        SEND_VAR_EX                                              !0
         22        SEND_VAR_EX                                              !2
         23        DO_FCALL                                      0  $8      
         24      > RETURN                                                   $8
   41    25*     > RETURN                                                   null

End of function encode

Function encodenumber:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 9, Position 2 = 17
Branch analysis from position: 9
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 17
2 jumps found. (Code = 43) Position 1 = 19, Position 2 = 20
Branch analysis from position: 19
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 20
2 jumps found. (Code = 43) Position 1 = 24, Position 2 = 28
Branch analysis from position: 24
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 28
filename:       /in/2EWaZ
function name:  encodeNumber
number of ops:  34
compiled vars:  !0 = $float, !1 = $options, !2 = $precision
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   49     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   51     2        INIT_METHOD_CALL                                         'isInteger'
          3        SEND_VAR_EX                                              !0
          4        CHECK_FUNC_ARG                                           
          5        FETCH_DIM_FUNC_ARG                               $3      !1, 'float.integers'
          6        SEND_FUNC_ARG                                            $3
          7        DO_FCALL                                      0  $4      
          8      > JMPZ                                                     $4, ->17
   52     9    >   INIT_FCALL                                               'number_format'
         10        SEND_VAR                                                 !0
         11        SEND_VAL                                                 0
         12        SEND_VAL                                                 '.'
         13        SEND_VAL                                                 ''
         14        DO_ICALL                                         $5      
         15      > RETURN                                                   $5
         16*       JMP                                                      ->20
   53    17    >   IS_IDENTICAL                                             !0, 0
         18      > JMPZ                                                     ~6, ->20
   54    19    > > RETURN                                                   '0.0'
   57    20    >   FETCH_DIM_R                                      ~7      !1, 'float.precision'
         21        ASSIGN                                                   !2, ~7
   59    22        TYPE_CHECK                                    4          !2
         23      > JMPZ                                                     ~9, ->28
   60    24    >   INIT_FCALL                                               'ini_get'
         25        SEND_VAL                                                 'serialize_precision'
         26        DO_ICALL                                         $10     
         27        ASSIGN                                                   !2, $10
   63    28    >   INIT_METHOD_CALL                                         'encodeFloat'
         29        SEND_VAR_EX                                              !0
         30        SEND_VAR_EX                                              !2
         31        DO_FCALL                                      0  $12     
         32      > RETURN                                                   $12
   64    33*     > RETURN                                                   null

End of function encodenumber

Function isinteger:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 47) Position 1 = 4, Position 2 = 9
Branch analysis from position: 4
2 jumps found. (Code = 43) Position 1 = 10, Position 2 = 12
Branch analysis from position: 10
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 12
2 jumps found. (Code = 43) Position 1 = 17, Position 2 = 18
Branch analysis from position: 17
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 18
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 9
filename:       /in/2EWaZ
function name:  isInteger
number of ops:  21
compiled vars:  !0 = $float, !1 = $allowIntegers
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   72     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   74     2        BOOL_NOT                                         ~2      !1
          3      > JMPNZ_EX                                         ~2      ~2, ->9
          4    >   INIT_FCALL                                               'round'
          5        SEND_VAR                                                 !0
          6        DO_ICALL                                         $3      
          7        IS_NOT_IDENTICAL                                 ~4      !0, $3
          8        BOOL                                             ~2      ~4
          9    > > JMPZ                                                     ~2, ->12
   75    10    > > RETURN                                                   <false>
         11*       JMP                                                      ->18
   76    12    >   INIT_FCALL                                               'abs'
         13        SEND_VAR                                                 !0
         14        DO_ICALL                                         $5      
         15        IS_SMALLER                                               $5, 9.0072e+15
         16      > JMPZ                                                     ~6, ->18
   77    17    > > RETURN                                                   <true>
   80    18    >   IS_IDENTICAL                                     ~7      !1, 'all'
         19      > RETURN                                                   ~7
   81    20*     > RETURN                                                   null

End of function isinteger

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 = 41
Branch analysis from position: 34
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 41
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 33
Branch analysis from position: 27
filename:       /in/2EWaZ
function name:  encodeFloat
number of ops:  75
compiled vars:  !0 = $float, !1 = $precision, !2 = $log, !3 = $string
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   89     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   91     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
   92     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
   94    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, ->41
   95    34    >   INIT_METHOD_CALL                                         'formatFloat'
         35        SEND_VAR_EX                                              !0
         36        SUB                                              ~17     !1, !2
         37        SUB                                              ~18     ~17, 1
         38        SEND_VAL_EX                                              ~18
         39        DO_FCALL                                      0  $19     
         40      > RETURN                                                   $19
   99    41    >   INIT_FCALL                                               'round'
         42        INIT_FCALL                                               'abs'
         43        SEND_VAR                                                 !0
         44        DO_ICALL                                         $20     
         45        INIT_FCALL                                               'pow'
         46        SEND_VAL                                                 10
         47        SEND_VAR                                                 !2
         48        DO_ICALL                                         $21     
         49        DIV                                              ~22     $20, $21
         50        SEND_VAL                                                 ~22
         51        SUB                                              ~23     !1, 1
         52        SEND_VAL                                                 ~23
         53        DO_ICALL                                         $24     
         54        DIV                                              ~25     $24, 10
         55        CAST                                          4  ~26     ~25
         56        ASSIGN_OP                                     1          !2, ~26
  100    57        INIT_METHOD_CALL                                         'formatFloat'
         58        INIT_FCALL                                               'pow'
         59        SEND_VAL                                                 10
         60        SEND_VAR                                                 !2
         61        DO_ICALL                                         $28     
         62        DIV                                              ~29     !0, $28
         63        SEND_VAL_EX                                              ~29
         64        SUB                                              ~30     !1, 1
         65        SEND_VAL_EX                                              ~30
         66        DO_FCALL                                      0  $31     
         67        ASSIGN                                                   !3, $31
  102    68        INIT_FCALL                                               'sprintf'
         69        SEND_VAL                                                 '%25sE%25%2Bd'
         70        SEND_VAR                                                 !3
         71        SEND_VAR                                                 !2
         72        DO_ICALL                                         $33     
         73      > RETURN                                                   $33
  103    74*     > 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/2EWaZ
function name:  formatFloat
number of ops:  31
compiled vars:  !0 = $float, !1 = $digits, !2 = $string
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  111     0  E >   RECV                                             !0      
          1        RECV                                             !1      
  113     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
  114     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
  116    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
  117    30*     > RETURN                                                   null

End of function formatfloat

End of class FloatEncoder.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
173.91 ms | 1416 KiB | 43 Q