3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** * Immutable class for handling numbers with precision. * * Never change the $number or the $precision after the creation of the object. */ class DecimalNumber { private $precision; private $number; public function __construct() { return $this; } /** * Get an instance given a string and a precision * @param string $string The decimal number in string form * @param int $precision The precision of the number * @return DecimalNumber Return new instance */ public static function get($string, $precision = 4) { $number = static::_get($string, $precision); $number->number = bcadd($string, '0', $precision); return $number; } private static function _get($string, $precision) { $me = get_called_class(); $number = new $me; $number->precision = $precision; $number->number = $string; return $number; } /** * Add a formal entry point for decimal numbers * @param DecimalNumber $number Decimal number to base off of * @param integer $precision new precision * @return DecimalNumber DecimalNumber with new precision */ public static function decimalNumber(DecimalNumber $number, $precision = 4) { if ($number->precision === $precision) return clone $number; return static::get($number, $precision); } /** * Perform an addition operation * @param DecimalNumber $number Addend * @return DecimalNumber Sum */ public function sum(DecimalNumber $number) { return DecimalNumber::_get(bcadd($this->number, $number, $this->precision)); } /** * Perform a subtraction operation * @param DecimalNumber $number Addend * @return DecimalNumber Sum */ public function difference(DecimalNumber $number) { return DecimalNumber::_get(bcsub($this->number, $number, $this->precision)); } /** * Perform a multiplication operation * @param DecimalNumber $number Multiplier * @return DecimalNumber Product */ public function product(DecimalNumber $number) { return DecimalNumber::_get(bcmul($this->number, $number, $this->precision)); } /** * Perform a division operation * @param DecimalNumber $number Divisor * @return DecimalNumber Quotient */ public function quotient(DecimalNumber $number) { return DecimalNumber::_get(bcdiv($this->number, $number, $this->precision)); } public function floored() { return DecimalNumber::_get(bcadd(bcadd($this->number, '0'), '0', $this->precision), $this->precision); } /** * Automagically convert to a string * @return string returns a string containing the value of this DecimalNumber */ public function __toString() { return $this->number; } } $a = DecimalNumber::get('.7'); $b = DecimalNumber::get('.1'); $c = $a->add($b); $d = $c->product(DecimalNumber::get('10'));
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/rVIm4
function name:  (null)
number of ops:  21
compiled vars:  !0 = $a, !1 = $b, !2 = $c, !3 = $d
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    7     0  E >   DECLARE_CLASS                                            'decimalnumber'
  100     1        INIT_STATIC_METHOD_CALL                                  'DecimalNumber', 'get'
          2        SEND_VAL_EX                                              '.7'
          3        DO_FCALL                                      0  $4      
          4        ASSIGN                                                   !0, $4
  101     5        INIT_STATIC_METHOD_CALL                                  'DecimalNumber', 'get'
          6        SEND_VAL_EX                                              '.1'
          7        DO_FCALL                                      0  $6      
          8        ASSIGN                                                   !1, $6
  103     9        INIT_METHOD_CALL                                         !0, 'add'
         10        SEND_VAR_EX                                              !1
         11        DO_FCALL                                      0  $8      
         12        ASSIGN                                                   !2, $8
  104    13        INIT_METHOD_CALL                                         !2, 'product'
         14        INIT_STATIC_METHOD_CALL                                  'DecimalNumber', 'get'
         15        SEND_VAL_EX                                              '10'
         16        DO_FCALL                                      0  $10     
         17        SEND_VAR_NO_REF_EX                                       $10
         18        DO_FCALL                                      0  $11     
         19        ASSIGN                                                   !3, $11
         20      > RETURN                                                   1

Class DecimalNumber:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/rVIm4
function name:  __construct
number of ops:  3
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   13     0  E >   FETCH_THIS                                       ~0      
          1      > RETURN                                                   ~0
   14     2*     > RETURN                                                   null

End of function __construct

