Source for file bcmath.php
Documentation is available at bcmath.php
include_once 'common.php';
//include_once 'Math/Integer/common.php';
class Math_Integer_BCMATH extends Math_Integer_Common {/*{{{*/
function Math_Integer_BCMATH ($value) {/*{{{*/
function makeClone () {/*{{{*/
return new Math_Integer_BCMATH ($this->toString ());
function setValue ($value) {/*{{{*/
if ($this->_is ($value, 'Math_Integer_BCMATH')) {
$this->setValue ($value->toString ());
function negate () {/*{{{*/
$newval = bcmul ($this->getValue (), -1 );
$this->setValue ($newval);
if ($this->isNegative ()) {
if ($this->isNegative ()) {
return PEAR ::raiseError ('Factorial of a negative number is undefined');
$val = $this->getValue ();
while (bccomp ($val, 1 ) != 0 ) {
$fact = bcmul ($fact, $val);
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);
$newval = bcadd ($this->getValue (), 1 );
$this->setValue ($newval);
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);
$newval = bcsub ($this->getValue (), 1 );
$this->setValue ($newval);
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);
function div (&$int) {/*{{{*/
if (!$this->_is (&$int, 'Math_Integer_BCMATH')) {
return PEAR ::raiseError ('Paramater is not a Math_Integer_BCMATH object');
return PEAR ::raiseError ('Division by zero is undefined');
$newval = bcdiv ($this->getValue (), $int->getValue ());
$this->setValue ($newval);
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);
function powmod (&$int, &$mod) {/*{{{*/
if (!$this->_is (&$int, 'Math_Integer_BCMATH')) {
$err .= 'Exponent is not a Math_Integer_BCMATH object.';
if (!$this->_is (&$mod, 'Math_Integer_BCMATH')) {
$err .= 'Modulus is not a Math_Integer_BCMATH object.';
if ($mod->isZero () || $mod->isNegative ()) {
$err .= 'Modulus object must be positive.';
return PEAR ::raiseError ($err);
$newval = bcpowmod ($this->getValue (), $int->getValue (), $mod->getValue ());
$this->setValue ($newval);
} elseif ($this->isNegative ()) {
return PEAR ::raiseError ('Cannot take square root of a negative number');
$newval = bcsqrt ($this->getValue (), $int->getValue (), $mod->getValue ());
$this->setValue ($newval);
function mod (&$int) {/*{{{*/
if (!$this->_is (&$int, 'Math_Integer_BCMATH')) {
$err = 'Modulus is not a Math_Integer_BCMATH object.';
if ($int->isZero () || $int->isNegative ()) {
$err = 'Modulus object must be positive.';
return PEAR ::raiseError ($err);
$newval = bcmod ($this->getValue (), $int->getValue ());
$this->setValue ($newval);
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 ());
if ($this->isNegative ()) {
} elseif ($this->isZero ()) {
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->makeClone ();
$int2 = $int->makeClone ();
// make sure both are positive
if ($int1->isNegative ()) {
if ($int2->isNegative ()) {
if ($int1->compare ($int2) == -1 ) {
$mod = $int1->mod ($int2);
if (PEAR ::isError ($mod)) {
} elseif (!$int1->isZero ()) {
return $int2->gcd ($int1);
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;
Documentation generated on Mon, 11 Mar 2019 15:39:15 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|