3v4l.org

run code in 300+ PHP versions simultaneously
<?php function fractionToDecimal($fraction) { // Split fraction into whole number and fraction components preg_match('/^(?P<whole>\d+)?\s?((?P<numerator>\d+)\/(?P<denominator>\d+))?$/', $fraction, $components); // Extract whole number, numerator, and denominator components $whole = $components['whole'] ?: 0; $numerator = $components['numerator'] ?: 0; $denominator = $components['denominator'] ?: 0; // Create decimal value $decimal = $whole; $numerator && $denominator && $decimal += ($numerator/$denominator); return $decimal; } function decimalToFraction($decimal) { // Determine decimal precision and extrapolate multiplier required to convert to integer $precision = strpos(strrev($decimal), '.') ?: 0; $multiplier = pow(10, $precision); // Calculate initial numerator and denominator $numerator = $decimal * $multiplier; $denominator = 1 * $multiplier; // Extract whole number from numerator $whole = floor($numerator / $denominator); $numerator = $numerator % $denominator; // Find greatest common divisor between numerator and denominator and reduce accordingly $factor = gmp_intval(gmp_gcd($numerator, $denominator)); $numerator /= $factor; $denominator /= $factor; // Create fraction value $fraction = []; $whole && $fraction[] = $whole; $numerator && $fraction[] = "{$numerator}/{$denominator}"; return implode(' ', $fraction); } // Examples var_dump(fractionToDecimal('1/25')); var_dump(fractionToDecimal('2 3/4')); var_dump(fractionToDecimal('6/4')); var_dump(decimalToFraction(1.375)); var_dump(decimalToFraction(3)); var_dump(decimalToFraction(2.875));
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/ft0ZT
function name:  (null)
number of ops:  37
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   48     0  E >   INIT_FCALL                                               'var_dump'
          1        INIT_FCALL                                               'fractiontodecimal'
          2        SEND_VAL                                                 '1%2F25'
          3        DO_FCALL                                      0  $0      
          4        SEND_VAR                                                 $0
          5        DO_ICALL                                                 
   49     6        INIT_FCALL                                               'var_dump'
          7        INIT_FCALL                                               'fractiontodecimal'
          8        SEND_VAL                                                 '2+3%2F4'
          9        DO_FCALL                                      0  $2      
         10        SEND_VAR                                                 $2
         11        DO_ICALL                                                 
   50    12        INIT_FCALL                                               'var_dump'
         13        INIT_FCALL                                               'fractiontodecimal'
         14        SEND_VAL                                                 '6%2F4'
         15        DO_FCALL                                      0  $4      
         16        SEND_VAR                                                 $4
         17        DO_ICALL                                                 
   52    18        INIT_FCALL                                               'var_dump'
         19        INIT_FCALL                                               'decimaltofraction'
         20        SEND_VAL                                                 1.375
         21        DO_FCALL                                      0  $6      
         22        SEND_VAR                                                 $6
         23        DO_ICALL                                                 
   53    24        INIT_FCALL                                               'var_dump'
         25        INIT_FCALL                                               'decimaltofraction'
         26        SEND_VAL                                                 3
         27        DO_FCALL                                      0  $8      
         28        SEND_VAR                                                 $8
         29        DO_ICALL                                                 
   54    30        INIT_FCALL                                               'var_dump'
         31        INIT_FCALL                                               'decimaltofraction'
         32        SEND_VAL                                                 2.875
         33        DO_FCALL                                      0  $10     
         34        SEND_VAR                                                 $10
         35        DO_ICALL                                                 
         36      > RETURN                                                   1

Function fractiontodecimal:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 46) Position 1 = 20, Position 2 = 21
Branch analysis from position: 20
2 jumps found. (Code = 46) Position 1 = 22, Position 2 = 25
Branch analysis from position: 22
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 25
Branch analysis from position: 21
filename:       /in/ft0ZT
function name:  fractionToDecimal
number of ops:  27
compiled vars:  !0 = $fraction, !1 = $components, !2 = $whole, !3 = $numerator, !4 = $denominator, !5 = $decimal
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   RECV                                             !0      
    6     1        INIT_FCALL                                               'preg_match'
          2        SEND_VAL                                                 '%2F%5E%28%3FP%3Cwhole%3E%5Cd%2B%29%3F%5Cs%3F%28%28%3FP%3Cnumerator%3E%5Cd%2B%29%5C%2F%28%3FP%3Cdenominator%3E%5Cd%2B%29%29%3F%24%2F'
          3        SEND_VAR                                                 !0
          4        SEND_REF                                                 !1
          5        DO_ICALL                                                 
    9     6        FETCH_DIM_R                                      ~7      !1, 'whole'
          7        JMP_SET                                          ~8      ~7, ->9
          8        QM_ASSIGN                                        ~8      0
          9        ASSIGN                                                   !2, ~8
   10    10        FETCH_DIM_R                                      ~10     !1, 'numerator'
         11        JMP_SET                                          ~11     ~10, ->13
         12        QM_ASSIGN                                        ~11     0
         13        ASSIGN                                                   !3, ~11
   11    14        FETCH_DIM_R                                      ~13     !1, 'denominator'
         15        JMP_SET                                          ~14     ~13, ->17
         16        QM_ASSIGN                                        ~14     0
         17        ASSIGN                                                   !4, ~14
   14    18        ASSIGN                                                   !5, !2
   15    19      > JMPZ_EX                                          ~17     !3, ->21
         20    >   BOOL                                             ~17     !4
         21    > > JMPZ_EX                                          ~17     ~17, ->25
         22    >   DIV                                              ~18     !3, !4
         23        ASSIGN_OP                                     1  ~19     !5, ~18
         24        BOOL                                             ~17     ~19
   17    25    > > RETURN                                                   !5
   18    26*     > RETURN                                                   null