Function get:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/rVIm4
function name:  get
number of ops:  16
compiled vars:  !0 = $string, !1 = $precision, !2 = $number
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   22     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      4
   23     2        INIT_STATIC_METHOD_CALL                                  '_get'
          3        SEND_VAR_EX                                              !0
          4        SEND_VAR_EX                                              !1
          5        DO_FCALL                                      0  $3      
          6        ASSIGN                                                   !2, $3
   24     7        INIT_FCALL_BY_NAME                                       'bcadd'
          8        SEND_VAR_EX                                              !0
          9        SEND_VAL_EX                                              '0'
         10        SEND_VAR_EX                                              !1
         11        DO_FCALL                                      0  $6      
         12        ASSIGN_OBJ                                               !2, 'number'
         13        OP_DATA                                                  $6
   26    14      > RETURN                                                   !2
   27    15*     > RETURN                                                   null

End of function get

Function _get:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/rVIm4
function name:  _get
number of ops:  14
compiled vars:  !0 = $string, !1 = $precision, !2 = $me, !3 = $number
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   29     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   30     2        GET_CALLED_CLASS                                 ~4      
          3        ASSIGN                                                   !2, ~4
   31     4        FETCH_CLASS                                   0  $6      !2
          5        NEW                                              $7      $6
          6        DO_FCALL                                      0          
          7        ASSIGN                                                   !3, $7
   33     8        ASSIGN_OBJ                                               !3, 'precision'
          9        OP_DATA                                                  !1
   34    10        ASSIGN_OBJ                                               !3, 'number'
         11        OP_DATA                                                  !0
   36    12      > RETURN                                                   !3
   37    13*     > RETURN                                                   null

End of function _get

Function decimalnumber:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 5, Position 2 = 7
Branch analysis from position: 5
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 7
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/rVIm4
function name:  decimalNumber
number of ops:  13
compiled vars:  !0 = $number, !1 = $precision
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   45     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      4
   46     2        FETCH_OBJ_R                                      ~2      !0, 'precision'
          3        IS_IDENTICAL                                             !1, ~2
          4      > JMPZ                                                     ~3, ->7
          5    >   CLONE                                            ~4      !0
          6      > RETURN                                                   ~4
   47     7    >   INIT_STATIC_METHOD_CALL                                  'get'
          8        SEND_VAR_EX                                              !0
          9        SEND_VAR_EX                                              !1
         10        DO_FCALL                                      0  $5      
         11      > RETURN                                                   $5
   48    12*     > RETURN                                                   null

End of function decimalnumber

Function sum:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/rVIm4
function name:  sum
number of ops:  15
compiled vars:  !0 = $number
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   55     0  E >   RECV                                             !0      
   56     1        INIT_STATIC_METHOD_CALL                                  'DecimalNumber', '_get'
          2        INIT_FCALL_BY_NAME                                       'bcadd'
          3        CHECK_FUNC_ARG                                           
          4        FETCH_OBJ_FUNC_ARG                               $1      'number'
          5        SEND_FUNC_ARG                                            $1
          6        SEND_VAR_EX                                              !0
          7        CHECK_FUNC_ARG                                           
          8        FETCH_OBJ_FUNC_ARG                               $2      'precision'
          9        SEND_FUNC_ARG                                            $2
         10        DO_FCALL                                      0  $3      
         11        SEND_VAR                                                 $3
         12        DO_FCALL                                      0  $4      
         13      > RETURN                                                   $4
   57    14*     > RETURN                                                   null

End of function sum

Function difference:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/rVIm4
function name:  difference
number of ops:  15
compiled vars:  !0 = $number
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   63     0  E >   RECV                                             !0      
   64     1        INIT_STATIC_METHOD_CALL                                  'DecimalNumber', '_get'
          2        INIT_FCALL_BY_NAME                                       'bcsub'
          3        CHECK_FUNC_ARG                                           
          4        FETCH_OBJ_FUNC_ARG                               $1      'number'
          5        SEND_FUNC_ARG                                            $1
          6        SEND_VAR_EX                                              !0
          7        CHECK_FUNC_ARG                                           
          8        FETCH_OBJ_FUNC_ARG                               $2      'precision'
          9        SEND_FUNC_ARG                                            $2
         10        DO_FCALL                                      0  $3      
         11        SEND_VAR                                                 $3
         12        DO_FCALL                                      0  $4      
         13      > RETURN                                                   $4
   65    14*     > RETURN                                                   null

