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

Source for file IntegerOp.php

Documentation is available at IntegerOp.php

  1. <?php
  2. //
  3. // +----------------------------------------------------------------------+
  4. // | PHP Version 4                                                        |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2003 The PHP Group                                |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.0 of the PHP license,       |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available at through the world-wide-web at                           |
  11. // | http://www.php.net/license/2_02.txt.                                 |
  12. // | If you did not receive a copy of the PHP license and are unable to   |
  13. // | obtain it through the world-wide-web, please send a note to          |
  14. // | license@php.net so we can mail you a copy immediately.               |
  15. // +----------------------------------------------------------------------+
  16. // | Authors: Jesus M. Castagnetto <jmcastagnetto@php.net>                |
  17. // +----------------------------------------------------------------------+
  18. //
  19. // $Id: IntegerOp.php 303940 2010-10-02 13:53:19Z clockwerx $
  20. //
  21.  
  22. include_once 'Math/Integer.php';
  23.  
  24. /**
  25.  * Class implementing operations on Math_Integer objects. If available it
  26.  * will use the GMP or BCMATH libraries. Will default to the standard PHP
  27.  * integer representation otherwise.
  28.  * 
  29.  * The operations are implemented as static methods of the class.
  30.  *
  31.  * @author  Jesus M. Castagnetto <jmcastagnetto@php.net>
  32.  * @version 0.9
  33.  * @access  public
  34.  * @package Math_Integer
  35.  */
  36. class Math_IntegerOp {/*{{{*/
  37.  
  38.     /**
  39.      * Checks if the given parameter is a Math_Integer object
  40.      *
  41.      * @param object Math_Integer $int1 
  42.      * @return boolean TRUE if parameter is an instance of Math_Integer, FALSE otherwise
  43.      * @access public
  44.      */
  45.     function isInteger(&$int{/*{{{*/
  46.         return strtolower(get_class($int)) != 'math_integer_common' 
  47.                 && is_subclass_of($int'math_integer_common');
  48.     }/*}}}*/
  49.  
  50.     /**
  51.      * Checks if the Math_Integer object is Odd
  52.      *
  53.      * @param object Math_Integer $int1 
  54.      * @return mixed TRUE if Math_Integer object is odd, FALSE if it is not, PEAR_Error on error.
  55.      * @access public
  56.      */
  57.     function isOdd(&$int{/*{{{*/
  58.         $err Math_IntegerOp::_validInt($int);
  59.         if (PEAR::isError($err)) {
  60.             return $err;
  61.         }
  62.         return $int->isOdd();
  63.     }/*}}}*/
  64.  
  65.     /**
  66.      * Checks if the Math_Integer object is even
  67.      *
  68.      * @param object Math_Integer $int1 
  69.      * @return mixed TRUE if Math_Integer object is even, FALSE if it is not, PEAR_Error on error.
  70.      * @access public
  71.      */
  72.     function isEven(&$int{/*{{{*/
  73.         $err Math_IntegerOp::_validInt($int);
  74.         if (PEAR::isError($err)) {
  75.             return $err;
  76.         }
  77.         return $int->isEven();
  78.     }/*}}}*/
  79.  
  80.     /**
  81.      * Checks if the Math_Integer object is positive
  82.      *
  83.      * @param object Math_Integer $int1 
  84.      * @return mixed TRUE if Math_Integer object is positive, FALSE if it is not, PEAR_Error on error.
  85.      * @access public
  86.      */
  87.     function isPositive(&$int{/*{{{*/
  88.         $err Math_IntegerOp::_validInt($int);
  89.         if (PEAR::isError($err)) {
  90.             return $err;
  91.         }
  92.         return $int->isPositive();
  93.     }/*}}}*/
  94.  
  95.     /**
  96.      * Checks if the Math_Integer object is negative
  97.      *
  98.      * @param object Math_Integer $int1 
  99.      * @return mixed TRUE if Math_Integer object is negative, FALSE if it is not, PEAR_Error on error.
  100.      * @access public
  101.      */
  102.     function isNegative(&$int{/*{{{*/
  103.         $err Math_IntegerOp::_validInt($int);
  104.         if (PEAR::isError($err)) {
  105.             return $err;
  106.         }
  107.         return $int->isNegative();
  108.     }/*}}}*/
  109.  
  110.     /**
  111.      * Checks if the Math_Integer object is zero
  112.      *
  113.      * @param object Math_Integer $int1 
  114.      * @return mixed TRUE if Math_Integer object is zero, FALSE if it is not, PEAR_Error on error.
  115.      * @access public
  116.      */
  117.     function isZero(&$int{/*{{{*/
  118.         $err Math_IntegerOp::_validInt($int);
  119.         if (PEAR::isError($err)) {
  120.             return $err;
  121.         }
  122.         return $int->isZero();
  123.     }/*}}}*/
  124.  
  125.     /**
  126.      * Add two Math_Integer objects: $i1 + $i2
  127.      *
  128.      * @param object Math_Integer $int1 
  129.      * @param object Math_Integer $int2 
  130.      * @return object Math_Integer on success, PEAR_Error otherwise
  131.      * @access public
  132.      */
  133.     function &add(&$int1&$int2{/*{{{*/
  134.         if (PEAR::isError($err Math_IntegerOp::_validInts($int1$int2))) {
  135.             return $err;
  136.         }
  137.         $res $int1->makeClone();
  138.         $err $res->add($int2);
  139.         if (PEAR::isError($err)) {
  140.             return $err;
  141.         }
  142.         return $res;
  143.     }/*}}}*/
  144.  
  145.     /**
  146.      * Substract two Math_Integer objects: $i1 - $i2
  147.      *
  148.      * @param object Math_Integer $int1 
  149.      * @param object Math_Integer $int2 
  150.      * @return object Math_Integer on success, PEAR_Error otherwise
  151.      * @access public
  152.      */
  153.     function &sub(&$int1&$int2{/*{{{*/
  154.         if (PEAR::isError($err Math_IntegerOp::_validInts($int1$int2))) {
  155.             return $err;
  156.         }
  157.         $res $int1->makeClone();
  158.         $err $res->sub($int2);
  159.         if (PEAR::isError($err)) {
  160.             return $err;
  161.         }
  162.         return $res;
  163.     }/*}}}*/
  164.  
  165.     /**
  166.      * Multiply two Math_Integer objects: $i1 * $i2
  167.      *
  168.      * @param object Math_Integer $int1 
  169.      * @param object Math_Integer $int2 
  170.      * @return object Math_Integer on success, PEAR_Error otherwise
  171.      * @access public
  172.      */
  173.     function &mul(&$int1&$int2{/*{{{*/
  174.         if (PEAR::isError($err Math_IntegerOp::_validInts($int1$int2))) {
  175.             return $err;
  176.         }
  177.         $res $int1->makeClone();
  178.         $err $res->mul($int2);
  179.         if (PEAR::isError($err)) {
  180.             return $err;
  181.         }
  182.         return $res;
  183.     }/*}}}*/
  184.  
  185.     /**
  186.      * Divide two Math_Integer objects: $i1 / $i2
  187.      *
  188.      * @param object Math_Integer $int1 
  189.      * @param object Math_Integer $int2 
  190.      * @return object Math_Integer on success, PEAR_Error otherwise
  191.      * @access public
  192.      */
  193.     function &div(&$int1&$int2{/*{{{*/
  194.         if (PEAR::isError($err Math_IntegerOp::_validInts($int1$int2))) {
  195.             return $err;
  196.         }
  197.         $res $int1->makeClone();
  198.         $err $res->div($int2);
  199.         if (PEAR::isError($err)) {
  200.             return $err;
  201.         }
  202.         return $res;
  203.     }/*}}}*/
  204.  
  205.     /**
  206.      * Calculate the modulus of $i1 and $i2: $i1 % $i2
  207.      *
  208.      * @param object Math_Integer $int1 
  209.      * @param object Math_Integer $int2 
  210.      * @return object Math_Integer on success, PEAR_Error otherwise
  211.      * @access public
  212.      */
  213.     function &mod(&$int1&$int2{/*{{{*/
  214.         if (PEAR::isError($err Math_IntegerOp::_validInts($int1$int2))) {
  215.             return $err;
  216.         }
  217.         $res $int1->makeClone();
  218.         $err $res->mod($int2);
  219.         if (PEAR::isError($err)) {
  220.             return $err;
  221.         }
  222.         return $res;
  223.     }/*}}}*/
  224.  
  225.     /**
  226.      * Raise $i1 to the $i2 exponent: $i1^$i2
  227.      *
  228.      * @param object Math_Integer $int1 
  229.      * @param object Math_Integer $int2 
  230.      * @return object Math_Integer on success, PEAR_Error otherwise
  231.      * @access public
  232.      */
  233.     function &pow(&$int1&$int2{/*{{{*/
  234.         if (PEAR::isError($err Math_IntegerOp::_validInts($int1$int2))) {
  235.             return $err;
  236.         }
  237.         $res $int1->makeClone();
  238.         $err $res->pow($int2);
  239.         if (PEAR::isError($err)) {
  240.             return $err;
  241.         }
  242.         return $res;
  243.     }/*}}}*/
  244.  
  245.     /**
  246.      * Calculates the GCD of 2 Math_Integer objects
  247.      *
  248.      * @param object Math_Integer $int1 
  249.      * @param object Math_Integer $int2 
  250.      * @return mixed and integer on success, PEAR_Error otherwise
  251.      * @access public
  252.      * @see Math_IntegerOp::sign
  253.      */
  254.     function &gcd(&$int1&$int2{/*{{{*/
  255.         if (PEAR::isError($err Math_IntegerOp::_validInts($int1$int2))) {
  256.             return $err;
  257.         }
  258.         return $int1->gcd($int2);
  259.     }/*}}}*/
  260.  
  261.     /**
  262.      * Compare two Math_Integer objects.
  263.      * if $i1 > $i2, returns +1,
  264.      * if $i1 == $i2, returns +0,
  265.      * if $i1 < $i2, returns -1,
  266.      *
  267.      * @param object Math_Integer $int1 
  268.      * @param object Math_Integer $int2 
  269.      * @return mixed and integer on success, PEAR_Error otherwise
  270.      * @access public
  271.      * @see Math_IntegerOp::sign
  272.      */
  273.     function &compare(&$int1&$int2{/*{{{*/
  274.         if (PEAR::isError($err Math_IntegerOp::_validInts($int1$int2))) {
  275.             return $err;
  276.         }
  277.         return $int1->compare($int2);
  278.     }/*}}}*/
  279.  
  280.     /**
  281.      * Returns the sign of a Math_Integer number
  282.      * if $i1 > 0, returns +1,
  283.      * if $i1 == 0, returns +0,
  284.      * if $i1 < 0, returns -1,
  285.      *
  286.      * @param object Math_Integer $int1 
  287.      * @return mixed and integer on success, PEAR_Error otherwise
  288.      * @access public
  289.      */
  290.     function &sign(&$int1{/*{{{*/
  291.         if (PEAR::isError($err Math_IntegerOp::_validInt($int1))) {
  292.             return $err;
  293.         }
  294.         return $int1->sign();
  295.     }/*}}}*/
  296.  
  297.     /**
  298.      * Returns the negative of a Math_Integer number: -1 * $i1
  299.      *
  300.      * @param object Math_Integer $int1 
  301.      * @return object Math_Integer on success, PEAR_Error otherwise
  302.      * @access public
  303.      */
  304.     function &negate(&$int1{/*{{{*/
  305.         if (PEAR::isError($err Math_IntegerOp::_validInt($int1))) {
  306.             return $err;
  307.         }
  308.         $res $int1->makeClone();
  309.         $err $res->negate();
  310.         if (PEAR::isError($err)) {
  311.             return $err;
  312.         }
  313.         return $res;
  314.     }/*}}}*/
  315.  
  316.     /**
  317.      * Returns the (integer) square root of a Math_Integer number
  318.      *
  319.      * @param object Math_Integer $int1 
  320.      * @return object Math_Integer on success, PEAR_Error otherwise
  321.      * @access public
  322.      */
  323.     function &sqrt(&$int1{/*{{{*/
  324.         if (PEAR::isError($err Math_IntegerOp::_validInt($int1))) {
  325.             return $err;
  326.         }
  327.         $res $int1->makeClone();
  328.         $err $res->sqrt();
  329.         if (PEAR::isError($err)) {
  330.             return $err;
  331.         }
  332.         return $res;
  333.     }/*}}}*/
  334.  
  335.     /**
  336.      * Returns the absolute value of a Math_Integer number
  337.      *
  338.      * @param object Math_Integer $int1 
  339.      * @return object Math_Integer on success, PEAR_Error otherwise
  340.      * @access public
  341.      */
  342.     function &abs(&$int1{/*{{{*/
  343.         if (PEAR::isError($err Math_IntegerOp::_validInt($int1))) {
  344.             return $err;
  345.         }
  346.         $res $int1->makeClone();
  347.         $err $res->abs();
  348.         if (PEAR::isError($err)) {
  349.             return $err;
  350.         }
  351.         return $res;
  352.     }/*}}}*/
  353.  
  354.     /**
  355.      * Checks that the 2 passed objects are valid Math_Integer numbers.
  356.      * The objects must be instances of Math_Integer and have been properly
  357.      * initialized.
  358.      *
  359.      * @param object Math_Integer $int1 
  360.      * @param object Math_Integer $int2 
  361.      * @return mixed TRUE if both are Math_Integer objects, PEAR_Error otherwise
  362.      * @access private
  363.      */
  364.     function _validInts(&$int1&$int2{/*{{{*/
  365.         $err1 Math_IntegerOp::_validInt($int1);
  366.         $err2 Math_IntegerOp::_validInt($int2);
  367.         $error '';
  368.         if (PEAR::isError($err1)) {
  369.             $error .= 'First parameter: '.$err1->getMessage();
  370.         }
  371.         if (PEAR::isError($err2)) {
  372.             $error .= ' Second parameter: '.$err2->getMessage();
  373.         }
  374.         if (!empty($error)) {
  375.             return PEAR::raiseError($error);
  376.         else {
  377.             return true;
  378.         }
  379.     }/*}}}*/
  380.  
  381.     /**
  382.      * Checks that the passed object is a valid Math_Integer number.
  383.      * The object must be an instance of Math_Integer and have been properly
  384.      * initialized.
  385.      *
  386.      * @param object Math_Integer $int1 
  387.      * @return mixed TRUE if is a Math_Integer object, PEAR_Error otherwise
  388.      * @access private
  389.      */
  390.     function _validInt(&$int1{/*{{{*/
  391.         $error '';
  392.         if (!Math_IntegerOp::isInteger($int1)) {
  393.             $error 'Is not an Integer object.';
  394.         elseif (is_null($int1->getValue())) {
  395.             $error 'Integer object is uninitalized.';
  396.         }
  397.         if (!empty($error)) {
  398.             return PEAR::raiseError($error);
  399.         else {
  400.             return true;
  401.         }
  402.     }/*}}}*/
  403. }/*}}} end of Math_IntegerOp */
  404.  
  405. // vim: ts=4:sw=4:et:
  406. // vim6: fdl=1:
  407. ?>

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