Math_Integer
[ class tree: Math_Integer ] [ index: Math_Integer ] [ all elements ]

Source for file bcmath.php

Documentation is available at bcmath.php

  1. <?php
  2.  
  3. include_once 'common.php';
  4. //include_once 'Math/Integer/common.php';
  5. bcscale(0);
  6.  
  7. class Math_Integer_BCMATH extends Math_Integer_Common {/*{{{*/
  8.     
  9.     function Math_Integer_BCMATH($value{/*{{{*/
  10.         $this->setValue($value);
  11.     }/*}}}*/
  12.  
  13.     function makeClone({/*{{{*/
  14.         return new Math_Integer_BCMATH($this->toString());
  15.     }/*}}}*/
  16.  
  17.     function setValue($value{/*{{{*/
  18.         if ($this->_is($value'Math_Integer_BCMATH')) {
  19.             $this->setValue($value->toString());
  20.         elseif (is_scalar($value)) {
  21.             $this->_value $value;
  22.         else {
  23.             $this->_value = null;
  24.         }
  25.     }/*}}}*/
  26.  
  27.     function negate({/*{{{*/
  28.         $newval = bcmul($this->getValue()-1);
  29.         $this->setValue($newval);
  30.         return true;
  31.     }/*}}}*/
  32.  
  33.     function abs({/*{{{*/
  34.         if ($this->isNegative()) {
  35.             return $this->negate();
  36.         }
  37.         return true;
  38.     }/*}}}*/
  39.  
  40.     function fact({/*{{{*/
  41.         if ($this->isNegative()) {
  42.             return PEAR::raiseError('Factorial of a negative number is undefined');
  43.         }
  44.         if ($this->isZero()) {
  45.             $this->setValue(1);
  46.             return true;
  47.         else {
  48.             $fact = 1;
  49.             $val $this->getValue();
  50.             while (bccomp($val1!= 0{
  51.                 $fact = bcmul($fact$val);
  52.                 $val = bcsub($val1);
  53.             }
  54.             $this->setValue($fact);
  55.             return true;
  56.         }
  57.     }/*}}}*/
  58.  
  59.     function add(&$int{/*{{{*/
  60.         if (!$this->_is(&$int'Math_Integer_BCMATH')) {
  61.             return PEAR::raiseError('Paramater is not a Math_Integer_BCMATH object');
  62.         }
  63.         $newval = bcadd($this->getValue()$int->getValue());
  64.         $this->setValue($newval);
  65.         return true;
  66.     }/*}}}*/
  67.  
  68.     function inc({/*{{{*/
  69.         $newval = bcadd($this->getValue()1);
  70.         $this->setValue($newval);
  71.         return true;
  72.     }/*}}}*/
  73.  
  74.     function sub(&$int{/*{{{*/
  75.         if (!$this->_is(&$int'Math_Integer_BCMATH')) {
  76.             return PEAR::raiseError('Paramater is not a Math_Integer_BCMATH object');
  77.         }
  78.         $newval = bcsub($this->getValue()$int->getValue());
  79.         $this->setValue($newval);
  80.         return true;
  81.     }/*}}}*/
  82.  
  83.     function dec({/*{{{*/
  84.         $newval = bcsub($this->getValue()1);
  85.         $this->setValue($newval);
  86.         return true;
  87.     }/*}}}*/
  88.  
  89.     function mul(&$int{/*{{{*/
  90.         if (!$this->_is(&$int'Math_Integer_BCMATH')) {
  91.             return PEAR::raiseError('Paramater is not a Math_Integer_BCMATH object');
  92.         }
  93.         $newval = bcmul($this->getValue()$int->getValue());
  94.         $this->setValue($newval);
  95.         return true;
  96.     }/*}}}*/
  97.  
  98.     function div(&$int{/*{{{*/
  99.         if (!$this->_is(&$int'Math_Integer_BCMATH')) {
  100.             return PEAR::raiseError('Paramater is not a Math_Integer_BCMATH object');
  101.         }
  102.         if ($int->isZero()) {
  103.             return PEAR::raiseError('Division by zero is undefined');
  104.         }
  105.         $newval = bcdiv($this->getValue()$int->getValue());
  106.         $this->setValue($newval);
  107.         return true;
  108.     }/*}}}*/
  109.  
  110.     function pow(&$int{/*{{{*/
  111.         if (!$this->_is(&$int'Math_Integer_BCMATH')) {
  112.             return PEAR::raiseError('Exponent is not a Math_Integer_BCMATH object');
  113.         }
  114.         $newval = bcpow($this->getValue()$int->getValue());
  115.         $this->setValue($newval);
  116.         return true;
  117.     }/*}}}*/
  118.  
  119.     function powmod(&$int&$mod{/*{{{*/
  120.         $err '';
  121.         if (!$this->_is(&$int'Math_Integer_BCMATH')) {
  122.             $err .= 'Exponent is not a Math_Integer_BCMATH object.';
  123.         }
  124.         if (!empty($err)) {
  125.             $err .= ' ';
  126.         }
  127.         if (!$this->_is(&$mod'Math_Integer_BCMATH')) {
  128.             $err .= 'Modulus is not a Math_Integer_BCMATH object.';
  129.         else {
  130.             if ($mod->isZero(|| $mod->isNegative()) {
  131.                 $err .= 'Modulus object must be positive.';
  132.             }
  133.         }
  134.         if (!empty($err)) {
  135.             return PEAR::raiseError($err);
  136.         }
  137.         $newval = bcpowmod($this->getValue()$int->getValue()$mod->getValue());
  138.         $this->setValue($newval);
  139.         return true;
  140.     }/*}}}*/
  141.  
  142.     function sqrt({/*{{{*/
  143.         if ($this->isZero()) {
  144.             return true;
  145.         elseif ($this->isNegative()) {
  146.             return PEAR::raiseError('Cannot take square root of a negative number');
  147.         else {
  148.             $newval = bcsqrt($this->getValue()$int->getValue()$mod->getValue());
  149.             $this->setValue($newval);
  150.             return true;
  151.         }
  152.     }/*}}}*/
  153.  
  154.     function mod(&$int{/*{{{*/
  155.         if (!$this->_is(&$int'Math_Integer_BCMATH')) {
  156.             $err 'Modulus is not a Math_Integer_BCMATH object.';
  157.         else {
  158.             if ($int->isZero(|| $int->isNegative()) {
  159.                 $err 'Modulus object must be positive.';
  160.             }
  161.         }
  162.         if (!empty($err)) {
  163.             return PEAR::raiseError($err);
  164.         }
  165.         $newval = bcmod($this->getValue()$int->getValue());
  166.         $this->setValue($newval);
  167.         return true;
  168.     }/*}}}*/
  169.  
  170.     function compare(&$int{/*{{{*/
  171.         if (!$this->_is(&$int'Math_Integer_BCMATH')) {
  172.             return PEAR::raiseError('Paramater is not a Math_Integer_BCMATH object');
  173.         }
  174.         return bccomp($this->getValue()$int->getValue());
  175.     }/*}}}*/
  176.  
  177.     function sign({/*{{{*/
  178.         if ($this->isNegative()) {
  179.             return -1;
  180.         elseif ($this->isZero()) {
  181.             return 0;
  182.         else {
  183.             return 1;
  184.         }
  185.     }/*}}}*/
  186.  
  187.     function &gcd(&$int{/*{{{*/
  188.         if (!$this->_is(&$int'Math_Integer_BCMATH')) {
  189.             $err 'Modulus is not a Math_Integer_BCMATH object.';
  190.         }
  191.         // if both are the same, return either
  192.         if ($this->compare($int== 0{
  193.             return new Math_Integer_GMP($int);
  194.         }
  195.         $int1 $this->makeClone();
  196.         $int2 $int->makeClone();
  197.         // make sure both are positive
  198.         if ($int1->isNegative()) {
  199.             $int1->negate();
  200.         }
  201.         if ($int2->isNegative()) {
  202.             $int2->negate();
  203.         }
  204.         if ($int1->compare($int2== -1{
  205.             $tmp $int1;
  206.             $int1 $int2;
  207.             $int2 $tmp;
  208.         }
  209.         $mod $int1->mod($int2);
  210.         if (PEAR::isError($mod)) {
  211.             return $mod;
  212.         elseif (!$int1->isZero()) {
  213.             return $int2->gcd($int1);
  214.         else {
  215.             return $int2;
  216.         }
  217.     }/*}}}*/
  218.  
  219.     function isOdd({/*{{{*/
  220.         return bcmod($this->getValue()2!= 0;
  221.     }/*}}}*/
  222.  
  223.     function isEven({/*{{{*/
  224.         return bcmod($this->getValue()2== 0;
  225.     }/*}}}*/
  226.  
  227.     function isPositive({/*{{{*/
  228.         return bccomp($this->getValue()0== 1;
  229.     }/*}}}*/
  230.  
  231.     function isNegative({/*{{{*/
  232.         return bccomp($this->getValue()0== -1;
  233.     }/*}}}*/
  234.  
  235.     function isZero({/*{{{*/
  236.         return bccomp($this->getValue()0== 0;
  237.     }/*}}}*/
  238.  
  239. }/*}}}*/
  240.  
  241. ?>

Documentation generated on Mon, 11 Mar 2019 15:39:15 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.