End of function difference

Function product:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/rVIm4
function name:  product
number of ops:  15
compiled vars:  !0 = $number
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   72     0  E >   RECV                                             !0      
   73     1        INIT_STATIC_METHOD_CALL                                  'DecimalNumber', '_get'
          2        INIT_FCALL_BY_NAME                                       'bcmul'
          3        CHECK_FUNC_ARG                                           
          4        FETCH_OBJ_FUNC_ARG                               $1      'number'
          5        SEND_FUNC_ARG                                            $1
          6        SEND_VAR_EX                                              !0
          7        CHECK_FUNC_ARG                                           
          8        FETCH_OBJ_FUNC_ARG                               $2      'precision'
          9        SEND_FUNC_ARG                                            $2
         10        DO_FCALL                                      0  $3      
         11        SEND_VAR                                                 $3
         12        DO_FCALL                                      0  $4      
         13      > RETURN                                                   $4
   74    14*     > RETURN                                                   null

End of function product

Function quotient:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/rVIm4
function name:  quotient
number of ops:  15
compiled vars:  !0 = $number
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   81     0  E >   RECV                                             !0      
   82     1        INIT_STATIC_METHOD_CALL                                  'DecimalNumber', '_get'
          2        INIT_FCALL_BY_NAME                                       'bcdiv'
          3        CHECK_FUNC_ARG                                           
          4        FETCH_OBJ_FUNC_ARG                               $1      'number'
          5        SEND_FUNC_ARG                                            $1
          6        SEND_VAR_EX                                              !0
          7        CHECK_FUNC_ARG                                           
          8        FETCH_OBJ_FUNC_ARG                               $2      'precision'
          9        SEND_FUNC_ARG                                            $2
         10        DO_FCALL                                      0  $3      
         11        SEND_VAR                                                 $3
         12        DO_FCALL                                      0  $4      
         13      > RETURN                                                   $4
   83    14*     > RETURN                                                   null

End of function quotient

Function floored:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/rVIm4
function name:  floored
number of ops:  20
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   86     0  E >   INIT_STATIC_METHOD_CALL                                  'DecimalNumber', '_get'
          1        INIT_FCALL_BY_NAME                                       'bcadd'
          2        INIT_FCALL_BY_NAME                                       'bcadd'
          3        CHECK_FUNC_ARG                                           
          4        FETCH_OBJ_FUNC_ARG                               $0      'number'
          5        SEND_FUNC_ARG                                            $0
          6        SEND_VAL_EX                                              '0'
          7        DO_FCALL                                      0  $1      
          8        SEND_VAR_NO_REF_EX                                       $1
          9        SEND_VAL_EX                                              '0'
         10        CHECK_FUNC_ARG                                           
         11        FETCH_OBJ_FUNC_ARG                               $2      'precision'
         12        SEND_FUNC_ARG                                            $2
         13        DO_FCALL                                      0  $3      
         14        SEND_VAR                                                 $3
         15        FETCH_OBJ_R                                      ~4      'precision'
         16        SEND_VAL                                                 ~4
         17        DO_FCALL                                      0  $5      
         18      > RETURN                                                   $5
   87    19*     > RETURN                                                   null

End of function floored

Function __tostring:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/rVIm4
function name:  __toString
number of ops:  5
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   94     0  E >   FETCH_OBJ_R                                      ~0      'number'
          1        VERIFY_RETURN_TYPE                                       ~0
          2      > RETURN                                                   ~0
   95     3*       VERIFY_RETURN_TYPE                                       
          4*     > RETURN                                                   null

End of function __tostring

End of class DecimalNumber.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
180.96 ms | 1411 KiB | 25 Q