cvs: pear /Math_Integer/Integer bcmath.php common.php gmp.php

From: Date: Thu, 09 Jan 2003 09:01:30 +0000
Subject: cvs: pear /Math_Integer/Integer bcmath.php common.php gmp.php
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-8140@lists.php.net to get a copy of this message
jmcastagnetto Thu Jan 9 04:01:30 2003 EDT Modified files: /pear/Math_Integer/Integer bcmath.php common.php gmp.php Log: added fact() method for GMP and BCMATH, added probPrime() to GMP misc edits and typo fixes: tabs -> spaces, added folding markers, etc.

Index: pear/Math_Integer/Integer/bcmath.php diff -u pear/Math_Integer/Integer/bcmath.php:1.3 pear/Math_Integer/Integer/bcmath.php:1.4 --- pear/Math_Integer/Integer/bcmath.php:1.3 Wed Jan 8 13:20:52 2003 +++ pear/Math_Integer/Integer/bcmath.php Thu Jan 9 04:01:22 2003 @@ -4,219 +4,238 @@ //include_once 'Math/Integer/common.php'; bcscale(0); -class Math_Integer_BCMATH extends Math_Integer_Common { - - function Math_Integer_BCMATH($value) { - $this->setValue($value); - } - - function &clone() { - return new Math_Integer_BCMATH($this->toString()); - } - - function setValue($value) { - if ($this->_is($value, 'Math_Integer_BCMATH')) { - $this->setValue($value->toString()); - } elseif (is_scalar($value)) { - $this->_value = $value; - } else { - $this->_value = null; - } - } - - function negate() { - $newval = bcmul($this->getValue(), -1); - $this->setValue($newval); - return true; - } - - function abs() { - if ($this->isNegative()) { - return $this->negate(); - } - return true; - } - - function add(&$int) { - if (!$this->_is(&$int, 'Math_Integer_BCMATH')) { - return PEAR::raiseError('Paramater is not a Math_Integer_BCMATH object'); - } - $newval = bcadd($this->getValue(), $int->getValue()); - $this->setValue($newval); - return true; - } - - function inc() { - $newval = bcadd($this->getValue(), 1); - $this->setValue($newval); - return true; - } - - function sub(&$int) { - if (!$this->_is(&$int, 'Math_Integer_BCMATH')) { - return PEAR::raiseError('Paramater is not a Math_Integer_BCMATH object'); - } - $newval = bcsub($this->getValue(), $int->getValue()); - $this->setValue($newval); - return true; - } - - function dec() { - $newval = bcsub($this->getValue(), 1); - $this->setValue($newval); - return true; - } - - function mul(&$int) { - if (!$this->_is(&$int, 'Math_Integer_BCMATH')) { - return PEAR::raiseError('Paramater is not a Math_Integer_BCMATH object'); - } - $newval = bcmul($this->getValue(), $int->getValue()); - $this->setValue($newval); - return true; - } - - function div(&$int) { - if (!$this->_is(&$int, 'Math_Integer_BCMATH')) { - return PEAR::raiseError('Paramater is not a Math_Integer_BCMATH object'); - } - if ($int->isZero()) { - return PEAR::raiseError('Division by zero is undefined'); - } - $newval = bcdiv($this->getValue(), $int->getValue()); - $this->setValue($newval); - return true; - } - - function pow(&$int) { - if (!$this->_is(&$int, 'Math_Integer_BCMATH')) { - return PEAR::raiseError('Exponent is not a Math_Integer_BCMATH object'); - } - $newval = bcpow($this->getValue(), $int->getValue()); - $this->setValue($newval); - return true; - } - - function powmod(&$int, &$mod) { - $err = ''; - if (!$this->_is(&$int, 'Math_Integer_BCMATH')) { - $err .= 'Exponent is not a Math_Integer_BCMATH object.'; - } - if (!empty($err)) { - $err .= ' '; - } - if (!$this->_is(&$mod, 'Math_Integer_BCMATH')) { - $err .= 'Modulus is not a Math_Integer_BCMATH object.'; - } else { - if ($mod->isZero() || $mod->isNegative()) { - $err .= 'Modulus object must be positive.'; - } - } - if (!empty($err)) { - return PEAR::raiseError($err); - } - $newval = bcpowmod($this->getValue(), $int->getValue(), $mod->getValue()); - $this->setValue($newval); - return true; - } - - function sqrt() { - if ($this->isZero()) { - return true; - } elseif ($this->isNegative()) { - return PEAR::raiseError('Cannot take square root of a negative number'); - } else { - $newval = bcsqrt($this->getValue(), $int->getValue(), $mod->getValue()); - $this->setValue($newval); - return true; - } - } - - function mod(&$int) { - if (!$this->_is(&$int, 'Math_Integer_BCMATH')) { - $err = 'Modulus is not a Math_Integer_BCMATH object.'; - } else { - if ($int->isZero() || $int->isNegative()) { - $err = 'Modulus object must be positive.'; - } - } - if (!empty($err)) { - return PEAR::raiseError($err); - } - $newval = bcmod($this->getValue(), $int->getValue()); - $this->setValue($newval); - return true; - } - - function compare(&$int) { - if (!$this->_is(&$int, 'Math_Integer_BCMATH')) { - return PEAR::raiseError('Paramater is not a Math_Integer_BCMATH object'); - } - return bccomp($this->getValue(), $int->getValue()); - } - - function sign() { - if ($this->isNegative()) { - return -1; - } elseif ($this->isZero()) { - return 0; - } else { - return 1; - } - } - - function &gcd(&$int) { - if (!$this->_is(&$int, 'Math_Integer_BCMATH')) { - $err = 'Modulus is not a Math_Integer_BCMATH object.'; - } - // if both are the same, return either - if ($this->compare($int) == 0) { - return new Math_Integer_GMP($int); - } - $int1 = $this->clone(); - $int2 = $int->clone(); - // make sure both are positive - if ($int1->isNegative()) { - $int1->negate(); - } - if ($int2->isNegative()) { - $int2->negate(); - } - if ($int1->compare($int2) == -1) { - $tmp = $int1; - $int1 = $int2; - $int2 = $tmp; - } - $mod = $int1->mod($int2); - if (PEAR::isError($mod)) { - return $mod; - } elseif (!$int1->isZero()) { - return $int2->gcd($int1); - } else { - return $int2; - } - } - - function isOdd() { - return bcmod($this->getValue(), 2) != 0; - } - - function isEven() { - return bcmod($this->getValue(), 2) == 0; - } - - function isPositive() { - return bccomp($this->getValue(), 0) == 1; - } - - function isNegative() { - return bccomp($this->getValue(), 0) == -1; - } - - function isZero() { - return bccomp($this->getValue(), 0) == 0; - } +class Math_Integer_BCMATH extends Math_Integer_Common {/*{{{*/ + + function Math_Integer_BCMATH($value) {/*{{{*/ + $this->setValue($value); + }/*}}}*/ + + function &clone() {/*{{{*/ + return new Math_Integer_BCMATH($this->toString()); + }/*}}}*/ + + function setValue($value) {/*{{{*/ + if ($this->_is($value, 'Math_Integer_BCMATH')) { + $this->setValue($value->toString()); + } elseif (is_scalar($value)) { + $this->_value = $value; + } else { + $this->_value = null; + } + }/*}}}*/ + + function negate() {/*{{{*/ + $newval = bcmul($this->getValue(), -1); + $this->setValue($newval); + return true; + }/*}}}*/ + + function abs() {/*{{{*/ + if ($this->isNegative()) { + return $this->negate(); + } + return true; + }/*}}}*/ + + function fact() {/*{{{*/ + if ($this->isNegative()) { + return PEAR::isError('Factorial of a negative number is undefined'); + } + if ($this->isZero()) { + $this->setValue(1); + return true; + } else { + $fact = 1; + $val = $this->getValue(); + while (bccomp($val, 1) != 0) { + $fact = bcmul($fac, $val); + $val = bcsub($val, 1); + } + $this->setValue($fact); + return true; + } + }/*}}}*/ + + function add(&$int) {/*{{{*/ + if (!$this->_is(&$int, 'Math_Integer_BCMATH')) { + return PEAR::raiseError('Paramater is not a Math_Integer_BCMATH object'); + } + $newval = bcadd($this->getValue(), $int->getValue()); + $this->setValue($newval); + return true; + }/*}}}*/ + + function inc() {/*{{{*/ + $newval = bcadd($this->getValue(), 1); + $this->setValue($newval); + return true; + }/*}}}*/ + + function sub(&$int) {/*{{{*/ + if (!$this->_is(&$int, 'Math_Integer_BCMATH')) { + return PEAR::raiseError('Paramater is not a Math_Integer_BCMATH object'); + } + $newval = bcsub($this->getValue(), $int->getValue()); + $this->setValue($newval); + return true; + }/*}}}*/ + + function dec() {/*{{{*/ + $newval = bcsub($this->getValue(), 1); + $this->setValue($newval); + return true; + }/*}}}*/ + + function mul(&$int) {/*{{{*/ + if (!$this->_is(&$int, 'Math_Integer_BCMATH')) { + return PEAR::raiseError('Paramater is not a Math_Integer_BCMATH object'); + } + $newval = bcmul($this->getValue(), $int->getValue()); + $this->setValue($newval); + return true; + }/*}}}*/ + + function div(&$int) {/*{{{*/ + if (!$this->_is(&$int, 'Math_Integer_BCMATH')) { + return PEAR::raiseError('Paramater is not a Math_Integer_BCMATH object'); + } + if ($int->isZero()) { + return PEAR::raiseError('Division by zero is undefined'); + } + $newval = bcdiv($this->getValue(), $int->getValue()); + $this->setValue($newval); + return true; + }/*}}}*/ + + function pow(&$int) {/*{{{*/ + if (!$this->_is(&$int, 'Math_Integer_BCMATH')) { + return PEAR::raiseError('Exponent is not a Math_Integer_BCMATH object'); + } + $newval = bcpow($this->getValue(), $int->getValue()); + $this->setValue($newval); + return true; + }/*}}}*/ + + function powmod(&$int, &$mod) {/*{{{*/ + $err = ''; + if (!$this->_is(&$int, 'Math_Integer_BCMATH')) { + $err .= 'Exponent is not a Math_Integer_BCMATH object.'; + } + if (!empty($err)) { + $err .= ' '; + } + if (!$this->_is(&$mod, 'Math_Integer_BCMATH')) { + $err .= 'Modulus is not a Math_Integer_BCMATH object.'; + } else { + if ($mod->isZero() || $mod->isNegative()) { + $err .= 'Modulus object must be positive.'; + } + } + if (!empty($err)) { + return PEAR::raiseError($err); + } + $newval = bcpowmod($this->getValue(), $int->getValue(), $mod->getValue()); + $this->setValue($newval); + return true; + }/*}}}*/ + + function sqrt() {/*{{{*/ + if ($this->isZero()) { + return true; + } elseif ($this->isNegative()) { + return PEAR::raiseError('Cannot take square root of a negative number'); + } else { + $newval = bcsqrt($this->getValue(), $int->getValue(), $mod->getValue()); + $this->setValue($newval); + return true; + } + }/*}}}*/ + + function mod(&$int) {/*{{{*/ + if (!$this->_is(&$int, 'Math_Integer_BCMATH')) { + $err = 'Modulus is not a Math_Integer_BCMATH object.'; + } else { + if ($int->isZero() || $int->isNegative()) { + $err = 'Modulus object must be positive.'; + } + } + if (!empty($err)) { + return PEAR::raiseError($err); + } + $newval = bcmod($this->getValue(), $int->getValue()); + $this->setValue($newval); + return true; + }/*}}}*/ + + function compare(&$int) {/*{{{*/ + if (!$this->_is(&$int, 'Math_Integer_BCMATH')) { + return PEAR::raiseError('Paramater is not a Math_Integer_BCMATH object'); + } + return bccomp($this->getValue(), $int->getValue()); + }/*}}}*/ + + function sign() {/*{{{*/ + if ($this->isNegative()) { + return -1; + } elseif ($this->isZero()) { + return 0; + } else { + return 1; + } + }/*}}}*/ + + function &gcd(&$int) {/*{{{*/ + if (!$this->_is(&$int, 'Math_Integer_BCMATH')) { + $err = 'Modulus is not a Math_Integer_BCMATH object.'; + } + // if both are the same, return either + if ($this->compare($int) == 0) { + return new Math_Integer_GMP($int); + } + $int1 = $this->clone(); + $int2 = $int->clone(); + // make sure both are positive + if ($int1->isNegative()) { + $int1->negate(); + } + if ($int2->isNegative()) { + $int2->negate(); + } + if ($int1->compare($int2) == -1) { + $tmp = $int1; + $int1 = $int2; + $int2 = $tmp; + } + $mod = $int1->mod($int2); + if (PEAR::isError($mod)) { + return $mod; + } elseif (!$int1->isZero()) { + return $int2->gcd($int1); + } else { + return $int2; + } + }/*}}}*/ + + function isOdd() {/*{{{*/ + return bcmod($this->getValue(), 2) != 0; + }/*}}}*/ + + function isEven() {/*{{{*/ + return bcmod($this->getValue(), 2) == 0; + }/*}}}*/ + + function isPositive() {/*{{{*/ + return bccomp($this->getValue(), 0) == 1; + }/*}}}*/ + + function isNegative() {/*{{{*/ + return bccomp($this->getValue(), 0) == -1; + }/*}}}*/ + + function isZero() {/*{{{*/ + return bccomp($this->getValue(), 0) == 0; + }/*}}}*/ -} +}/*}}}*/ ?> Index: pear/Math_Integer/Integer/common.php diff -u pear/Math_Integer/Integer/common.php:1.4 pear/Math_Integer/Integer/common.php:1.5 --- pear/Math_Integer/Integer/common.php:1.4 Wed Jan 8 13:20:52 2003 +++ pear/Math_Integer/Integer/common.php Thu Jan 9 04:01:26 2003 @@ -2,124 +2,132 @@ include_once 'PEAR.php'; -class Math_Integer_Common { +class Math_Integer_Common {/*{{{*/ - var $_value; - - function Math_Integer_Common($value) { - $this->setValue($value); - } - - function toString() { - return strval($this->_value); - } - - function clone() { - return new Math_Integer_Common($this->toString()); - } - - function setValue($value) { - $this->_value = $value; - } - - function getValue() { - return $this->_value; - } - - function negate() { - return $this->_noImplemented('negate'); - } - - function abs() { - return $this->_noImplemented('abs'); - } - - function add(&$int) { - return $this->_noImplemented('add'); - } - - function inc() { - return $this->_noImplemented('inc'); - } - - function sub(&$int) { - return $this->_noImplemented('sub'); - } - - function dec() { - return $this->_noImplemented('dec'); - } - - function mul(&$int) { - return $this->_noImplemented('mul'); - } - - function div(&$int) { - return $this->_noImplemented('div'); - } - - function pow(&$int) { - return $this->_noImplemented('pow'); - } - - function powmod(&$int) { - return $this->_noImplemented('pow'); - } - - function sqrt() { - return $this->_noImplemented('sqrt'); - } - - function mod(&$int) { - return $this->_noImplemented('mod'); - } - - function compare(&$int) { - return $this->_noImplemented('compare'); - } - - function sign(&$int) { - return $this->_noImplemented('sign'); - } - - function &gcd(&$int) { - return $this->_noImplemented('gcd'); - } - - function isOdd() { - return $this->_noImplemented('isOdd'); - } - - function isEven() { - return $this->_noImplemented('isEven'); - } - - function isPositive() { - return $this->_noImplemented('isPositive'); - } - - function isNegative() { - return $this->_noImplemented('isNegative'); - } - - function isZero() { - return $this->_noImplemented('isZero'); - } - - function _is(&$obj, $classname) { - if (!is_object($obj)) { - return false; - } - if (function_exists('is_a')) { - return is_a($obj, $classname); - } else { - return get_class($obj) == strtolower($classname); - } - } - - function _notImplemented($func) { - return PEAR::raiseError("Method $func not implemented"); - } -} + var $_value; + + function Math_Integer_Common($value) {/*{{{*/ + $this->setValue($value); + }/*}}}*/ + + function toString() {/*{{{*/ + return strval($this->_value); + }/*}}}*/ + + function clone() {/*{{{*/ + return new Math_Integer_Common($this->toString()); + }/*}}}*/ + + function setValue($value) {/*{{{*/ + $this->_value = $value; + }/*}}}*/ + + function getValue() {/*{{{*/ + return $this->_value; + }/*}}}*/ + + function negate() {/*{{{*/ + return $this->_noImplemented('negate'); + }/*}}}*/ + + function abs() {/*{{{*/ + return $this->_noImplemented('abs'); + }/*}}}*/ + + function fact() {/*{{{*/ + return $this->_noImplemented('fact'); + }/*}}}*/ + + function add(&$int) {/*{{{*/ + return $this->_noImplemented('add'); + }/*}}}*/ + + function inc() {/*{{{*/ + return $this->_noImplemented('inc'); + }/*}}}*/ + + function sub(&$int) {/*{{{*/ + return $this->_noImplemented('sub'); + }/*}}}*/ + + function dec() {/*{{{*/ + return $this->_noImplemented('dec'); + }/*}}}*/ + + function mul(&$int) {/*{{{*/ + return $this->_noImplemented('mul'); + }/*}}}*/ + + function div(&$int) {/*{{{*/ + return $this->_noImplemented('div'); + }/*}}}*/ + + function pow(&$int) {/*{{{*/ + return $this->_noImplemented('pow'); + }/*}}}*/ + + function powmod(&$int) {/*{{{*/ + return $this->_noImplemented('pow'); + }/*}}}*/ + + function sqrt() {/*{{{*/ + return $this->_noImplemented('sqrt'); + }/*}}}*/ + + function mod(&$int) {/*{{{*/ + return $this->_noImplemented('mod'); + }/*}}}*/ + + function compare(&$int) {/*{{{*/ + return $this->_noImplemented('compare'); + }/*}}}*/ + + function sign(&$int) {/*{{{*/ + return $this->_noImplemented('sign'); + }/*}}}*/ + + function &gcd(&$int) {/*{{{*/ + return $this->_noImplemented('gcd'); + }/*}}}*/ + + function isOdd() {/*{{{*/ + return $this->_noImplemented('isOdd'); + }/*}}}*/ + + function isEven() {/*{{{*/ + return $this->_noImplemented('isEven'); + }/*}}}*/ + + function isPositive() {/*{{{*/ + return $this->_noImplemented('isPositive'); + }/*}}}*/ + + function isNegative() {/*{{{*/ + return $this->_noImplemented('isNegative'); + }/*}}}*/ + + function isZero() {/*{{{*/ + return $this->_noImplemented('isZero'); + }/*}}}*/ + + function probPrime($reps) {/*{{{*/ + return $this->_noImplemented('probPrime'); + }/*}}}*/ + + function _is(&$obj, $classname) {/*{{{*/ + if (!is_object($obj)) { + return false; + } + if (function_exists('is_a')) { + return is_a($obj, $classname); + } else { + return get_class($obj) == strtolower($classname); + } + }/*}}}*/ + + function _notImplemented($func) {/*{{{*/ + return PEAR::raiseError("Method $func not implemented"); + }/*}}}*/ +}/*}}}*/ ?> Index: pear/Math_Integer/Integer/gmp.php diff -u pear/Math_Integer/Integer/gmp.php:1.4 pear/Math_Integer/Integer/gmp.php:1.5 --- pear/Math_Integer/Integer/gmp.php:1.4 Wed Jan 8 13:20:52 2003 +++ pear/Math_Integer/Integer/gmp.php Thu Jan 9 04:01:26 2003 @@ -3,278 +3,299 @@ //include_once 'Math/Integer/common.php'; include_once 'common.php'; -class Math_Integer_GMP extends Math_Integer_Common { - - function Math_Integer_GMP($value) { - $this->setValue($value); - } - - function toString() { - return gmp_strval($this->_value); - } - - function &clone() { - return new Math_Integer_GMP($this->toString()); - } - - function setValue($value) { - if ($this->_is($value, 'Math_Integer_GMP')) { - $this->setValue($value->toString()); - } elseif (is_resource($value) && get_resource_type($value) == 'GMP integer') { - $this->_value = $value; - } elseif (is_scalar($value)) { - $this->_value = gmp_init($value); - } else { - $this->_value = null; - } - } - - function getValue() { - return $this->_value; - } - - function negate() { - $newval = gmp_neg($this->getValue()); - if (is_resource($newval)) { - $this->setValue($newval); - return true; - } else { - return PEAR::raiseError('Error while negating Math_Integer_GMP '. - 'object: '.$this->toString()); - } - } - - function abs() { - $newval = gmp_abs($this->getValue()); - if (is_resource($newval)) { - $this->setValue($newval); - return true; - } else { - return PEAR::raiseError('Error while negating Math_Integer_GMP '. - 'object: '.$this->toString()); - } - } - - function add(&$int) { - if (!$this->_is(&$int, 'Math_Integer_GMP')) { - return PEAR::raiseError('Parameter is not a Math_Integer_GMP object'); - } - $newval = gmp_add($this->getValue(), $int->getValue()); - if (is_resource($newval)) { - $this->setValue($newval); - return true; - } else { - return PEAR::raiseError('Error while adding Math_Integer_GMP '. - 'objects: '.$this->toString().' and '. - $int->toString()); - } - } - - function inc() { - $newval = gmp_add($this->getValue(), gmp_init(1)); - if (is_resource($newval)) { - $this->setValue($newval); - return true; - } else { - return PEAR::raiseError('Error while incrementing Math_Integer_GMP '. - 'object: '.$this->toString()); - } - } - - function sub(&$int) { - if (!$this->_is(&$int, 'Math_Integer_GMP')) { - return PEAR::raiseError('Parameter is not a Math_Integer_GMP object'); - } - $newval = gmp_sub($this->getValue(), $int->getValue()); - if (is_resource($newval)) { - $this->setValue($newval); - return true; - } else { - return PEAR::raiseError('Error while substracting Math_Integer_GMP '. - 'objects: '.$this->toString().' and '. - $int->toString()); - } - } - - function dec() { - $newval = gmp_sub($this->getValue(), gmp_init(1)); - if (is_resource($newval)) { - $this->setValue($newval); - return true; - } else { - return PEAR::raiseError('Error while decrementing Math_Integer_GMP '. - 'object: '.$this->toString()); - } - } - - function mul(&$int) { - if (!$this->_is(&$int, 'Math_Integer_GMP')) { - return PEAR::raiseError('Parameter is not a Math_Integer_GMP object'); - } - $newval = gmp_mul($this->getValue(), $int->getValue()); - if (is_resource($newval)) { - $this->setValue($newval); - return true; - } else { - return PEAR::raiseError('Error while multiplying Math_Integer_GMP '. - 'objects: '.$this->toString().' and '. - $int->toString()); - } - } - - function div(&$int) { - if (!$this->_is(&$int, 'Math_Integer_GMP')) { - return PEAR::raiseError('Parameter is not a Math_Integer_GMP object'); - } - if ($int->isZero()) { - return PEAR::raiseError('Division by zero is undefined'); - } - $newval = gmp_div($this->getValue(), $int->getValue()); - if (is_resource($newval)) { - $this->setValue($newval); - return true; - } else { - return PEAR::raiseError('Error while dividing Math_Integer_GMP '. - 'objects: '.$this->toString().' and '. - $int->toString()); - } - return $this->_noImplemented('div'); - } - - function pow(&$int) { - if (!$this->_is(&$int, 'Math_Integer_GMP')) { - return PEAR::raiseError('Parameter is not a Math_Integer_GMP object'); - } - if ($int->isNegative()) { - return PEAR::raisError('Exponent cannot be negative'); - } elseif ($int->isZero()) { - $this->setValue(1); - } else { - $newval = gmp_pow($this->getValue(), $int->toString()); - if (is_resource($newval)) { - $this->setValue($newval); - return true; - } else { - return PEAR::raiseError('Error while doing pow($n, $e) Math_Integer_GMP '. - 'objects: '.$this->toString().' and '. - $int->toString()); - } - } - } - - function powmod(&$int, &$mod) { - $err = ''; - if (!$this->_is(&$int, 'Math_Integer_GMP')) { - $err .= 'Exponent is not a Math_Integer_GMP object.'; - } elseif ($int->isNegative()) { - $err .= 'Exponent cannot be negative'; - } - if (!is_empty($err)) { - $err .= ' '; - } - if (!$this->_is(&$mod, 'Math_Integer_GMP')) { - $err .= 'Modulus is not a Math_Integer_GMP object.'; - } elseif ($mod->isZero() || $mod->isNegative()) { - $err .= 'Modulus must be positive and greater than zero'; - } - if (!empty($err)) { - return PEAR::raiseError($err); - } - $newval = gmp_powm($this->getValue(), $int->getValue(), $mod->getValue()); - if (is_resource($newval)) { - $this->setValue($newval); - return true; - } else { - return PEAR::raiseError('Error while doing pow(n, e, m) Math_Integer_GMP '. - 'objects: '.$this->toString().', '. - $int->toString().', and '.$mod->toString()); - } - } - - function sqrt() { - if ($this->isZero()) { - return true; - } elseif ($this->isNegative()) { - return PEAR::isError('Cannot take square root of a negative number'); - } else { - $newval = gmp_add($this->getValue(), $int->getValue()); - if (is_resource($newval)) { - $this->setValue($newval); - return true; - } else { - return PEAR::raiseError('Error while taking a square root of: '. - $this->toString()); - } - } - } - - function mod(&$int) { - if (!$this->_is(&$int, 'Math_Integer_GMP')) { - return PEAR::raiseError('Modulus is not a Math_Integer_GMP object'); - } elseif ($int->isZero() || $int->isNegative()) { - return PEAR::raiseError('Modulus must be positive and greater than zero'); - } - $newval = gmp_mod($this->getValue(), $int->getValue()); - if (is_resource($newval)) { - $this->setValue($newval); - return true; - } else { - return PEAR::raiseError('Error while doing mod(i, m) using the '. - 'objects: '.$this->toString().' and '. - $int->toString()); - } - } - - function compare(&$int) { - if (!$this->_is(&$int, 'Math_Integer_GMP')) { - return PEAR::raiseError('Parameter is not a Math_Integer_GMP object'); - } - $cmp = gmp_cmp($this->getValue(), $int->getValue()); - if ($cmp > 0) { - return 1; - } elseif ($cmp == 0) { - return 0; - } else { - return -1; - } - } - - function sign() { - return gmp_sign($this->getValue()); - } - - function gcd(&$int) { - if (!$this->_is(&$int, 'Math_Integer_GMP')) { - return PEAR::raiseError('Parameter is not a Math_Integer_GMP object'); - } - $gcd = gmp_gcd($this->getValue(), $int->getValue()); - if (!is_resource($gcd) || get_resource_type($gcd) != 'GMP integer') { - return PEAR::raiseError('Unkown error calculating GCD'); - } - return new Math_Integer_GMP($gcd); - } - - function isOdd() { - return gmp_mod($this->getValue(), gmp_init(2)) != 0; - } - - function isEven() { - return gmp_mod($this->getValue(), gmp_init(2)) == 0; - } - - function isPositive() { - return gmp_sign($this->getValue()) == 1; - } - - function isNegative() { - return gmp_sign($this->getValue()) == -1; - } - - function isZero() { - return gmp_sign($this->getValue()) == 0; - } +class Math_Integer_GMP extends Math_Integer_Common {/*{{{*/ + + function Math_Integer_GMP($value) {/*{{{*/ + $this->setValue($value); + }/*}}}*/ + + function toString() {/*{{{*/ + return gmp_strval($this->_value); + }/*}}}*/ + + function &clone() {/*{{{*/ + return new Math_Integer_GMP($this->toString()); + }/*}}}*/ + + function setValue($value) {/*{{{*/ + if ($this->_is($value, 'Math_Integer_GMP')) { + $this->setValue($value->toString()); + } elseif (is_resource($value) && get_resource_type($value) == 'GMP integer') { + $this->_value = $value; + } elseif (is_scalar($value)) { + $this->_value = gmp_init($value); + } else { + $this->_value = null; + } + }/*}}}*/ + + function getValue() {/*{{{*/ + return $this->_value; + }/*}}}*/ + + function negate() {/*{{{*/ + $newval = gmp_neg($this->getValue()); + if (is_resource($newval)) { + $this->setValue($newval); + return true; + } else { + return PEAR::raiseError('Error while negating Math_Integer_GMP '. + 'object: '.$this->toString()); + } + }/*}}}*/ + + function abs() {/*{{{*/ + $newval = gmp_abs($this->getValue()); + if (is_resource($newval)) { + $this->setValue($newval); + return true; + } else { + return PEAR::raiseError('Error while doing Math_Integer_GMP::abs() '. + 'object: '.$this->toString()); + } + }/*}}}*/ + + function fact() {/*{{{*/ + $newval = gmp_fact($this->toString()); + if (is_resource($newval)) { + $this->setValue($newval); + return true; + } else { + return PEAR::raiseError('Error while doing Math_Integer_GMP::fact() '. + 'object: '.$this->toString()); + } + }/*}}}*/ + + function add(&$int) {/*{{{*/ + if (!$this->_is(&$int, 'Math_Integer_GMP')) { + return PEAR::raiseError('Parameter is not a Math_Integer_GMP object'); + } + $newval = gmp_add($this->getValue(), $int->getValue()); + if (is_resource($newval)) { + $this->setValue($newval); + return true; + } else { + return PEAR::raiseError('Error while adding Math_Integer_GMP '. + 'objects: '.$this->toString().' and '. + $int->toString()); + } + }/*}}}*/ + + function inc() {/*{{{*/ + $newval = gmp_add($this->getValue(), gmp_init(1)); + if (is_resource($newval)) { + $this->setValue($newval); + return true; + } else { + return PEAR::raiseError('Error while incrementing Math_Integer_GMP '. + 'object: '.$this->toString()); + } + }/*}}}*/ + + function sub(&$int) {/*{{{*/ + if (!$this->_is(&$int, 'Math_Integer_GMP')) { + return PEAR::raiseError('Parameter is not a Math_Integer_GMP object'); + } + $newval = gmp_sub($this->getValue(), $int->getValue()); + if (is_resource($newval)) { + $this->setValue($newval); + return true; + } else { + return PEAR::raiseError('Error while substracting Math_Integer_GMP '. + 'objects: '.$this->toString().' and '. + $int->toString()); + } + }/*}}}*/ + + function dec() {/*{{{*/ + $newval = gmp_sub($this->getValue(), gmp_init(1)); + if (is_resource($newval)) { + $this->setValue($newval); + return true; + } else { + return PEAR::raiseError('Error while decrementing Math_Integer_GMP '. + 'object: '.$this->toString()); + } + }/*}}}*/ + + function mul(&$int) {/*{{{*/ + if (!$this->_is(&$int, 'Math_Integer_GMP')) { + return PEAR::raiseError('Parameter is not a Math_Integer_GMP object'); + } + $newval = gmp_mul($this->getValue(), $int->getValue()); + if (is_resource($newval)) { + $this->setValue($newval); + return true; + } else { + return PEAR::raiseError('Error while multiplying Math_Integer_GMP '. + 'objects: '.$this->toString().' and '. + $int->toString()); + } + }/*}}}*/ + + function div(&$int) {/*{{{*/ + if (!$this->_is(&$int, 'Math_Integer_GMP')) { + return PEAR::raiseError('Parameter is not a Math_Integer_GMP object'); + } + if ($int->isZero()) { + return PEAR::raiseError('Division by zero is undefined'); + } + $newval = gmp_div($this->getValue(), $int->getValue()); + if (is_resource($newval)) { + $this->setValue($newval); + return true; + } else { + return PEAR::raiseError('Error while dividing Math_Integer_GMP '. + 'objects: '.$this->toString().' and '. + $int->toString()); + } + return $this->_noImplemented('div'); + }/*}}}*/ + + function pow(&$int) {/*{{{*/ + if (!$this->_is(&$int, 'Math_Integer_GMP')) { + return PEAR::raiseError('Parameter is not a Math_Integer_GMP object'); + } + if ($int->isNegative()) { + return PEAR::raisError('Exponent cannot be negative'); + } elseif ($int->isZero()) { + $this->setValue(1); + } else { + $newval = gmp_pow($this->getValue(), $int->toString()); + if (is_resource($newval)) { + $this->setValue($newval); + return true; + } else { + return PEAR::raiseError('Error while doing pow($n, $e) Math_Integer_GMP '. + 'objects: '.$this->toString().' and '. + $int->toString()); + } + } + }/*}}}*/ + + function powmod(&$int, &$mod) {/*{{{*/ + $err = ''; + if (!$this->_is(&$int, 'Math_Integer_GMP')) { + $err .= 'Exponent is not a Math_Integer_GMP object.'; + } elseif ($int->isNegative()) { + $err .= 'Exponent cannot be negative'; + } + if (!is_empty($err)) { + $err .= ' '; + } + if (!$this->_is(&$mod, 'Math_Integer_GMP')) { + $err .= 'Modulus is not a Math_Integer_GMP object.'; + } elseif ($mod->isZero() || $mod->isNegative()) { + $err .= 'Modulus must be positive and greater than zero'; + } + if (!empty($err)) { + return PEAR::raiseError($err); + } + $newval = gmp_powm($this->getValue(), $int->getValue(), $mod->getValue()); + if (is_resource($newval)) { + $this->setValue($newval); + return true; + } else { + return PEAR::raiseError('Error while doing pow(n, e, m) Math_Integer_GMP '. + 'objects: '.$this->toString().', '. + $int->toString().', and '.$mod->toString()); + } + }/*}}}*/ + + function sqrt() {/*{{{*/ + if ($this->isZero()) { + return true; + } elseif ($this->isNegative()) { + return PEAR::isError('Cannot take square root of a negative number'); + } else { + $newval = gmp_add($this->getValue(), $int->getValue()); + if (is_resource($newval)) { + $this->setValue($newval); + return true; + } else { + return PEAR::raiseError('Error while taking a square root of: '. + $this->toString()); + } + } + }/*}}}*/ + + function mod(&$int) {/*{{{*/ + if (!$this->_is(&$int, 'Math_Integer_GMP')) { + return PEAR::raiseError('Modulus is not a Math_Integer_GMP object'); + } elseif ($int->isZero() || $int->isNegative()) { + return PEAR::raiseError('Modulus must be positive and greater than zero'); + } + $newval = gmp_mod($this->getValue(), $int->getValue()); + if (is_resource($newval)) { + $this->setValue($newval); + return true; + } else { + return PEAR::raiseError('Error while doing mod(i, m) using the '. + 'objects: '.$this->toString().' and '. + $int->toString()); + } + }/*}}}*/ + + function compare(&$int) {/*{{{*/ + if (!$this->_is(&$int, 'Math_Integer_GMP')) { + return PEAR::raiseError('Parameter is not a Math_Integer_GMP object'); + } + $cmp = gmp_cmp($this->getValue(), $int->getValue()); + if ($cmp > 0) { + return 1; + } elseif ($cmp == 0) { + return 0; + } else { + return -1; + } + }/*}}}*/ + + function sign() {/*{{{*/ + return gmp_sign($this->getValue()); + }/*}}}*/ + + function gcd(&$int) {/*{{{*/ + if (!$this->_is(&$int, 'Math_Integer_GMP')) { + return PEAR::raiseError('Parameter is not a Math_Integer_GMP object'); + } + $gcd = gmp_gcd($this->getValue(), $int->getValue()); + if (!is_resource($gcd) || get_resource_type($gcd) != 'GMP integer') { + return PEAR::raiseError('Unkown error calculating GCD'); + } + return new Math_Integer_GMP($gcd); + }/*}}}*/ + + function isOdd() {/*{{{*/ + return gmp_mod($this->getValue(), gmp_init(2)) != 0; + }/*}}}*/ + + function isEven() {/*{{{*/ + return gmp_mod($this->getValue(), gmp_init(2)) == 0; + }/*}}}*/ + + function isPositive() {/*{{{*/ + return gmp_sign($this->getValue()) == 1; + }/*}}}*/ + + function isNegative() {/*{{{*/ + return gmp_sign($this->getValue()) == -1; + }/*}}}*/ + + function isZero() {/*{{{*/ + return gmp_sign($this->getValue()) == 0; + }/*}}}*/ + + function probPrime($reps=10) {/*{{{*/ + if (!is_int($reps)) { + return PEAR::raiseError('Expecting an integer for the number of repeats'); + } + if ($reps < 5 || $reps > 10) { + return PEAR::raiseError("Number of repeats for algorithm must be between 5 ". + "and 10 (default). You requested $reps repreats"); + } + return gmp_prob_prime($this->getValue(), $reps); + }/*}}}*/ - -} +}/*}}}*/ ?>
« previous php.pear.cvs (#8140) next »