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

Source for file FractionOp.php

Documentation is available at FractionOp.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. include_once 'Math/Fraction.php';
  23.  
  24. /**
  25.  * Math_FractionOp: static class to operate on Math_Fraction objects
  26.  *
  27.  * @author  Kouber Saparev <kouber@php.net>
  28.  * @version 0.3.0
  29.  * @access  public
  30.  * @package Math_Fraction
  31.  */
  32. class Math_FractionOp {
  33.  
  34.     /**
  35.      * Checks if a given object is an instance of PEAR::Math_Fraction
  36.      *
  37.      * @static
  38.      * @return boolean 
  39.      * @access public
  40.      */
  41.     function isFraction($n)
  42.     {
  43.         if (function_exists('is_a')) {
  44.             return is_a($n'math_fraction');
  45.         else {
  46.             return (strtolower(get_class($n)) == 'math_fraction' 
  47.                     || is_subclass_of($n'math_fraction'));
  48.         }
  49.     }
  50.  
  51.     /**
  52.      * Compares two fractions.
  53.      * if $n1 > $n2, returns 1,
  54.      * if $n1 == $n2, returns 0,
  55.      * if $n1 < $n2, returns -1
  56.      *
  57.      * @static
  58.      * @param object $n1 Math_Fraction
  59.      * @param object $n2 Math_Fraction
  60.      * @return int 
  61.      * @access public
  62.      */
  63.     function compare($n1$n2
  64.     {
  65.         if (!Math_FractionOp::isFraction($n1
  66.             || !Math_FractionOp::isFraction($n2)) {
  67.             return PEAR::raiseError('Both arguments must be PEAR::Math_Fraction objects');
  68.         else {
  69.             $num1 $n1->getNum();
  70.             $den1 $n1->getDen();
  71.  
  72.             $num2 $n2->getNum();
  73.             $den2 $n2->getDen();
  74.  
  75.             $lcm Math_FractionOp::lcm($den1$den2);
  76.  
  77.             $f1 $num1 $lcm/$den1;
  78.             $f2 $num2 $lcm/$den2;
  79.  
  80.             if ($f1 $f2{
  81.                 return -1;
  82.             else {
  83.                 return intval($f1 $f2);
  84.             }
  85.         }
  86.     }
  87.  
  88.     /**
  89.      * Returns the sum of two fractions: n = n1 + n2
  90.      *
  91.      * @static
  92.      * @param object $n1 Math_Fraction
  93.      * @param object $n2 Math_Fraction
  94.      * @return object Math_Fraction 
  95.      * @access public
  96.      */
  97.     function add($n1$n2$return_simplified = true
  98.     {
  99.         if (!Math_FractionOp::isFraction($n1
  100.             || !Math_FractionOp::isFraction($n2)) {
  101.             return PEAR::raiseError('Both arguments must be PEAR::Math_Fraction objects');
  102.         else {
  103.             $den1 $n1->getDen();
  104.             $den2 $n2->getDen();
  105.             $lcm Math_FractionOp::lcm($den1$den2);
  106.             $num $n1->getNum($lcm/$den1 $n2->getNum($lcm/$den2;
  107.             $f = new Math_Fraction(intval($num)$lcm);
  108.             if ($return_simplified{
  109.                 return Math_FractionOp::simplify($f);
  110.             else {
  111.                 return $f;
  112.             }
  113.         }
  114.     }
  115.  
  116.     /**
  117.      * Returns the substraction of two fractions: n = n1 - n2
  118.      *
  119.      * @static
  120.      * @param object $n1 Math_Fraction
  121.      * @param object $n2 Math_Fraction
  122.      * @return object Math_Fraction 
  123.      * @access public
  124.      */
  125.     function sub($n1$n2$return_simplified = true
  126.     {
  127.         if (!Math_FractionOp::isFraction($n1
  128.             || !Math_FractionOp::isFraction($n2)) {
  129.             return PEAR::raiseError('Both arguments must be PEAR::Math_Fraction objects');
  130.         else {
  131.             return Math_FractionOp::add($n1new Math_Fraction($n2->getNum()*-1$n2->getDen())$return_simplified);
  132.         }
  133.     }
  134.  
  135.     /**
  136.      * Returns the product of two fractions: n = n1 * n2
  137.      *
  138.      * @static
  139.      * @param object $n1 Math_Fraction
  140.      * @param object $n2 Math_Fraction
  141.      * @return object Math_Fraction 
  142.      * @access public
  143.      */
  144.     function mult($n1$n2$return_simplified = true
  145.     {
  146.         if (!Math_FractionOp::isFraction($n1
  147.             || !Math_FractionOp::isFraction($n2)) {
  148.             return PEAR::raiseError('Both arguments must be PEAR::Math_Fraction objects');
  149.         else {
  150.             $num $n1->getNum($n2->getNum();
  151.             $den $n1->getDen($n2->getDen();
  152.             $f = new Math_Fraction(intval($num)$den);
  153.             if ($return_simplified{
  154.                 return Math_FractionOp::simplify($f);
  155.             else {
  156.                 return $f;
  157.             }
  158.         }
  159.     }
  160.  
  161.     /**
  162.      * Returns the quotient of two fractions: n = n1 / n2
  163.      *
  164.      * @static
  165.      * @param object $n1 Math_Fraction
  166.      * @param object $n2 Math_Fraction
  167.      * @return object Math_Fraction 
  168.      * @access public
  169.      */
  170.     function div($n1$n2$return_simplified = true
  171.     {
  172.         if (!Math_FractionOp::isFraction($n1
  173.             || !Math_FractionOp::isFraction($n2)) {
  174.             return PEAR::raiseError('Both arguments must be PEAR::Math_Fraction objects');
  175.         else {
  176.             return Math_FractionOp::mult($n1Math_FractionOp::reciprocal($n2)$return_simplified);
  177.         }
  178.     }
  179.  
  180.     /**
  181.      * Returns the greatest common divisor of two integers.
  182.      *
  183.      * @static
  184.      * @param int $n1 
  185.      * @param int $n2 
  186.      * @return int 
  187.      * @access public
  188.      */
  189.     function gcd($n1$n2
  190.     {
  191.         $n1 intval($n1);
  192.         $n2 intval($n2);
  193.  
  194.         $n1 abs($n1);
  195.         $n2 abs($n2);
  196.  
  197.         if ($n1 $n2{
  198.             $t $n1;
  199.             $n1 $n2;
  200.             $n2 $t;
  201.         }
  202.  
  203.         while ($n2 != 0){
  204.             $t $n1 $n2;
  205.             $n1 $n2;
  206.             $n2 $t;
  207.         }
  208.  
  209.         return intval($n1);
  210.     }
  211.  
  212.     /**
  213.      * Returns the least common multiple of two integers.
  214.      *
  215.      * @static
  216.      * @param int $n1 
  217.      * @param int $n2 
  218.      * @return int 
  219.      * @access public
  220.      */
  221.     function lcm($n1$n2)
  222.     {
  223.         $n1 intval($n1);
  224.         $n2 intval($n2);
  225.  
  226.         $n1 abs($n1);
  227.         $n2 abs($n2);
  228.  
  229.         return intval(($n1 $n2Math_FractionOp::gcd($n1$n2));
  230.     }
  231.  
  232.     /**
  233.      * Returns the reciprocal value of a fraction: n = 1/n
  234.      *
  235.      * @static
  236.      * @param object $n Math_Fraction
  237.      * @return object Math_Fraction 
  238.      * @access public
  239.      */
  240.     function reciprocal($n
  241.     {
  242.         if (!Math_FractionOp::isFraction($n)) {
  243.             return PEAR::raiseError('Argument must be PEAR::Math_Fraction object');
  244.         else {
  245.             $num $n->getNum();
  246.             $den $n->getDen();
  247.             return new Math_Fraction($den$num);
  248.         }
  249.     }
  250.  
  251.     /**
  252.      * Returns the simplified value (reduction) of a fraction.
  253.      *
  254.      * @static
  255.      * @param object $n Math_Fraction
  256.      * @return object Math_Fraction 
  257.      * @access public
  258.      */
  259.     function &simplify(&$n)
  260.     {
  261.         if (!Math_FractionOp::isFraction($n)) {
  262.             return PEAR::raiseError('Argument must be PEAR::Math_Fraction object');
  263.         else {
  264.             $num $n->getNum();
  265.             $den $n->getDen();
  266.             $gcd Math_FractionOp::gcd($num$den);
  267.             if ($gcd > 1{
  268.                 return new Math_Fraction($num/$gcd$den/$gcd);
  269.             else {
  270.                 return $n;
  271.             }
  272.         }
  273.     }
  274.  
  275.     /**
  276.      * An alias of the Math_FractionOp::simplify() method.
  277.      *
  278.      * @static
  279.      * @param object $n Math_Fraction
  280.      * @return object Math_Fraction 
  281.      * @access public
  282.      */
  283.     function &reduce(&$n)
  284.     {
  285.         return Math_FractionOp::simplify($n);
  286.     }
  287.  
  288.     /**
  289.      * Converts float to fraction and try to keep the maximal possible precision.
  290.      *
  291.      * @static
  292.      * @param float $f 
  293.      * @return object Math_Fraction 
  294.      * @access public
  295.      */
  296.     function floatToFraction($f)
  297.     {
  298.         $f floatval($f);
  299.  
  300.         // keep the original sign so that the numerator could be converted later
  301.         $is_negative ($f < 0);
  302.         if ($is_negative{
  303.             $f *= -1;
  304.         }
  305.  
  306.         // get the part before the floating point
  307.         $int floor($f);
  308.  
  309.         // make the float belonging to the interval [0, 1)
  310.         $flt $f $int;
  311.  
  312.         // strip the zero and the floating point
  313.         $flt substr($flt2);
  314.  
  315.         // try to get an integer for the numerator
  316.         do {
  317.             $len strlen($flt);
  318.             $val $int pow(10$len$flt;
  319.             $flt substr($flt0-1);
  320.         while ($val intval($val));
  321.  
  322.         if ($is_negative{
  323.             $val *= -1;
  324.         }
  325.  
  326.         $num intval($val);
  327.         $den pow(10$len);
  328.  
  329.         return new Math_Fraction($num$den);
  330.     }
  331. }
  332. ?>

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