| // +----------------------------------------------------------------------+ // // $Id:$ include_once "PEAR.php"; /** * Math_Fraction: class to represent and manipulate fractions (n = a/b) * * * @author Kouber Saparev * @version 0.3 * @access public * @package Math_Fraction */ class Math_Fraction {/*{{{*/ /** * The numerator of the fraction * * @var int * @access private */ var $_num; /** * The denominator of the fraction * * @var int * @access private */ var $_den; /*{{{ Math_Fraction() */ /** * Constructor for Math_Fraction * * @param mixed $num Integer for the Numerator or a Float that the fraction will be built from. * @param int $den Denominator * @return object Math_Fraction * @access public */ function Math_Fraction($num, $den=null) { if (is_float($num)) { // the fraction is built from a float // signature = (float) $fr =& Math_FractionOp::floatToFraction($num); $this->_num =& $fr->getNum(); $this->_den =& $fr->getDen(); } else { // classical construction with numerator and denominator // signature = (int, int) if (is_null($den)) { // invalid signature = (int) return PEAR::raiseError('Denominator missing.'); } $num = intval($num); $den = intval($den); if (!$den) { return PEAR::raiseError('Denominator must not be zero.'); } $this->_num = intval($num); $this->_den = intval($den); } }/*}}}*/ /*{{{ getNum() */ /** * Returns the numerator of the fraction * * @return int * @access public */ function getNum() { return $this->_num; }/*}}}*/ /*{{{ getDen() */ /** * Returns the denominator of the fraction * @return int * @access public */ function getDen() { return $this->_den; }/*}}}*/ /*{{{ toFloat() */ /** * Float evaluation of the fraction * * @return float * @access public */ function toFloat() { $n = $this->getNum(); $d = $this->getDen(); return floatval($n/$d); }/*}}}*/ /*{{{ toString() */ /** * String representation of the fraction * * @return string * @access public */ function toString() { $n = $this->getNum(); $d = $this->getDen(); return "$n/$d"; }/*}}}*/ } /* end of Math_Fraction class *//*}}}*/ ?>