Source for file FractionOp.php
Documentation is available at FractionOp.php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
// +----------------------------------------------------------------------+
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2004 The PHP Group |
// +----------------------------------------------------------------------+
// | This source file is subject to version 3.0 of the PHP license, |
// | that is bundled with this package in the file LICENSE, and is |
// | available through the world-wide-web at the following url: |
// | http://www.php.net/license/3_0.txt. |
// | If you did not receive a copy of the PHP license and are unable to |
// | obtain it through the world-wide-web, please send a note to |
// | license@php.net so we can mail you a copy immediately. |
// +----------------------------------------------------------------------+
// | Authors: Kouber Saparev <kouber@php.net> |
// +----------------------------------------------------------------------+
// $Id: FractionOp.php,v 1.4 2007/07/18 14:53:31 kouber Exp $
include_once 'Math/Fraction.php';
* Math_FractionOp: static class to operate on Math_Fraction objects
* @author Kouber Saparev <kouber@php.net>
* Checks if a given object is an instance of PEAR::Math_Fraction
return is_a($n, 'math_fraction');
* Compares two fractions.
* if $n1 > $n2, returns 1,
* if $n1 == $n2, returns 0,
* if $n1 < $n2, returns -1
* @param object $n1 Math_Fraction
* @param object $n2 Math_Fraction
$f1 = $num1 * $lcm/ $den1;
$f2 = $num2 * $lcm/ $den2;
* Returns the sum of two fractions: n = n1 + n2
* @param object $n1 Math_Fraction
* @param object $n2 Math_Fraction
* @return object Math_Fraction
function add($n1, $n2, $return_simplified = true )
$num = $n1->getNum () * $lcm/ $den1 + $n2->getNum () * $lcm/ $den2;
if ($return_simplified) {
* Returns the subtraction of two fractions: n = n1 - n2
* @param object $n1 Math_Fraction
* @param object $n2 Math_Fraction
* @return object Math_Fraction
function sub($n1, $n2, $return_simplified = true )
* Returns the product of two fractions: n = n1 * n2
* @param object $n1 Math_Fraction
* @param object $n2 Math_Fraction
* @return object Math_Fraction
function mult($n1, $n2, $return_simplified = true )
$num = $n1->getNum () * $n2->getNum ();
$den = $n1->getDen () * $n2->getDen ();
if ($return_simplified) {
* Returns the quotient of two fractions: n = n1 / n2
* @param object $n1 Math_Fraction
* @param object $n2 Math_Fraction
* @return object Math_Fraction
function div($n1, $n2, $return_simplified = true )
* Returns the greatest common divisor of two integers.
* Returns the least common multiple of two integers.
* Returns the reciprocal value of a fraction: n = 1/n
* @param object $n Math_Fraction
* @return object Math_Fraction
* Returns the simplified value (reduction) of a fraction.
* @param object $n Math_Fraction
* @return object Math_Fraction
* An alias of the Math_FractionOp::simplify() method.
* @param object $n Math_Fraction
* @return object Math_Fraction
* Converts float to fraction and try to keep the maximal possible precision.
* @return object Math_Fraction
// keep the original sign so that the numerator could be converted later
// get the part before the floating point
// make the float belonging to the interval [0, 1)
// strip the zero and the floating point
// try to get an integer for the numerator
$val = $int * pow(10 , $len) + $flt;
} while ($val > intval($val));
* Converts string to fraction.
* @return object Math_Fraction
if (preg_match('#^(-)? *?(\d+) *?/ *?(-)? *?(\d+)$#', trim($str), $m)) {
// there is one "-" sign => the fraction is negative
* An error capturing function.
* @return object PEAR::raiseError()
return PEAR ::raiseError ($str);
Documentation generated on Mon, 11 Mar 2019 15:07:36 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|