cvs: pear /Math_Integer Integer.php IntegerOp.php package.xml /Math_Integer/test testMath_Integer.php

From: Date: Thu, 02 Jan 2003 01:55:06 +0000
Subject: cvs: pear /Math_Integer Integer.php IntegerOp.php package.xml /Math_Integer/test testMath_Integer.php
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-8028@lists.php.net to get a copy of this message
jmcastagnetto Wed Jan 1 20:55:06 2003 EDT Added files: /pear/Math_Integer Integer.php IntegerOp.php package.xml /pear/Math_Integer/test testMath_Integer.php Log: Initial import into PEAR

Index: pear/Math_Integer/Integer.php +++ pear/Math_Integer/Integer.php <?php // // +----------------------------------------------------------------------+ // | PHP Version 4 | // +----------------------------------------------------------------------+ // | Copyright (c) 1997-2002 The PHP Group | // +----------------------------------------------------------------------+ // | This source file is subject to version 2.0 of the PHP license, | // | that is bundled with this package in the file LICENSE, and is | // | available at through the world-wide-web at | // | http://www.php.net/license/2_02.txt. | // | If you did not receive a copy of the PHP license and are unable to | // | obtain it through the world-wide-web, please send a note to | // | license@php.net so we can mail you a copy immediately. | // +----------------------------------------------------------------------+ // | Authors: Jesus M. Castagnetto <jmcastagnetto@php.net> | // +----------------------------------------------------------------------+ // // $Id: Integer.php,v 1.1 2003/01/02 01:55:05 jmcastagnetto Exp $ // include_once 'PEAR.php'; /** * The maximum integer we will accept if using the standard signed long */ define ('MATH_MAXINT', 2147483647); // allow for user override of the integer library to use if (!defined('MATH_INTLIB')) { if (extension_loaded('gmp')) { define ('MATH_INTLIB', 'gmp'); } elseif (extension_loaded('bcmath')) { define ('MATH_INTLIB', 'bcmath'); bcscale(0); } else { define ('MATH_INTLIB', 'std'); } } /** * Class to represent integers. If the GMP or BCMATH libraries are present the * integer can be of arbitrary size, otherwise the internal PHP integer * representation will be use (signed long). If the latter case, you cannot * create Math_Integer objects bigger than MATH_MAXINT. * * @author Jesus M. Castagnetto <jmcastagnetto@php.net> * @version 0.8 * @access public * @package Math_Integer */ class Math_Integer {/*{{{*/ /** * If the GMP library is present, this variable will contain the resource * handle created using gmp_init(). If only the BCMATH library is present * it will contain the string representation of the integer. If neither * GMP or BCMATH are present, it will contain the actual integer. * * @access private * @var mixed */ var $_value = null; /** * Class constructor, accepts an optional number or string representing the integer. * * @param optional mixed $num an integer or its string representation * @return Math_Integer object * @access public */ function Math_Integer($num=null) {/*{{{*/ if (!is_null($num)) { $this->setValue($num); } }/*}}}*/ /** * Method to set the value for the object * @param optional mixed $num an integer or its string representation * @return mixed TRUE on success, a PEAR_Error otherwise * @access public */ function setValue($num) {/*{{{*/ if (!is_scalar($num)) { $this->_value = null; return PEAR::raiseError('Parameter is not a number'); } switch (MATH_INTLIB) { case 'gmp' : $this->_value = gmp_init(strval($num)); return true; break; case 'bcmath' : $this->_value = $num; return true; break; case 'std' : default : if ($num > MATH_MAXINT) { $this->_value = null; return PEAR::raiseError('Cannot initialize, number > MATH_MAXINT '. 'and no gmp or bcmath extensions detected'); } else { $this->_value = intval($num); return true; } break; } }/*}}}*/ /** * Returns the content of $this->_value * * @return mixed the value on success, a PEAR_Error otherwise * @access public */ function getValue() {/*{{{*/ if (!$this->initialized()) { return PEAR::raiseError('Math_Integer object not initialized'); } return $this->_value; }/*}}}*/ /** * Returns a string representation of the value * * @return mixed a string on success, a PEAR_Error otherwise * @access public */ function toString() {/*{{{*/ if (!$this->initialized()) { return PEAR::raiseError('Math_Integer object not initialized'); } if (MATH_INTLIB == 'gmp') { return gmp_strval($this->_value); } else { // for 'std' and 'bcmath' return (string) $this->_value; } }/*}}}*/ /** * Checks if the object has been succesfully initialized * * @return boolean TRUE if initialized, FALSE otherwise * @access public */ function initialized() {/*{{{*/ return !is_null($this->_value); }/*}}}*/ }/*}}} end of Math_Integer */ // vim: ts=4:sw=4:et: // vim6: fdl=1: ?> Index: pear/Math_Integer/IntegerOp.php +++ pear/Math_Integer/IntegerOp.php <?php // // +----------------------------------------------------------------------+ // | PHP Version 4 | // +----------------------------------------------------------------------+ // | Copyright (c) 1997-2002 The PHP Group | // +----------------------------------------------------------------------+ // | This source file is subject to version 2.0 of the PHP license, | // | that is bundled with this package in the file LICENSE, and is | // | available at through the world-wide-web at | // | http://www.php.net/license/2_02.txt. | // | If you did not receive a copy of the PHP license and are unable to | // | obtain it through the world-wide-web, please send a note to | // | license@php.net so we can mail you a copy immediately. | // +----------------------------------------------------------------------+ // | Authors: Jesus M. Castagnetto <jmcastagnetto@php.net> | // +----------------------------------------------------------------------+ // // $Id: IntegerOp.php,v 1.1 2003/01/02 01:55:05 jmcastagnetto Exp $ // include_once 'Math/Integer.php'; /** * Class implementing operations on Math_Integer objects. If available it * will use the GMP or BCMATH libraries. Will default to the standard PHP * integer representation otherwise. * * The operations are implemented as static methods of the class. * * @author Jesus M. Castagnetto <jmcastagnetto@php.net> * @version 0.8 * @access public * @package Math_Integer */ class Math_IntegerOp {/*{{{*/ /** * Checks if the given parameter is a Math_Integer object * * @param object Math_Integer $int1 * @return boolean TRUE if parameter is an instance of Math_Integer, FALSE otherwise * @access public */ function isMath_Integer(&$int) {/*{{{*/ if (function_exists('is_a')) { return is_a($int, 'Math_Integer'); } else { return get_class($int) == 'math_integer' || is_subclass_of($int, 'math_integer'); } }/*}}}*/ /** * Add two Math_Integer objects: $i1 + $i2 * * @param object Math_Integer $int1 * @param object Math_Integer $int2 * @return object Math_Integer on success, PEAR_Error otherwise * @access public */ function &add(&$int1, &$int2) {/*{{{*/ if (PEAR::isError($err = Math_IntegerOp::_validInts($int1, $int2))) { return $err; } switch (MATH_INTLIB) {/*{{{*/ case 'gmp' : $tmp = gmp_strval(gmp_add($int1->getValue(), $int2->getValue())); break; case 'bcmath' : $tmp = bcadd($int1->getValue(), $int2->getValue()); break; case 'std' : $tmp = $int1->getValue() + $int2->getValue(); break; }/*}}}*/ return new Math_Integer($tmp); }/*}}}*/ /** * Substract two Math_Integer objects: $i1 - $i2 * * @param object Math_Integer $int1 * @param object Math_Integer $int2 * @return object Math_Integer on success, PEAR_Error otherwise * @access public */ function &sub(&$int1, &$int2) {/*{{{*/ if (PEAR::isError($err = Math_IntegerOp::_validInts($int1, $int2))) { return $err; } switch (MATH_INTLIB) {/*{{{*/ case 'gmp' : $tmp = gmp_strval(gmp_sub($int1->getValue(), $int2->getValue())); break; case 'bcmath' : $tmp = bcsub($int1->getValue(), $int2->getValue()); break; case 'std' : $tmp = $int1->getValue() - $int2->getValue(); break; }/*}}}*/ return new Math_Integer($tmp); }/*}}}*/ /** * Multiply two Math_Integer objects: $i1 * $i2 * * @param object Math_Integer $int1 * @param object Math_Integer $int2 * @return object Math_Integer on success, PEAR_Error otherwise * @access public */ function &mul(&$int1, &$int2) {/*{{{*/ if (PEAR::isError($err = Math_IntegerOp::_validInts($int1, $int2))) { return $err; } switch (MATH_INTLIB) {/*{{{*/ case 'gmp' : $tmp = gmp_strval(gmp_mul($int1->getValue(), $int2->getValue())); break; case 'bcmath' : $tmp = bcmul($int1->getValue(), $int2->getValue()); break; case 'std' : $tmp = $int1->getValue() * $int2->getValue(); break; }/*}}}*/ return new Math_Integer($tmp); }/*}}}*/ /** * Divide two Math_Integer objects: $i1 / $i2 * * @param object Math_Integer $int1 * @param object Math_Integer $int2 * @return object Math_Integer on success, PEAR_Error otherwise * @access public */ function &div(&$int1, &$int2) {/*{{{*/ if (PEAR::isError($err = Math_IntegerOp::_validInts($int1, $int2))) { return $err; } switch (MATH_INTLIB) {/*{{{*/ case 'gmp' : $tmp = gmp_strval(gmp_div($int1->getValue(), $int2->getValue())); break; case 'bcmath' : $tmp = bcdiv($int1->getValue(), $int2->getValue()); break; case 'std' : $tmp = intval($int1->getValue() / $int2->getValue()); break; }/*}}}*/ return new Math_Integer($tmp); }/*}}}*/ /** * Calculate the modulus of $i1 and $i2: $i1 % $i2 * * @param object Math_Integer $int1 * @param object Math_Integer $int2 * @return object Math_Integer on success, PEAR_Error otherwise * @access public */ function &mod(&$int1, &$int2) {/*{{{*/ if (PEAR::isError($err = Math_IntegerOp::_validInts($int1, $int2))) { return $err; } switch (MATH_INTLIB) {/*{{{*/ case 'gmp' : $tmp = gmp_strval(gmp_mod($int1->getValue(), $int2->getValue())); break; case 'bcmath' : $tmp = bcmod($int1->getValue(), $int2->getValue()); break; case 'std' : $tmp = $int1->getValue() % $int2->getValue(); break; }/*}}}*/ return new Math_Integer($tmp); }/*}}}*/ /** * Raise $i1 to the $i2 exponent: $i1^$i2 * * @param object Math_Integer $int1 * @param object Math_Integer $int2 * @return object Math_Integer on success, PEAR_Error otherwise * @access public */ function &pow(&$int1, &$int2) {/*{{{*/ if (PEAR::isError($err = Math_IntegerOp::_validInts($int1, $int2))) { return $err; } switch (MATH_INTLIB) {/*{{{*/ case 'gmp' : $tmp = gmp_strval(gmp_pow($int1->getValue(), (int) $int2->toString())); break; case 'bcmath' : $tmp = bcpow($int1->getValue(), $int2->getValue()); break; case 'std' : $tmp = pow($int1->getValue(), $int2->getValue()); break; }/*}}}*/ return new Math_Integer($tmp); }/*}}}*/ /** * Compare two Math_Integer objects. * if $i1 > $i2, returns +1, * if $i1 == $i2, returns +0, * if $i1 < $i2, returns -1, * * @param object Math_Integer $int1 * @param object Math_Integer $int2 * @return mixed and integer on success, PEAR_Error otherwise * @access public * @see Math_IntegerOp::sign */ function &compare(&$int1, &$int2) {/*{{{*/ if (PEAR::isError($err = Math_IntegerOp::_validInts($int1, $int2))) { return $err; } switch (MATH_INTLIB) {/*{{{*/ case 'gmp' : $cmp = gmp_cmp($int1->getValue(), $int2->getValue()); break; case 'bcmath' : $cmp = bccomp($int1->getValue(), $int2->getValue()); break; case 'std' : $cmp = $int1->getValue() - $int2->getValue(); break; }/*}}}*/ return Math_IntegerOp::sign(new Math_Integer($cmp)); }/*}}}*/ /** * Returns the sign of a Math_Integer number * if $i1 > 0, returns +1, * if $i1 == 0, returns +0, * if $i1 < 0, returns -1, * * @param object Math_Integer $int1 * @return mixed and integer on success, PEAR_Error otherwise * @access public */ function &sign(&$int1) {/*{{{*/ if (PEAR::isError($err = Math_IntegerOp::_validInt($int1))) { return $err; } switch (MATH_INTLIB) {/*{{{*/ case 'gmp' : return gmp_sign($int1->getValue()); break; case 'bcmath' : case 'std' : $tmp = $int1->getValue(); if ($tmp > 0) { return 1; } elseif ($tmp < 0) { return -1; } else { // $tmp == 0 return 0; } break; }/*}}}*/ }/*}}}*/ /** * Returns the negative of a Math_Integer number: -1 * $i1 * * @param object Math_Integer $int1 * @return object Math_Integer on success, PEAR_Error otherwise * @access public */ function &neg(&$int1) {/*{{{*/ if (PEAR::isError($err = Math_IntegerOp::_validInt($int1))) { return $err; } switch (MATH_INTLIB) {/*{{{*/ case 'gmp' : $tmp = gmp_strval(gmp_neg($int1->getValue())); break; case 'bcmath' : $tmp = bcmul(-1, $int1->getValue()); break; case 'std' : $tmp = -1 * $int1->getValue(); break; }/*}}}*/ return new Math_Integer($tmp); }/*}}}*/ /** * Returns the (integer) square root of a Math_Integer number * * @param object Math_Integer $int1 * @return object Math_Integer on success, PEAR_Error otherwise * @access public */ function &sqrt(&$int1) {/*{{{*/ if (PEAR::isError($err = Math_IntegerOp::_validInt($int1))) { return $err; } switch (MATH_INTLIB) {/*{{{*/ case 'gmp' : $tmp = gmp_strval(gmp_sqrt($int1->getValue())); break; case 'bcmath' : $tmp = bcsqrt(-1, $int1->getValue()); break; case 'std' : $tmp = intval(sqrt($int1->getValue())); break; }/*}}}*/ return new Math_Integer($tmp); }/*}}}*/ /** * Returns the absolute value of a Math_Integer number * * @param object Math_Integer $int1 * @return object Math_Integer on success, PEAR_Error otherwise * @access public */ function &abs(&$int1) {/*{{{*/ if (PEAR::isError($err = Math_IntegerOp::_validInt($int1))) { return $err; } switch (MATH_INTLIB) {/*{{{*/ case 'gmp' : $tmp = gmp_strval(gmp_abs($int1->getValue())); break; case 'bcmath' : if ($int1->getValue() < 0) { $tmp = bcmul(-1, $int1->getValue()); } else { $tmp = $int1->getValue(); } break; case 'std' : $tmp = abs($int1->getValue()); break; }/*}}}*/ return new Math_Integer($tmp); }/*}}}*/ /** * Checks that the 2 passed objects are valid Math_Integer numbers. * The objects must be instances of Math_Integer and have been properly * initialized. * * @param object Math_Integer $int1 * @param object Math_Integer $int2 * @return mixed TRUE if both are Math_Integer objects, PEAR_Error otherwise * @access private */ function _validInts(&$int1, &$int2) {/*{{{*/ $err1 = Math_IntegerOp::_validInt($int1); $err2 = Math_IntegerOp::_validInt($int2); $error = ''; if (PEAR::isError($err1)) { $error .= 'First parameter: '.$err1->getMessage(); } if (PEAR::isError($err2)) { $error .= ' Second parameter: '.$err2->getMessage(); } if (!empty($error)) { return PEAR::raiseError($error); } else { return true; } }/*}}}*/ /** * Checks that the passed object is a valid Math_Integer number. * The object must be an instance of Math_Integer and have been properly * initialized. * * @param object Math_Integer $int1 * @return mixed TRUE if is a Math_Integer object, PEAR_Error otherwise * @access private */ function _validInt(&$int1) {/*{{{*/ $error = ''; if (!Math_IntegerOp::isMath_Integer($int1)) { $error = 'Is not a Math_Integer object.'; } elseif (!$int1->initialized()) { $error = 'Math_Integer object is uninitalized.'; } if (!empty($error)) { return PEAR::raiseError($error); } else { return true; } }/*}}}*/ }/*}}} end of Math_IntegerOp */ // vim: ts=4:sw=4:et: // vim6: fdl=1: ?> Index: pear/Math_Integer/package.xml +++ pear/Math_Integer/package.xml <?xml version="1.0" encoding="ISO-8859-1"?> <!DOCTYPE package SYSTEM "../package.dtd"> <!-- do not use the "Type" attribute here, that one is only for generated package.xml files --> <!-- $Id: package.xml,v 1.1 2003/01/02 01:55:05 jmcastagnetto Exp $ --> <package> <name>Math_Integer</name> <summary>Package to represent and manipulate integers</summary> <description> The class Math_Integer can represent integers bigger than the signed longs that are the default of PHP, if either the GMP or the BCMATH (bundled with PHP) are present. Otherwise it will fall back to the internal integer representation. The Math_IntegerOp class defines operations on Math_Integer objects. </description> <maintainers> <maintainer> <user>jmcastagnetto</user> <name>Jesus M. Castagnetto</name> <email>jmcastagnetto@php.net</email> <role>lead</role> </maintainer> </maintainers> <release> <license>PHP</license> <version>0.8</version> <date>2002-12-31</date> <notes> Initial release under PEAR. </notes> <state>stable</state> <filelist> <dir name="/" baseinstalldir="Math"> <file role="php">Integer.php</file> <file role="php">IntegerOp.php</file> <dir name="test" role="test"> <file role="php">testMath_Integer.php</file> </dir> </dir> </filelist> </release> <changelog> <release> </release> </changelog> </package> Index: pear/Math_Integer/test/testMath_Integer.php +++ pear/Math_Integer/test/testMath_Integer.php <?php echo date('\* r')."\n"; echo '* PHP version: '.phpversion()."\n"; echo '* Zend version: '.zend_version()."\n"; // force to use a particular lib // comment all out to get automatic selection //define ('MATH_INTLIB', 'gmp'); //define ('MATH_INTLIB', 'bcmath'); //define ('MATH_INTLIB', 'std'); include_once 'Math/IntegerOp.php'; if (MATH_INTLIB == 'gmp' || MATH_INTLIB == 'bcmath') { $i1 = new Math_Integer('333333333333333333333333'); $i2 = new Math_Integer('111111111111111111111111'); } else { $i1 = new Math_Integer('33333'); $i2 = new Math_Integer('11111'); } $i3 = new Math_Integer(6); echo '* Using lib: '.MATH_INTLIB."\n"; echo 'i1 = '.$i1->toString()."\n"; echo 'i2 = '.$i2->toString()."\n"; echo 'i3 = '.$i3->toString()."\n"; $res = Math_IntegerOp::add($i1, $i2); echo 'i1 + i2 = '.$res->toString()."\n"; $res = Math_IntegerOp::sub($i1, $i2); echo 'i1 - i2 = '.$res->toString()."\n"; $res = Math_IntegerOp::sub($i2, $i1); echo 'i2 - i1 = '.$res->toString()."\n"; $res = Math_IntegerOp::mul($i1, $i2); echo 'i1 * i2 = '.$res->toString()."\n"; $res = Math_IntegerOp::div($i1, $i3); echo 'i1 / i3 = '.$res->toString()."\n"; $res = Math_IntegerOp::mod($i2, $i3); echo 'i1 % i3 = '.$res->toString()."\n"; $res = Math_IntegerOp::neg($i1); echo 'neg(i1) = '.$res->toString()."\n"; echo 'sign(neg(i1)) = '.Math_IntegerOp::sign($res)."\n"; echo 'sign(neg(0)) = '.Math_IntegerOp::sign(new Math_Integer(0))."\n"; echo 'sign(i2) = '.Math_IntegerOp::sign($i2)."\n"; echo 'compare(i1, i2) = '.Math_IntegerOp::compare($i1, $i2)."\n"; echo 'compare(i3, i3) = '.Math_IntegerOp::compare($i3, $i3)."\n"; echo 'compare(i2, i1) = '.Math_IntegerOp::compare($i2, $i1)."\n"; $res = Math_IntegerOp::abs(Math_IntegerOp::neg($i2)); echo 'abs(neg(i2)) = '.$res->toString()."\n"; // vim: ts=4:sw=4:et: // vim6: fdl=1: ?>
« previous php.pear.cvs (#8028) next »