Source for file gmp.php
Documentation is available at gmp.php
//include_once 'Math/Integer/common.php';
include_once 'common.php';
class Math_Integer_GMP extends Math_Integer_Common {/*{{{*/
function Math_Integer_GMP ($value) {/*{{{*/
function toString () {/*{{{*/
return gmp_strval ($this->_value);
function makeClone () {/*{{{*/
return new Math_Integer_GMP ($this->toString ());
function setValue ($value) {/*{{{*/
if ($this->_is ($value, 'Math_Integer_GMP')) {
$this->setValue ($value->toString ());
$this->_value = gmp_init ($value);
function getValue () {/*{{{*/
function negate () {/*{{{*/
$newval = gmp_neg ($this->getValue ());
$this->setValue ($newval);
return PEAR ::raiseError ('Error while negating Math_Integer_GMP '.
'object: '. $this->toString ());
$newval = gmp_abs ($this->getValue ());
$this->setValue ($newval);
return PEAR ::raiseError ('Error while doing Math_Integer_GMP::abs() '.
'object: '. $this->toString ());
$newval = gmp_fact ($this->toString ());
$this->setValue ($newval);
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 ());
$this->setValue ($newval);
return PEAR ::raiseError ('Error while adding Math_Integer_GMP '.
'objects: '. $this->toString (). ' and '.
$newval = gmp_add ($this->getValue (), gmp_init (1 ));
$this->setValue ($newval);
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 ());
$this->setValue ($newval);
return PEAR ::raiseError ('Error while substracting Math_Integer_GMP '.
'objects: '. $this->toString (). ' and '.
$newval = gmp_sub ($this->getValue (), gmp_init (1 ));
$this->setValue ($newval);
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 ());
$this->setValue ($newval);
return PEAR ::raiseError ('Error while multiplying Math_Integer_GMP '.
'objects: '. $this->toString (). ' and '.
function div (&$int) {/*{{{*/
if (!$this->_is (&$int, 'Math_Integer_GMP')) {
return PEAR ::raiseError ('Parameter is not a Math_Integer_GMP object');
return PEAR ::raiseError ('Division by zero is undefined');
$newval = gmp_div ($this->getValue (), $int->getValue ());
$this->setValue ($newval);
return PEAR ::raiseError ('Error while dividing Math_Integer_GMP '.
'objects: '. $this->toString (). ' and '.
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 ::raiseError ('Exponent cannot be negative');
} elseif ($int->isZero ()) {
$newval = gmp_pow ($this->getValue (), $int->toString ());
$this->setValue ($newval);
return PEAR ::raiseError ('Error while doing pow($n, $e) Math_Integer_GMP '.
'objects: '. $this->toString (). ' and '.
function powmod (&$int, &$mod) {/*{{{*/
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 (!$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';
return PEAR ::raiseError ($err);
$newval = gmp_powm ($this->getValue (), $int->getValue (), $mod->getValue ());
$this->setValue ($newval);
return PEAR ::raiseError ('Error while doing pow(n, e, m) Math_Integer_GMP '.
'objects: '. $this->toString (). ', '.
$int->toString (). ', and '. $mod->toString ());
} elseif ($this->isNegative ()) {
return PEAR ::raiseError ('Cannot take square root of a negative number');
$newval = gmp_add ($this->getValue (), $int->getValue ());
$this->setValue ($newval);
return PEAR ::raiseError ('Error while taking a square root of: '.
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 ());
$this->setValue ($newval);
return PEAR ::raiseError ('Error while doing mod(i, m) using the '.
'objects: '. $this->toString (). ' and '.
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 ());
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 ());
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 isPrime ($reps=10 ) {/*{{{*/
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);
Documentation generated on Mon, 11 Mar 2019 15:39:15 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|