End of function fractiontodecimal

Function decimaltofraction:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 46) Position 1 = 39, Position 2 = 42
Branch analysis from position: 39
2 jumps found. (Code = 46) Position 1 = 43, Position 2 = 49
Branch analysis from position: 43
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 49
Branch analysis from position: 42
filename:       /in/ft0ZT
function name:  decimalToFraction
number of ops:  55
compiled vars:  !0 = $decimal, !1 = $precision, !2 = $multiplier, !3 = $numerator, !4 = $denominator, !5 = $whole, !6 = $factor, !7 = $fraction
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   20     0  E >   RECV                                             !0      
   23     1        INIT_FCALL                                               'strpos'
          2        INIT_FCALL                                               'strrev'
          3        SEND_VAR                                                 !0
          4        DO_ICALL                                         $8      
          5        SEND_VAR                                                 $8
          6        SEND_VAL                                                 '.'
          7        DO_ICALL                                         $9      
          8        JMP_SET                                          ~10     $9, ->10
          9        QM_ASSIGN                                        ~10     0
         10        ASSIGN                                                   !1, ~10
   24    11        INIT_FCALL                                               'pow'
         12        SEND_VAL                                                 10
         13        SEND_VAR                                                 !1
         14        DO_ICALL                                         $12     
         15        ASSIGN                                                   !2, $12
   27    16        MUL                                              ~14     !0, !2
         17        ASSIGN                                                   !3, ~14
   28    18        MUL                                              ~16     !2, 1
         19        ASSIGN                                                   !4, ~16
   31    20        INIT_FCALL                                               'floor'
         21        DIV                                              ~18     !3, !4
         22        SEND_VAL                                                 ~18
         23        DO_ICALL                                         $19     
         24        ASSIGN                                                   !5, $19
   32    25        MOD                                              ~21     !3, !4
         26        ASSIGN                                                   !3, ~21
   35    27        INIT_FCALL_BY_NAME                                       'gmp_intval'
         28        INIT_FCALL_BY_NAME                                       'gmp_gcd'
         29        SEND_VAR_EX                                              !3
         30        SEND_VAR_EX                                              !4
         31        DO_FCALL                                      0  $23     
         32        SEND_VAR_NO_REF_EX                                       $23
         33        DO_FCALL                                      0  $24     
         34        ASSIGN                                                   !6, $24
   36    35        ASSIGN_OP                                     4          !3, !6
   37    36        ASSIGN_OP                                     4          !4, !6
   40    37        ASSIGN                                                   !7, <array>
   41    38      > JMPZ_EX                                          ~29     !5, ->42
         39    >   ASSIGN_DIM                                       ~30     !7
         40        OP_DATA                                                  !5
         41        BOOL                                             ~29     ~30
   42    42    > > JMPZ_EX                                          ~31     !3, ->49
         43    >   ROPE_INIT                                     3  ~34     !3
         44        ROPE_ADD                                      1  ~34     ~34, '%2F'
         45        ROPE_END                                      2  ~33     ~34, !4
         46        ASSIGN_DIM                                       ~32     !7
         47        OP_DATA                                                  ~33
         48        BOOL                                             ~31     ~32
   44    49    >   INIT_FCALL                                               'implode'
         50        SEND_VAL                                                 '+'
         51        SEND_VAR                                                 !7
         52        DO_ICALL                                         $36     
         53      > RETURN                                                   $36
   45    54*     > RETURN                                                   null

End of function decimaltofraction

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
140.6 ms | 1016 KiB | 26 Q