Advertisement
jargon

Keal's "Phlegm" PHP Mathematics Class

Mar 22nd, 2024 (edited)
783
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.63 KB | Source Code | 0 0
  1. <?php
  2.  
  3. class phlegmClass
  4. {
  5.     public $bogus="bogus";
  6.    
  7.     public function __construct()
  8.     {
  9.         $this->bogus="bogus";
  10.     }
  11.  
  12.     public function math($mnemonic="",&$q=0,$p=0,$T=true,$F=false)
  13.     {
  14.         global $pretty, $stats;
  15.            
  16.         foreach(
  17.             [
  18.                 "inequalityGates" => false,
  19.                 "arithmeticOp" => false,
  20.                 "vennGates" => true
  21.             ] as $instructionSet => $truths
  22.         )
  23.         {
  24.             switch(true)
  25.             {
  26.             case $truths:
  27.                 $ret = $this->$instructionSet($mnemonic,$q,$p,$T,$F);
  28.             default:
  29.                 $ret = $this->$instructionSet($mnemonic,$q,$p);
  30.             }
  31.            
  32.             if($ret !== $this->bogus){return $ret;}
  33.         }
  34.        
  35.         return $this->bogus;
  36.     }
  37.                    
  38.     public function inequalityGates($mnemonic="",&$q=0,$p=0)
  39.     {
  40.         global $pretty, $stats;
  41.        
  42.         if(is_string($mnemonic))
  43.         {
  44.             $mnemonic=strtoupper($mnemonic);
  45.         }
  46.        
  47.         switch($mnemonic)
  48.         {
  49.         case "EQU":
  50.         case "==":
  51.         case "===":
  52.             return ($q==$p);
  53.            
  54.         case "NEQ":
  55.         case "<>":
  56.         case "!=":
  57.         case "!==":
  58.             return ($q!==$p);
  59.            
  60.         case "GTR":
  61.         case ">":
  62.             return ($q>$p);
  63.            
  64.         case "LSS":
  65.         case "<":
  66.             return ($q<$p);
  67.            
  68.         case "GEQ":
  69.         case ">=":
  70.             return ($q>=$p);
  71.            
  72.         case "LEQ":
  73.         case "<=":
  74.             return ($q<=$p);
  75.            
  76.         }
  77.        
  78.         return $this->bogus;
  79.     }
  80.  
  81.     public function arithmeticOp($mnemonic="",&$q=0,$p=0)
  82.     {
  83.         global $pretty, $stats;
  84.        
  85.         if(is_string($mnemonic))
  86.         {
  87.             $mnemonic=strtoupper($mnemonic);
  88.         }
  89.  
  90.         switch($mnemonic)
  91.         {
  92.         case "=":
  93.             $q=$p;
  94.             return $q;
  95.            
  96.         case "&=":
  97.         case ".=":
  98.             $q.=$p;
  99.             return $q;
  100.            
  101.         case "+=":
  102.             $q+=$p;
  103.             return $q;
  104.            
  105.         case "-=":
  106.             $q-=$p;
  107.             return $q;
  108.            
  109.         case "*=":
  110.             $q=$q*$p;
  111.             return $q;
  112.            
  113.         case "/=":
  114.             $q=$q/$p;
  115.             return $q;
  116.            
  117.         case "++":
  118.             $q++;
  119.             return $q;
  120.            
  121.         case "--":
  122.             $q--;
  123.             return $q;
  124.            
  125.         }
  126.        
  127.         return $this->bogus;
  128.     }
  129.  
  130.     // 64bit x 20input floating gate emulator
  131.     // 1280 total bits input, 64 total bits output
  132.     // copyright 2002 01/26, 2024 03/20 Timothy Robert Keal alias jargon
  133.    
  134.     public function Gate16
  135.     (
  136.    
  137.         $a = 0, $b = 0, $c = 0, $d = 0,
  138.    
  139.         $g0000 = 0,
  140.         $g0001 = 0,
  141.         $g0010 = 0,
  142.         $g0011 = 0,
  143.         $g0100 = 0,
  144.         $g0101 = 0,
  145.         $g0110 = 0,
  146.         $g0111 = 0,
  147.         $g1000 = 0,
  148.         $g1001 = 0,
  149.         $g1010 = 0,
  150.         $g1011 = 0,
  151.         $g1100 = 0,
  152.         $g1101 = 0,
  153.         $g1110 = 0,
  154.         $g1111 = 0
  155.     )
  156.    
  157.     {
  158.         $tmp = 0;
  159.        
  160.         $tmp = $tmp | ($g0000 & ~($a | $b | $c | $d));
  161.         $tmp = $tmp | ($g0001 & ~($this->IMP ( $d,($a | $b | $c))));
  162.         $tmp = $tmp | ($g0010 & ~($this->IMP ( $c, ($a | $b | $d))));
  163.         $tmp = $tmp | ($g0011 & ~($this->IMP ( ($c & $d), ($a | $b))));
  164.         $tmp = $tmp | ($g0100 & ~($this->IMP ( $b, ($a | $c | $d))));
  165.         $tmp = $tmp | ($g0101 & ~($this->IMP ( ($b & $d), ($a | $c))));
  166.         $tmp = $tmp | ($g0110 & ~($this->IMP ( ($b & $c), ($a | $d))));
  167.         $tmp = $tmp | ($g0111 & ~($this->IMP ( ($b & $c & $d), $a)));
  168.         $tmp = $tmp | ($g1000 & ~($this->IMP ( $a, ($b | $c | $d))));
  169.         $tmp = $tmp | ($g1001 & ~($this->IMP ( ($a & $d), ($b | $c))));
  170.         $tmp = $tmp | ($g1010 & ~($this->IMP ( ($a & $c), ($b | $d))));
  171.         $tmp = $tmp | ($g1011 & ~($this->IMP ( ($a & $c & $d), $b)));
  172.         $tmp = $tmp | ($g1100 & ~($this->IMP ( ($a & $b), ($c | $d))));
  173.         $tmp = $tmp | ($g1101 & ~($this->IMP ( ($a & $b & $d), $c)));
  174.         $tmp = $tmp | ($g1110 & ~($this->IMP ( ($a & $b & $c), $d)));
  175.         $tmp = $tmp | ($g1111 & ($a & $b & $c & $d));
  176.        
  177.         return $tmp;
  178.     }
  179.    
  180.     public function IMP ($q,$p)
  181.     {
  182.         return ~$q | $p;
  183.     }
  184.    
  185.     // 64bit x 6input floating gate emulator
  186.     // 384 total bits input, 64 total bits output
  187.     // copyright 2002 01/23, 2024 03/20 Timothy Robert Keal alias jargon
  188.    
  189.     public function Gate4
  190.     (  
  191.         $a = 0, $b = 0,
  192.        
  193.         $g00 = 0,
  194.         $g01 = 0,
  195.         $g10 = 0,
  196.         $g11 = 0
  197.     )
  198.     {
  199.         $a=intval($a);
  200.         $b=intval($b);
  201.        
  202.         $tmp = 0;
  203.        
  204.         $tmp = $tmp | ($g00 & ~($a | $b));
  205.         $tmp = $tmp | ($g01 & ~($this->IMP ( $b, $a)));
  206.         $tmp = $tmp | ($g10 & ~($this->IMP ( $a, $b)));
  207.         $tmp = $tmp | ($g11 & ($a & $b));
  208.        
  209.         return $tmp;
  210.     }
  211.  
  212.     public function vennGates
  213.     (
  214.         $gate = true,
  215.         $q = 0,
  216.         $p = 0,
  217.         $T = true,
  218.         $F = false
  219.     )
  220.     {
  221.        
  222.        
  223.         if(!is_bool($gate))
  224.         {
  225.             if(!is_numeric($gate))
  226.             {
  227.                 if(is_string($gate))
  228.                 {
  229.                     $gate=strtolower($gate);
  230.                     if(explode(":",$gate)[0] == "bitwise")
  231.                     {
  232.                         $float=explode(":",$gate);
  233.                         $gate=str_replace("_"," ",$float[0]);
  234.                         unset($float[0]);
  235.                         $float=explode(",",implode(",",$float));
  236.                         $float=array_map('intval', $float);
  237.                     }
  238.                 }
  239.             }else{
  240.                 $gate=intval($gate);
  241.  
  242.             }
  243.         }else{
  244.             $gate=boolval($gate);
  245.         }
  246.        
  247.         switch($gate)
  248.         {
  249.        
  250.         case 0:
  251.         case false:
  252.         case "false":
  253.         case "f":      
  254.             return $this->Gate4($q,$p,$F,$F,$F,$F);
  255.  
  256.         case 1:
  257.         case "q nor p":
  258.             return $this->Gate4($q,$p,$T,$F,$F,$F);
  259.  
  260.         case 2:
  261.         case "p nimp q":
  262.         case "p not implying q":
  263.             return $this->Gate4($q,$p,$F,$T,$F,$F);
  264.  
  265.         case 3:
  266.         case "not q":
  267.             return $this->Gate4($q,$p,$T,$T,$F,$F);
  268.  
  269.         case 4:
  270.         case "q nimp p":
  271.         case "q not implying p":
  272.             return $this->Gate4($q,$p,$F,$F,$T,$F);
  273.  
  274.         case 5:
  275.         case "not p":
  276.             return $this->Gate4($q,$p,$T,$F,$T,$F);
  277.  
  278.         case 6:
  279.         case "q neqv p":
  280.         case "q xor p":
  281.         case "q not equivalent p":
  282.             return $this->Gate4($q,$p,$F,$T,$T,$F);
  283.  
  284.         case 7:
  285.         case "q nand p":
  286.             return $this->Gate4($q,$p,$T,$T,$T,$F);
  287.  
  288.         case 8:
  289.         case "q and p":
  290.             return $this->Gate4($q,$p,$F,$F,$F,$T);
  291.  
  292.         case 9:
  293.         case "q eqv p":
  294.         case "q xnor p":
  295.         case "q equivalent p":
  296.             return $this->Gate4($q,$p,$T,$F,$F,$T);
  297.  
  298.         case 10:
  299.         case "p":
  300.             return $this->Gate4($q,$p,$F,$T,$F,$T);
  301.  
  302.         case 11:
  303.         case "q imp p":
  304.         case "q implying p":
  305.             return $this->Gate4($q,$p,$T,$T,$F,$T);
  306.  
  307.         case 12:
  308.         case "p":
  309.             return $this->Gate4($q,$p,$F,$F,$T,$T);
  310.  
  311.         case 13:
  312.         case "p imp q":
  313.         case "p implying q":
  314.             return $this->Gate4($q,$p,$T,$F,$T,$T);
  315.  
  316.         case 14:
  317.         case "q or p":
  318.             return $this->Gate4($q,$p,$F,$T,$T,$T);
  319.  
  320.         case 15:
  321.         case true:
  322.         case "truth":
  323.         case "true":
  324.         case "t":
  325.             return $this->Gate4($q,$p,$T,$T,$T,$T);
  326.  
  327.         case "bitwise":
  328.             return $this->Gate4($q,$p,$float[0],$float[1],$float[2],$float[3]);
  329.            
  330.         }
  331.        
  332.         return $this->bogus;
  333.     }
  334.  
  335.     public function diceRoll($mnemonic,&$q,&$p)
  336.     {
  337.         global $pretty, $stats;
  338.        
  339.         $tally=1;
  340.        
  341.         $Pattern="/([1-9][0-9]*[0-9]*|)d([1-9][0-9]*[0-9]*)/mi";
  342.        
  343.         if(preg_match_all($Pattern,$mnemonic,$matches,PREG_SET_ORDER)==0){return $this->bogus;}
  344.  
  345.         $exD=explode("d",$mnemonic);
  346.        
  347.         if(count($exD)<2)
  348.         {
  349.             return $this->bogus;
  350.         }
  351.        
  352.         if($exD[0]==""){$exD[0]=1;}
  353.        
  354.         $q=$exD[0];
  355.         $p=$exD[1];
  356.        
  357.         $tally=0;
  358.         for($multi=1;$multi<=$exD[0];$multi++)
  359.         {
  360.             $tally+=rand(1,$exD[1]);
  361.         }
  362.        
  363.         return $tally;
  364.        
  365.     }
  366. }
  367.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement