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

Source for file Fraction.php

Documentation is available at Fraction.php

  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:$
  20.  
  21. include_once 'PEAR.php';
  22.  
  23. /**
  24.  * Math_Fraction: class to represent and manipulate fractions (n = a/b)
  25.  *
  26.  *
  27.  * @author  Kouber Saparev <kouber@php.net>
  28.  * @version 0.3.0
  29.  * @access  public
  30.  * @package Math_Fraction
  31.  */
  32. class Math_Fraction {
  33.  
  34.     /**
  35.      * The numerator of the fraction
  36.      *
  37.      * @var int 
  38.      * @access  private
  39.      */
  40.     var $_num;
  41.  
  42.     /**
  43.      * The denominator of the fraction
  44.      *
  45.      * @var int 
  46.      * @access  private
  47.      */
  48.     var $_den;
  49.     
  50.     /**
  51.      * Constructor for Math_Fraction
  52.      * 
  53.      * @param mixed $num Integer for the Numerator or a Float that the fraction will be built from.
  54.      * @param int $den Denominator
  55.      * @return object Math_Fraction 
  56.      * @access public
  57.      */
  58.     function Math_Fraction($num$den = null)
  59.     {   
  60.         if (is_float($num)) {
  61.             // the fraction is built from a float
  62.             // signature = (float)
  63.             $fr =Math_FractionOp::floatToFraction($num);
  64.  
  65.             $this->_num =$fr->getNum();
  66.             $this->_den =$fr->getDen();
  67.         else {
  68.             // classical construction with numerator and denominator
  69.             // signature = (int, int)
  70.             if (is_null($den)) {
  71.                 // invalid signature = (int)
  72.                 return PEAR::raiseError('Denominator missing.');
  73.             }
  74.  
  75.             $num intval($num);
  76.             $den intval($den);
  77.  
  78.             if (!$den{
  79.                 return PEAR::raiseError('Denominator must not be zero.');
  80.             }
  81.  
  82.             $this->_num intval($num);
  83.             $this->_den intval($den);
  84.         }
  85.     }
  86.     
  87.     /**
  88.      * Returns the numerator of the fraction
  89.      *
  90.      * @return int 
  91.      * @access public
  92.      */
  93.     function getNum()
  94.     {
  95.         return $this->_num;
  96.     }
  97.  
  98.     /**
  99.      * Returns the denominator of the fraction
  100.      * @return int 
  101.      * @access public
  102.      */
  103.     function getDen()
  104.     {
  105.         return $this->_den;
  106.     }
  107.  
  108.     /**
  109.      * Float evaluation of the fraction
  110.      *
  111.      * @return float 
  112.      * @access public
  113.      */
  114.     function toFloat()
  115.     {
  116.         $n $this->getNum();
  117.         $d $this->getDen();
  118.         return floatval($n $d);
  119.     }
  120.  
  121.     /**
  122.      * String representation of the fraction
  123.      *
  124.      * @return string 
  125.      * @access public
  126.      */
  127.     function toString()
  128.     {
  129.         $n $this->getNum();
  130.         $d $this->getDen();
  131.         return "$n/$d";
  132.     }
  133. }
  134. ?>

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