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; /** * 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) { $me = get_called_class(); $number = new $me; $number->precision = $precision; $number->number = bcadd($string, '0', $precision); 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 getWithDecimalNumber(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::getWithDecimalNumber(bcadd($this->number, $number, $this->precision)); } /** * Perform a subtraction operation * @param DecimalNumber $number Addend * @return DecimalNumber Sum */ public function difference(DecimalNumber $number) { return DecimalNumber::getWithDecimalNumber(bcsub($this->number, $number, $this->precision)); } /** * Perform a multiplication operation * @param DecimalNumber $number Multiplier * @return DecimalNumber Product */ public function product(DecimalNumber $number) { return DecimalNumber::getWithDecimalNumber(bcmul($this->number, $number, $this->precision)); } /** * Perform a division operation * @param DecimalNumber $number Divisor * @return DecimalNumber Quotient */ public function quotient(DecimalNumber $number) { return DecimalNumber::getWithDecimalNumber(bcdiv($this->number, $number, $this->precision)); } public function floored() { return DecimalNumber::getWithDecimalNumber(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/BhFUO
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'
   89     1        INIT_STATIC_METHOD_CALL                                  'DecimalNumber', 'get'
          2        SEND_VAL_EX                                              '.7'
          3        DO_FCALL                                      0  $4      
          4        ASSIGN                                                   !0, $4
   90     5        INIT_STATIC_METHOD_CALL                                  'DecimalNumber', 'get'
          6        SEND_VAL_EX                                              '.1'
          7        DO_FCALL                                      0  $6      
          8        ASSIGN                                                   !1, $6
   92     9        INIT_METHOD_CALL                                         !0, 'add'
         10        SEND_VAR_EX                                              !1
         11        DO_FCALL                                      0  $8      
         12        ASSIGN                                                   !2, $8
   93    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 get:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/BhFUO
function name:  get
number of ops:  19
compiled vars:  !0 = $string, !1 = $precision, !2 = $me, !3 = $number
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   18     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      4
   19     2        GET_CALLED_CLASS                                 ~4      
          3        ASSIGN                                                   !2, ~4
   20     4        FETCH_CLASS                                   0  $6      !2
          5        NEW                                              $7      $6
          6        DO_FCALL                                      0          
          7        ASSIGN                                                   !3, $7
   22     8        ASSIGN_OBJ                                               !3, 'precision'
          9        OP_DATA                                                  !1
   23    10        INIT_FCALL_BY_NAME                                       'bcadd'
         11        SEND_VAR_EX                                              !0
         12        SEND_VAL_EX                                              '0'
         13        SEND_VAR_EX                                              !1
         14        DO_FCALL                                      0  $12     
         15        ASSIGN_OBJ                                               !3, 'number'
         16        OP_DATA                                                  $12
   25    17      > RETURN                                                   !3
   26    18*     > RETURN                                                   null

End of function get

Function getwithdecimalnumber:
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/BhFUO
function name:  getWithDecimalNumber
number of ops:  13
compiled vars:  !0 = $number, !1 = $precision
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   34     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      4
   35     2        FETCH_OBJ_R                                      ~2      !0, 'precision'
          3        IS_IDENTICAL                                             !1, ~2
          4      > JMPZ                                                     ~3, ->7
          5    >   CLONE                                            ~4      !0
          6      > RETURN                                                   ~4
   36     7    >   INIT_STATIC_METHOD_CALL                                  'get'
          8        SEND_VAR_EX                                              !0
          9        SEND_VAR_EX                                              !1
         10        DO_FCALL                                      0  $5      
         11      > RETURN                                                   $5
   37    12*     > RETURN                                                   null

End of function getwithdecimalnumber

Function sum:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/BhFUO
function name:  sum
number of ops:  15
compiled vars:  !0 = $number
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   44     0  E >   RECV                                             !0      
   45     1        INIT_STATIC_METHOD_CALL                                  'DecimalNumber', 'getWithDecimalNumber'
          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
   46    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/BhFUO
function name:  difference
number of ops:  15
compiled vars:  !0 = $number
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   52     0  E >   RECV                                             !0      
   53     1        INIT_STATIC_METHOD_CALL                                  'DecimalNumber', 'getWithDecimalNumber'
          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
   54    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/BhFUO
function name:  product
number of ops:  15
compiled vars:  !0 = $number
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   61     0  E >   RECV                                             !0      
   62     1        INIT_STATIC_METHOD_CALL                                  'DecimalNumber', 'getWithDecimalNumber'
          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
   63    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/BhFUO
function name:  quotient
number of ops:  15
compiled vars:  !0 = $number
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   70     0  E >   RECV                                             !0      
   71     1        INIT_STATIC_METHOD_CALL                                  'DecimalNumber', 'getWithDecimalNumber'
          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
   72    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/BhFUO
function name:  floored
number of ops:  20
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   75     0  E >   INIT_STATIC_METHOD_CALL                                  'DecimalNumber', 'getWithDecimalNumber'
          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
   76    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/BhFUO
function name:  __toString
number of ops:  5
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   83     0  E >   FETCH_OBJ_R                                      ~0      'number'
          1        VERIFY_RETURN_TYPE                                       ~0
          2      > RETURN                                                   ~0
   84     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:
163.63 ms | 1398 KiB | 25 Q