home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Servidores / xampp-win32-1.6.7-installer.exe / php / PEAR / Math / Fraction.php < prev    next >
Encoding:
PHP Script  |  2008-07-02  |  4.5 KB  |  153 lines

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3. // +----------------------------------------------------------------------+
  4. // | PHP version 4                                                        |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2004 The PHP Group                                |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 3.0 of the PHP license,       |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available through the world-wide-web at the following url:           |
  11. // | http://www.php.net/license/3_0.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: Kouber Saparev <kouber@php.net>                             |
  17. // +----------------------------------------------------------------------+
  18. //
  19. // $Id: Fraction.php,v 1.3 2007/07/18 14:53:31 kouber Exp $
  20.  
  21. /**
  22.  * Math_Fraction: class to represent and manipulate fractions (n = a/b)
  23.  *
  24.  *
  25.  * @author  Kouber Saparev <kouber@php.net>
  26.  * @version 0.4.1
  27.  * @access  public
  28.  * @package Math_Fraction
  29.  */
  30. class Math_Fraction {
  31.  
  32.     /**
  33.      * The numerator of the fraction
  34.      *
  35.      * @var int
  36.      * @access  private
  37.      */
  38.     var $_num;
  39.  
  40.     /**
  41.      * The denominator of the fraction
  42.      *
  43.      * @var int
  44.      * @access  private
  45.      */
  46.     var $_den;
  47.     
  48.     /**
  49.      * Constructor for Math_Fraction
  50.      * 
  51.      * @param mixed $num Integer for the Numerator or a Float that the fraction will be built from.
  52.      * @param int $den Denominator
  53.      * @return object Math_Fraction
  54.      * @access public
  55.      */
  56.     function Math_Fraction($num, $den = null)
  57.     {   
  58.         if (is_float($num)) {
  59.             // the fraction is built from a float
  60.             // signature = (float)
  61.             $fr =& Math_FractionOp::floatToFraction($num);
  62.  
  63.             $this->_num =& $fr->getNum();
  64.             $this->_den =& $fr->getDen();
  65.         } else {
  66.             // classical construction with numerator and denominator
  67.             // signature = (int, int)
  68.             if (is_null($den)) {
  69.                 // just one parameter is passed to the constructor
  70.  
  71.                 if (is_int($num)) {
  72.                     // invalid signature = (int)
  73.                     return Math_FractionOp::raiseError('Denominator missing.');
  74.                 } else {
  75.                     // try to create a fraction from string
  76.                     // signature = (string)
  77.                     $fr =& Math_FractionOp::stringToFraction($num);
  78.  
  79.                     if (!Math_FractionOp::isFraction($fr)) {
  80.                         return $fr;
  81.                     }
  82.  
  83.                     $this->_num =& $fr->getNum();
  84.                     $this->_den =& $fr->getDen();
  85.                 }
  86.             }
  87.  
  88.             $num = intval($num);
  89.             $den = intval($den);
  90.  
  91.             if (!$den) {
  92.                 return Math_FractionOp::raiseError('Denominator must not be zero.');
  93.             }
  94.  
  95.             if ($den < 0) {
  96.                 // denominator is negative => set the sign of the entire fraction
  97.                 $num *= -1;
  98.                 $den *= -1;
  99.             }
  100.  
  101.             $this->_num = $num;
  102.             $this->_den = $den;
  103.         }
  104.     }
  105.     
  106.     /**
  107.      * Returns the numerator of the fraction
  108.      *
  109.      * @return int
  110.      * @access public
  111.      */
  112.     function getNum()
  113.     {
  114.         return $this->_num;
  115.     }
  116.  
  117.     /**
  118.      * Returns the denominator of the fraction
  119.      * @return int
  120.      * @access public
  121.      */
  122.     function getDen()
  123.     {
  124.         return $this->_den;
  125.     }
  126.  
  127.     /**
  128.      * Float evaluation of the fraction
  129.      *
  130.      * @return float
  131.      * @access public
  132.      */
  133.     function toFloat()
  134.     {
  135.         $n = $this->getNum();
  136.         $d = $this->getDen();
  137.         return floatval($n / $d);
  138.     }
  139.  
  140.     /**
  141.      * String representation of the fraction
  142.      *
  143.      * @return string
  144.      * @access public
  145.      */
  146.     function toString()
  147.     {
  148.         $n = $this->getNum();
  149.         $d = $this->getDen();
  150.         return "$n/$d";
  151.     }
  152. }
  153. ?>