home *** CD-ROM | disk | FTP | other *** search
/ PC World 2003 March / PCWorld_2003-03_cd.bin / Software / Vyzkuste / phptriad / phptriad2-2-1.exe / php / pear / Math / Fraction.php next >
Encoding:
PHP Script  |  2001-11-13  |  6.6 KB  |  238 lines

  1. <?php
  2. //
  3. // +----------------------------------------------------------------------+
  4. // | PHP version 4.0                                                      |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2001 The PHP Group                                |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.02 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: Sebastian Bergmann <sb@sebastian-bergmann.de>               |
  17. // +----------------------------------------------------------------------+
  18. //
  19. // $Id: Fraction.php,v 1.3.2.3 2001/11/13 01:26:47 ssb Exp $
  20. //
  21.  
  22. /**
  23. * Math::Math_Fraction
  24. * Purpose:
  25. *   Class for handling fractions.
  26. * Example:
  27. *   $a = new Math_Fraction( 1, 2 );
  28. *   $b = new Math_Fraction( 3, 4 );
  29. *   $a->add( $b );
  30. * @author   Sebastian Bergmann <sb@sebastian-bergmann.de>
  31. * @version  $Revision: 1.3.2.3 $
  32. * @access   public
  33. * @package  Numbers
  34. */
  35.  
  36. require_once 'Math/Util.php';
  37.  
  38. class Math_Fraction
  39. {
  40.     /**
  41.     * numerator of the fraction
  42.     *
  43.     * @var    int numerator
  44.     * @access public
  45.     */
  46.  
  47.     var $numerator;
  48.  
  49.     /**
  50.     * denominator of the fraction
  51.     *
  52.     * @var    int denominator
  53.     * @access public
  54.     */
  55.  
  56.     var $denominator;
  57.  
  58.     /**
  59.     * Constructor
  60.     *
  61.     * @param  int numerator
  62.     * @param  int denominator
  63.     * @access public
  64.     */
  65.  
  66.     function Math_Fraction($numerator, $denominator = 1)
  67.     {
  68.         $this->numerator   = $numerator;
  69.         $this->denominator = $denominator;
  70.     }
  71.  
  72.     /**
  73.     * Add another fraction to this one.
  74.     *
  75.     * @param  Math_Fraction fraction              
  76.     * @param  boolean       overwrite_with_result 
  77.     * @return Math_Fraction result (if overwrite_with_result == false)
  78.     * @access public
  79.     * @see    sub, mul, div
  80.     */
  81.  
  82.     function add($fraction, $overwrite_with_result = true)
  83.     {
  84.         $fraction = $this->_check_fraction($fraction);
  85.  
  86.         $result = new Math_Fraction(($this->numerator   + $fraction->numerator), 
  87.                                     ($this->denominator * $fraction->denominator)
  88.                                    );
  89.  
  90.         return $this->_return($result, $overwrite_with_result);
  91.     }
  92.  
  93.     /**
  94.     * Subtract another fraction from this one.
  95.     *
  96.     * @param  Math_Fraction fraction              
  97.     * @param  boolean       overwrite_with_result 
  98.     * @return Math_Fraction result (if overwrite_with_result == false)
  99.     * @access public
  100.     * @see    add, mul, div
  101.     */
  102.  
  103.     function sub($fraction, $overwrite_with_result = true)
  104.     {
  105.         $fraction = $this->_check_fraction($fraction);
  106.  
  107.         $result = new Math_Fraction(($this->numerator   - $fraction->numerator), 
  108.                                     ($this->denominator * $fraction->denominator)
  109.                                    );
  110.  
  111.         return $this->_return($result, $overwrite_with_result);
  112.     }
  113.  
  114.     /**
  115.     * Multiply another fraction with this one.
  116.     *
  117.     * @param  Math_Fraction fraction              
  118.     * @param  boolean       overwrite_with_result 
  119.     * @return Math_Fraction result (if overwrite_with_result == false)
  120.     * @access public
  121.     * @see    add, sub, div
  122.     */
  123.  
  124.     function mul($fraction, $overwrite_with_result = true)
  125.     {
  126.         $fraction = $this->_check_fraction( $fraction );
  127.  
  128.         $result = new Math_Fraction(($this->numerator   * $fraction->numerator), 
  129.                                     ($this->denominator * $fraction->denominator)
  130.                                    );
  131.  
  132.         return $this->_return($result, $overwrite_with_result);
  133.     }
  134.  
  135.     /**
  136.     * Divide this fraction by another one.
  137.     *
  138.     * @param  Math_Fraction fraction              
  139.     * @param  boolean       overwrite_with_result 
  140.     * @return Math_Fraction result (if overwrite_with_result == false)
  141.     * @access public
  142.     * @see    add, sub, mul
  143.     */
  144.  
  145.     function div($fraction, $overwrite_with_result = true)
  146.     {
  147.         $fraction = $this->_check_fraction( $fraction );
  148.  
  149.         $result = new Math_Fraction(($this->numerator   * $fraction->denominator), 
  150.                                     ($this->denominator * $fraction->numerator)
  151.                                    );
  152.  
  153.         return $this->_return($result, $overwrite_with_result);
  154.     }
  155.  
  156.     /**
  157.     * Normalize this fraction.
  158.     *
  159.     * @access public
  160.     */
  161.  
  162.     function normalize()
  163.     {
  164.         $gcd = Math_Util::gcd($this->numerator, $this->denominator);
  165.  
  166.         $this->numerator   = $this->numerator   / $gcd;
  167.         $this->denominator = $this->denominator / $gcd;
  168.     }
  169.  
  170.     /**
  171.     * Dump this fraction.
  172.     *
  173.     * @access public
  174.     */
  175.  
  176.     function dump()
  177.     {
  178.         echo $this->get();
  179.     }
  180.  
  181.     /**
  182.     * Get string representation of this fraction.
  183.     *
  184.     * @return string  representation of fraction
  185.     * @access public
  186.     */
  187.  
  188.     function get()
  189.     {
  190.         return $this->numerator . ' / ' . $this->denominator;
  191.     }
  192.  
  193.     /**
  194.     * Check, if a varaible holds a Math_Fraction object.
  195.     *
  196.     * @param  Math_Fraction fraction to be checked
  197.     * @return Math_Fraction checked fraction
  198.     * @access private
  199.     */
  200.  
  201.     function _check_fraction($fraction)
  202.     {
  203.         if (get_class($fraction) != 'math_fraction') {
  204.           $fraction = new Math_Fraction( $fraction );
  205.         }
  206.  
  207.         return $fraction;
  208.     }
  209.  
  210.     /**
  211.     * Handle the return or storage of a result from add, sub, mul or div.
  212.     *
  213.     * @param  Math_Fraction  result
  214.     * @param  boolean        overwrite_with_result
  215.     * @return Math_Fraction result (if overwrite_with_result == false)
  216.     * @access private
  217.     */
  218.  
  219.     function _return($result, $overwrite_with_result)
  220.     {
  221.         $result->normalize();
  222.  
  223.         if ($overwrite_with_result) {
  224.           $this->numerator   = $result->numerator;
  225.           $this->denominator = $result->denominator;
  226.         } else {
  227.           return $result;
  228.         }
  229.     }
  230. }
  231. ?>
  232.