cvs: php4 /pear/Benchmark Iterate.php Timer.php /pear/Math Fraction.php Util.php
| From: | Sebastian Bergmann | Date: | Mon, 23 Jul 2001 09:57:53 +0000 |
| Subject: | cvs: php4 /pear/Benchmark Iterate.php Timer.php /pear/Math Fraction.php Util.php | ||
| Groups: | php.pear.cvs | ||
| Request: | Send a blank email to pear-cvs+get-331@lists.php.net to get a copy of this message | ||
sbergmann Mon Jul 23 05:57:53 2001 EDT
Modified files:
/php4/pear/Benchmark Iterate.php Timer.php
/php4/pear/Math Fraction.php Util.php
Log:
Make Math_Util a static class, synch Math_Fraction accordingly. Some whitespace fixes.
Index: php4/pear/Benchmark/Iterate.php diff -u php4/pear/Benchmark/Iterate.php:1.6 php4/pear/Benchmark/Iterate.php:1.7 --- php4/pear/Benchmark/Iterate.php:1.6 Sat Mar 3 01:36:44 2001 +++ php4/pear/Benchmark/Iterate.php Mon Jul 23 05:57:52 2001 @@ -16,7 +16,7 @@ // | Authors: Sebastian Bergmann <sb@sebastian-bergmann.de> | // +----------------------------------------------------------------------+ // -// $Id: Iterate.php,v 1.6 2001/03/03 06:36:44 sbergmann Exp $ +// $Id: Iterate.php,v 1.7 2001/07/23 09:57:52 sbergmann Exp $ // require_once 'Benchmark/Timer.php'; @@ -42,44 +42,32 @@ * $result = $benchmark->get(); * * @author Sebastian Bergmann <sb@sebastian-bergmann.de> -* @version $Revision: 1.6 $ +* @version $Revision: 1.7 $ * @access public */ -class Benchmark_Iterate extends Benchmark_Timer -{ - // {{{ run() - +class Benchmark_Iterate extends Benchmark_Timer { /** * Benchmarks a function. * * @access public */ - function run() - { - // get arguments - $arguments = func_get_args(); - $iterations = array_shift($arguments); + function run() { + $arguments = func_get_args(); + + $iterations = array_shift($arguments); $function_name = array_shift($arguments); - // main loop - for ($i = 1; $i <= $iterations; $i++) - { - // set 'start' marker for current iteration + for ($i = 1; $i <= $iterations; $i++) { $this->setMarker('start_'.$i); - // call function to be benchmarked call_user_func_array($function_name, $arguments); - // set 'end' marker for current iteration $this->setMarker('end_'.$i); } } - // }}} - // {{{ get() - /** * Returns benchmark result. * @@ -91,47 +79,33 @@ * @access public */ - function get() - { - // init result array + function get() { $result = array(); - - // init variable - $total = 0; + $total = 0; $iterations = count($this->markers)/2; - // loop through iterations - for ($i = 1; $i <= $iterations; $i++) - { - // get elapsed time for current iteration + for ($i = 1; $i <= $iterations; $i++) { $time = $this->timeElapsed('start_'.$i , 'end_'.$i); - // sum up total time spent if (extension_loaded('bcmath')) { $total = bcadd($total, $time, 6); } else { $total = $total + $time; } - // store time $result[$i] = $time; } - // calculate and store mean time if (extension_loaded('bcmath')) { $result['mean'] = bcdiv($total, $iterations, 6); } else { $result['mean'] = $total / $iterations; } - // store iterations $result['iterations'] = $iterations; - // return result array return $result; } - - // }}} } ?> Index: php4/pear/Benchmark/Timer.php diff -u php4/pear/Benchmark/Timer.php:1.8 php4/pear/Benchmark/Timer.php:1.9 --- php4/pear/Benchmark/Timer.php:1.8 Sat Mar 3 01:35:25 2001 +++ php4/pear/Benchmark/Timer.php Mon Jul 23 05:57:52 2001 @@ -16,7 +16,7 @@ // | Authors: Sebastian Bergmann <sb@sebastian-bergmann.de> | // +----------------------------------------------------------------------+ // -// $Id: Timer.php,v 1.8 2001/03/03 06:35:25 sbergmann Exp $ +// $Id: Timer.php,v 1.9 2001/07/23 09:57:52 sbergmann Exp $ // /** @@ -37,11 +37,11 @@ * $profiling = $timer->getProfiling(); * * @author Sebastian Bergmann <sb@sebastian-bergmann.de> - * @version $Revision: 1.8 $ + * @version $Revision: 1.9 $ * @access public */ -class Benchmark_Timer -{ + +class Benchmark_Timer { /** * Contains the markers * @@ -56,8 +56,7 @@ * @see setMarker(), stop() * @access public */ - function start() - { + function start() { $this->setMarker('Start'); } @@ -67,8 +66,7 @@ * @see setMarker(), start() * @access public */ - function stop() - { + function stop() { $this->setMarker('Stop'); } @@ -79,8 +77,7 @@ * @see start(), stop() * @access public */ - function setMarker($name) - { + function setMarker($name) { $microtime = explode(' ', microtime()); $this->markers[$name] = $microtime[1] . substr($microtime[0], 1); } @@ -93,8 +90,7 @@ * @return double $time_elapsed time elapsed between $start and $end * @access public */ - function timeElapsed($start = 'Start', $end = 'Stop') - { + function timeElapsed($start = 'Start', $end = 'Stop') { if (extension_loaded('bcmath')) { return bcsub($this->markers[$end], $this->markers[$start], 6); } else { @@ -113,8 +109,7 @@ * @return array $profiling * @access public */ - function getProfiling() - { + function getProfiling() { $i = 0; $total = 0; $result = array(); Index: php4/pear/Math/Fraction.php diff -u php4/pear/Math/Fraction.php:1.2 php4/pear/Math/Fraction.php:1.3 --- php4/pear/Math/Fraction.php:1.2 Tue Jan 9 20:01:56 2001 +++ php4/pear/Math/Fraction.php Mon Jul 23 05:57:52 2001 @@ -16,72 +16,62 @@ // | Authors: Sebastian Bergmann <sb@sebastian-bergmann.de> | // +----------------------------------------------------------------------+ // -// $Id: Fraction.php,v 1.2 2001/01/10 01:01:56 ssb Exp $ +// $Id: Fraction.php,v 1.3 2001/07/23 09:57:52 sbergmann Exp $ // - /** - * Math::Math_Fraction - * - * Purpose: - * - * Class for handling fractions. - * - * Example: - * - * $a = new Math_Fraction( 1, 2 ); - * $b = new Math_Fraction( 3, 4 ); - * - * $a->add( $b ); - * - * @author Sebastian Bergmann <sb@sebastian-bergmann.de> - * @version $Revision: 1.2 $ - * @access public - */ - - require_once "Math/Util.php"; - - class Math_Fraction - { - // {{{ properties +/** +* Math::Math_Fraction +* +* Purpose: +* +* Class for handling fractions. +* +* Example: +* +* $a = new Math_Fraction( 1, 2 ); +* $b = new Math_Fraction( 3, 4 ); +* +* $a->add( $b ); +* +* @author Sebastian Bergmann <sb@sebastian-bergmann.de> +* @version $Revision: 1.3 $ +* @access public +*/ +require_once 'Math/Util.php'; + +class Math_Fraction { /** - * zaehler of the fraction + * numerator of the fraction * - * @var int zaehler + * @var int numerator * @access public */ - var $zaehler; + var $numerator; /** - * nenner of the fraction + * denominator of the fraction * - * @var int nenner + * @var int denominator * @access public */ - var $nenner; - - // }}} - // {{{ Math_Fraction( $zaehler, $nenner ) + var $denominator; /** - * Constructor. + * Constructor * - * @param int zaehler - * @param int nenner + * @param int numerator + * @param int denominator * @access public */ - function Math_Fraction( $zaehler, $nenner = 1 ) - { - $this->zaehler = $zaehler; - $this->nenner = $nenner; + function Math_Fraction($numerator, $denominator = 1) { + $this->numerator = $numerator; + $this->denominator = $denominator; } - // }}} - // {{{ add( $fraction, $overwrite_with_result ) - /** * Add another fraction to this one. * @@ -92,22 +82,15 @@ * @see sub, mul, div */ - function add( $fraction, $overwrite_with_result = true ) - { - // check, if argument is a fraction - $fraction = $this->_check_fraction( $fraction ); - - // add - $result = new Math_Fraction( ( $this->zaehler + $fraction->zaehler ), - ( $this->nenner * $fraction->nenner ) - ); + function add($fraction, $overwrite_with_result = true) { + $fraction = $this->_check_fraction($fraction); - // handle result - return $this->_return( $result, $overwrite_with_result ); - } + $result = new Math_Fraction(($this->numerator + $fraction->numerator), + ($this->denominator * $fraction->denominator) + ); - // }}} - // {{{ sub( $fraction, $overwrite_with_result ) + return $this->_return($result, $overwrite_with_result); + } /** * Subtract another fraction from this one. @@ -119,22 +102,15 @@ * @see add, mul, div */ - function sub( $fraction, $overwrite_with_result = true ) - { - // check, if argument is a fraction - $fraction = $this->_check_fraction( $fraction ); - - // subtract - $result = new Math_Fraction( ( $this->zaehler - $fraction->zaehler ), - ( $this->nenner * $fraction->nenner ) - ); + function sub($fraction, $overwrite_with_result = true) { + $fraction = $this->_check_fraction($fraction); - // handle result - return $this->_return( $result, $overwrite_with_result ); - } + $result = new Math_Fraction(($this->numerator - $fraction->numerator), + ($this->denominator * $fraction->denominator) + ); - // }}} - // {{{ mul( $fraction, $overwrite_with_result ) + return $this->_return($result, $overwrite_with_result); + } /** * Multiply another fraction with this one. @@ -146,22 +122,15 @@ * @see add, sub, div */ - function mul( $fraction, $overwrite_with_result = true ) - { - // check, if argument is a fraction - $fraction = $this->_check_fraction( $fraction ); - - // multiply - $result = new Math_Fraction( ( $this->zaehler * $fraction->zaehler ), - ( $this->nenner * $fraction->nenner ) - ); + function mul($fraction, $overwrite_with_result = true) { + $fraction = $this->_check_fraction( $fraction ); - // handle result - return $this->_return( $result, $overwrite_with_result ); - } + $result = new Math_Fraction(($this->numerator * $fraction->numerator), + ($this->denominator * $fraction->denominator) + ); - // }}} - // {{{ div( $fraction, $overwrite_with_result ) + return $this->_return($result, $overwrite_with_result); + } /** * Divide this fraction by another one. @@ -173,22 +142,15 @@ * @see add, sub, mul */ - function div( $fraction, $overwrite_with_result = true ) - { - // check, if argument is a fraction - $fraction = $this->_check_fraction( $fraction ); - - // divide - $result = new Math_Fraction( ( $this->zaehler * $fraction->nenner ), - ( $this->nenner * $fraction->zaehler ) - ); + function div($fraction, $overwrite_with_result = true) { + $fraction = $this->_check_fraction( $fraction ); - // handle result - return $this->_return( $result, $overwrite_with_result ); - } + $result = new Math_Fraction(($this->numerator * $fraction->denominator), + ($this->denominator * $fraction->numerator) + ); - // }}} - // {{{ normalize() + return $this->_return($result, $overwrite_with_result); + } /** * Normalize this fraction. @@ -196,18 +158,12 @@ * @access public */ - function normalize() - { - // get greatest common divisor - $gcd = gcd( $this->zaehler, $this->nenner ); - - // divide zaehler / nenner by gcd - $this->zaehler = $this->zaehler / $gcd; - $this->nenner = $this->nenner / $gcd; - } + function normalize() { + $gcd = Math_Util::gcd($this->numerator, $this->denominator); - // }}} - // {{{ dump() + $this->numerator = $this->numerator / $gcd; + $this->denominator = $this->denominator / $gcd; + } /** * Dump this fraction. @@ -215,14 +171,10 @@ * @access public */ - function dump() - { - print $this->get(); + function dump() { + echo $this->get(); } - // }}} - // {{{ get() - /** * Get string representation of this fraction. * @@ -230,60 +182,44 @@ * @access public */ - function get() - { - return $this->zaehler . " / " . $this->nenner; + function get() { + return $this->numerator . ' / ' . $this->denominator; } - // }}} - // {{{ _check_fraction( $fraction ) - /** * Check, if a varaible holds a Math_Fraction object. * * @param Math_Fraction fraction to be checked * @return Math_Fraction checked fraction - * @access public + * @access private */ - function _check_fraction( $fraction ) - { - if( get_class( $fraction ) != "math_fraction" ) - { - $fraction = new Math_Fraction( $fraction ); - } + function _check_fraction($fraction) { + if (get_class($fraction) != 'math_fraction') { + $fraction = new Math_Fraction( $fraction ); + } - return $fraction; + return $fraction; } - // }}} - // {{{ _return( $result, $overwrite_with_result ) - /** * Handle the return or storage of a result from add, sub, mul or div. * - * @param Math_Fraction result - * @param boolean overwrite_with_result + * @param Math_Fraction result + * @param boolean overwrite_with_result * @return Math_Fraction result (if overwrite_with_result == false) - * @access public + * @access private */ - function _return( $result, $overwrite_with_result ) - { - $result->normalize(); - - if( $overwrite_with_result ) - { - $this->zaehler = $result->zaehler; - $this->nenner = $result->nenner; - } - - else - { - return $result; - } - } + function _return($result, $overwrite_with_result) { + $result->normalize(); - // }}} - } + if ($overwrite_with_result) { + $this->numerator = $result->numerator; + $this->denominator = $result->denominator; + } else { + return $result; + } + } +} ?> Index: php4/pear/Math/Util.php diff -u php4/pear/Math/Util.php:1.2 php4/pear/Math/Util.php:1.3 --- php4/pear/Math/Util.php:1.2 Tue Jan 9 20:01:56 2001 +++ php4/pear/Math/Util.php Mon Jul 23 05:57:52 2001 @@ -16,43 +16,34 @@ // | Authors: Sebastian Bergmann <sb@sebastian-bergmann.de> | // +----------------------------------------------------------------------+ // -// $Id: Util.php,v 1.2 2001/01/10 01:01:56 ssb Exp $ +// $Id: Util.php,v 1.3 2001/07/23 09:57:52 sbergmann Exp $ // - // {{{ gcd( $a, $b ) - +class Math_Util { /** * Calculates the Greatest Common Divisor (gcd) of two numbers. * * @param int a first number * @param int b second number - * @return int result gcd(a,b) + * @return int gcd(a,b) * @access public */ - function gcd( $a, $b ) - { - // make sure that $a > $b holds true - if( $b > $a ) - { - // swap $a and $b - list( $a, $b ) = array( $b, $a ); + function gcd($a, $b) { + if ($b > $a) { + list($a, $b) = array($b, $a); } - // init $c to 1 $c = 1; // the magic loop (thanks, Euclid :-) - while( $c > 0 ) - { + while ($c > 0) { $c = $a % $b; $a = $b; $b = $c; } - // return result return $a; } - - // }}} +} ?>
Index: php4/pear/Benchmark/Iterate.php diff -u php4/pear/Benchmark/Iterate.php:1.6 php4/pear/Benchmark/Iterate.php:1.7 --- php4/pear/Benchmark/Iterate.php:1.6 Sat Mar 3 01:36:44 2001 +++ php4/pear/Benchmark/Iterate.php Mon Jul 23 05:57:52 2001 @@ -16,7 +16,7 @@ // | Authors: Sebastian Bergmann <sb@sebastian-bergmann.de> | // +----------------------------------------------------------------------+ // -// $Id: Iterate.php,v 1.6 2001/03/03 06:36:44 sbergmann Exp $ +// $Id: Iterate.php,v 1.7 2001/07/23 09:57:52 sbergmann Exp $ // require_once 'Benchmark/Timer.php'; @@ -42,44 +42,32 @@ * $result = $benchmark->get(); * * @author Sebastian Bergmann <sb@sebastian-bergmann.de> -* @version $Revision: 1.6 $ +* @version $Revision: 1.7 $ * @access public */ -class Benchmark_Iterate extends Benchmark_Timer -{ - // {{{ run() - +class Benchmark_Iterate extends Benchmark_Timer { /** * Benchmarks a function. * * @access public */ - function run() - { - // get arguments - $arguments = func_get_args(); - $iterations = array_shift($arguments); + function run() { + $arguments = func_get_args(); + + $iterations = array_shift($arguments); $function_name = array_shift($arguments); - // main loop - for ($i = 1; $i <= $iterations; $i++) - { - // set 'start' marker for current iteration + for ($i = 1; $i <= $iterations; $i++) { $this->setMarker('start_'.$i); - // call function to be benchmarked call_user_func_array($function_name, $arguments); - // set 'end' marker for current iteration $this->setMarker('end_'.$i); } } - // }}} - // {{{ get() - /** * Returns benchmark result. * @@ -91,47 +79,33 @@ * @access public */ - function get() - { - // init result array + function get() { $result = array(); - - // init variable - $total = 0; + $total = 0; $iterations = count($this->markers)/2; - // loop through iterations - for ($i = 1; $i <= $iterations; $i++) - { - // get elapsed time for current iteration + for ($i = 1; $i <= $iterations; $i++) { $time = $this->timeElapsed('start_'.$i , 'end_'.$i); - // sum up total time spent if (extension_loaded('bcmath')) { $total = bcadd($total, $time, 6); } else { $total = $total + $time; } - // store time $result[$i] = $time; } - // calculate and store mean time if (extension_loaded('bcmath')) { $result['mean'] = bcdiv($total, $iterations, 6); } else { $result['mean'] = $total / $iterations; } - // store iterations $result['iterations'] = $iterations; - // return result array return $result; } - - // }}} } ?> Index: php4/pear/Benchmark/Timer.php diff -u php4/pear/Benchmark/Timer.php:1.8 php4/pear/Benchmark/Timer.php:1.9 --- php4/pear/Benchmark/Timer.php:1.8 Sat Mar 3 01:35:25 2001 +++ php4/pear/Benchmark/Timer.php Mon Jul 23 05:57:52 2001 @@ -16,7 +16,7 @@ // | Authors: Sebastian Bergmann <sb@sebastian-bergmann.de> | // +----------------------------------------------------------------------+ // -// $Id: Timer.php,v 1.8 2001/03/03 06:35:25 sbergmann Exp $ +// $Id: Timer.php,v 1.9 2001/07/23 09:57:52 sbergmann Exp $ // /** @@ -37,11 +37,11 @@ * $profiling = $timer->getProfiling(); * * @author Sebastian Bergmann <sb@sebastian-bergmann.de> - * @version $Revision: 1.8 $ + * @version $Revision: 1.9 $ * @access public */ -class Benchmark_Timer -{ + +class Benchmark_Timer { /** * Contains the markers * @@ -56,8 +56,7 @@ * @see setMarker(), stop() * @access public */ - function start() - { + function start() { $this->setMarker('Start'); } @@ -67,8 +66,7 @@ * @see setMarker(), start() * @access public */ - function stop() - { + function stop() { $this->setMarker('Stop'); } @@ -79,8 +77,7 @@ * @see start(), stop() * @access public */ - function setMarker($name) - { + function setMarker($name) { $microtime = explode(' ', microtime()); $this->markers[$name] = $microtime[1] . substr($microtime[0], 1); } @@ -93,8 +90,7 @@ * @return double $time_elapsed time elapsed between $start and $end * @access public */ - function timeElapsed($start = 'Start', $end = 'Stop') - { + function timeElapsed($start = 'Start', $end = 'Stop') { if (extension_loaded('bcmath')) { return bcsub($this->markers[$end], $this->markers[$start], 6); } else { @@ -113,8 +109,7 @@ * @return array $profiling * @access public */ - function getProfiling() - { + function getProfiling() { $i = 0; $total = 0; $result = array(); Index: php4/pear/Math/Fraction.php diff -u php4/pear/Math/Fraction.php:1.2 php4/pear/Math/Fraction.php:1.3 --- php4/pear/Math/Fraction.php:1.2 Tue Jan 9 20:01:56 2001 +++ php4/pear/Math/Fraction.php Mon Jul 23 05:57:52 2001 @@ -16,72 +16,62 @@ // | Authors: Sebastian Bergmann <sb@sebastian-bergmann.de> | // +----------------------------------------------------------------------+ // -// $Id: Fraction.php,v 1.2 2001/01/10 01:01:56 ssb Exp $ +// $Id: Fraction.php,v 1.3 2001/07/23 09:57:52 sbergmann Exp $ // - /** - * Math::Math_Fraction - * - * Purpose: - * - * Class for handling fractions. - * - * Example: - * - * $a = new Math_Fraction( 1, 2 ); - * $b = new Math_Fraction( 3, 4 ); - * - * $a->add( $b ); - * - * @author Sebastian Bergmann <sb@sebastian-bergmann.de> - * @version $Revision: 1.2 $ - * @access public - */ - - require_once "Math/Util.php"; - - class Math_Fraction - { - // {{{ properties +/** +* Math::Math_Fraction +* +* Purpose: +* +* Class for handling fractions. +* +* Example: +* +* $a = new Math_Fraction( 1, 2 ); +* $b = new Math_Fraction( 3, 4 ); +* +* $a->add( $b ); +* +* @author Sebastian Bergmann <sb@sebastian-bergmann.de> +* @version $Revision: 1.3 $ +* @access public +*/ +require_once 'Math/Util.php'; + +class Math_Fraction { /** - * zaehler of the fraction + * numerator of the fraction * - * @var int zaehler + * @var int numerator * @access public */ - var $zaehler; + var $numerator; /** - * nenner of the fraction + * denominator of the fraction * - * @var int nenner + * @var int denominator * @access public */ - var $nenner; - - // }}} - // {{{ Math_Fraction( $zaehler, $nenner ) + var $denominator; /** - * Constructor. + * Constructor * - * @param int zaehler - * @param int nenner + * @param int numerator + * @param int denominator * @access public */ - function Math_Fraction( $zaehler, $nenner = 1 ) - { - $this->zaehler = $zaehler; - $this->nenner = $nenner; + function Math_Fraction($numerator, $denominator = 1) { + $this->numerator = $numerator; + $this->denominator = $denominator; } - // }}} - // {{{ add( $fraction, $overwrite_with_result ) - /** * Add another fraction to this one. * @@ -92,22 +82,15 @@ * @see sub, mul, div */ - function add( $fraction, $overwrite_with_result = true ) - { - // check, if argument is a fraction - $fraction = $this->_check_fraction( $fraction ); - - // add - $result = new Math_Fraction( ( $this->zaehler + $fraction->zaehler ), - ( $this->nenner * $fraction->nenner ) - ); + function add($fraction, $overwrite_with_result = true) { + $fraction = $this->_check_fraction($fraction); - // handle result - return $this->_return( $result, $overwrite_with_result ); - } + $result = new Math_Fraction(($this->numerator + $fraction->numerator), + ($this->denominator * $fraction->denominator) + ); - // }}} - // {{{ sub( $fraction, $overwrite_with_result ) + return $this->_return($result, $overwrite_with_result); + } /** * Subtract another fraction from this one. @@ -119,22 +102,15 @@ * @see add, mul, div */ - function sub( $fraction, $overwrite_with_result = true ) - { - // check, if argument is a fraction - $fraction = $this->_check_fraction( $fraction ); - - // subtract - $result = new Math_Fraction( ( $this->zaehler - $fraction->zaehler ), - ( $this->nenner * $fraction->nenner ) - ); + function sub($fraction, $overwrite_with_result = true) { + $fraction = $this->_check_fraction($fraction); - // handle result - return $this->_return( $result, $overwrite_with_result ); - } + $result = new Math_Fraction(($this->numerator - $fraction->numerator), + ($this->denominator * $fraction->denominator) + ); - // }}} - // {{{ mul( $fraction, $overwrite_with_result ) + return $this->_return($result, $overwrite_with_result); + } /** * Multiply another fraction with this one. @@ -146,22 +122,15 @@ * @see add, sub, div */ - function mul( $fraction, $overwrite_with_result = true ) - { - // check, if argument is a fraction - $fraction = $this->_check_fraction( $fraction ); - - // multiply - $result = new Math_Fraction( ( $this->zaehler * $fraction->zaehler ), - ( $this->nenner * $fraction->nenner ) - ); + function mul($fraction, $overwrite_with_result = true) { + $fraction = $this->_check_fraction( $fraction ); - // handle result - return $this->_return( $result, $overwrite_with_result ); - } + $result = new Math_Fraction(($this->numerator * $fraction->numerator), + ($this->denominator * $fraction->denominator) + ); - // }}} - // {{{ div( $fraction, $overwrite_with_result ) + return $this->_return($result, $overwrite_with_result); + } /** * Divide this fraction by another one. @@ -173,22 +142,15 @@ * @see add, sub, mul */ - function div( $fraction, $overwrite_with_result = true ) - { - // check, if argument is a fraction - $fraction = $this->_check_fraction( $fraction ); - - // divide - $result = new Math_Fraction( ( $this->zaehler * $fraction->nenner ), - ( $this->nenner * $fraction->zaehler ) - ); + function div($fraction, $overwrite_with_result = true) { + $fraction = $this->_check_fraction( $fraction ); - // handle result - return $this->_return( $result, $overwrite_with_result ); - } + $result = new Math_Fraction(($this->numerator * $fraction->denominator), + ($this->denominator * $fraction->numerator) + ); - // }}} - // {{{ normalize() + return $this->_return($result, $overwrite_with_result); + } /** * Normalize this fraction. @@ -196,18 +158,12 @@ * @access public */ - function normalize() - { - // get greatest common divisor - $gcd = gcd( $this->zaehler, $this->nenner ); - - // divide zaehler / nenner by gcd - $this->zaehler = $this->zaehler / $gcd; - $this->nenner = $this->nenner / $gcd; - } + function normalize() { + $gcd = Math_Util::gcd($this->numerator, $this->denominator); - // }}} - // {{{ dump() + $this->numerator = $this->numerator / $gcd; + $this->denominator = $this->denominator / $gcd; + } /** * Dump this fraction. @@ -215,14 +171,10 @@ * @access public */ - function dump() - { - print $this->get(); + function dump() { + echo $this->get(); } - // }}} - // {{{ get() - /** * Get string representation of this fraction. * @@ -230,60 +182,44 @@ * @access public */ - function get() - { - return $this->zaehler . " / " . $this->nenner; + function get() { + return $this->numerator . ' / ' . $this->denominator; } - // }}} - // {{{ _check_fraction( $fraction ) - /** * Check, if a varaible holds a Math_Fraction object. * * @param Math_Fraction fraction to be checked * @return Math_Fraction checked fraction - * @access public + * @access private */ - function _check_fraction( $fraction ) - { - if( get_class( $fraction ) != "math_fraction" ) - { - $fraction = new Math_Fraction( $fraction ); - } + function _check_fraction($fraction) { + if (get_class($fraction) != 'math_fraction') { + $fraction = new Math_Fraction( $fraction ); + } - return $fraction; + return $fraction; } - // }}} - // {{{ _return( $result, $overwrite_with_result ) - /** * Handle the return or storage of a result from add, sub, mul or div. * - * @param Math_Fraction result - * @param boolean overwrite_with_result + * @param Math_Fraction result + * @param boolean overwrite_with_result * @return Math_Fraction result (if overwrite_with_result == false) - * @access public + * @access private */ - function _return( $result, $overwrite_with_result ) - { - $result->normalize(); - - if( $overwrite_with_result ) - { - $this->zaehler = $result->zaehler; - $this->nenner = $result->nenner; - } - - else - { - return $result; - } - } + function _return($result, $overwrite_with_result) { + $result->normalize(); - // }}} - } + if ($overwrite_with_result) { + $this->numerator = $result->numerator; + $this->denominator = $result->denominator; + } else { + return $result; + } + } +} ?> Index: php4/pear/Math/Util.php diff -u php4/pear/Math/Util.php:1.2 php4/pear/Math/Util.php:1.3 --- php4/pear/Math/Util.php:1.2 Tue Jan 9 20:01:56 2001 +++ php4/pear/Math/Util.php Mon Jul 23 05:57:52 2001 @@ -16,43 +16,34 @@ // | Authors: Sebastian Bergmann <sb@sebastian-bergmann.de> | // +----------------------------------------------------------------------+ // -// $Id: Util.php,v 1.2 2001/01/10 01:01:56 ssb Exp $ +// $Id: Util.php,v 1.3 2001/07/23 09:57:52 sbergmann Exp $ // - // {{{ gcd( $a, $b ) - +class Math_Util { /** * Calculates the Greatest Common Divisor (gcd) of two numbers. * * @param int a first number * @param int b second number - * @return int result gcd(a,b) + * @return int gcd(a,b) * @access public */ - function gcd( $a, $b ) - { - // make sure that $a > $b holds true - if( $b > $a ) - { - // swap $a and $b - list( $a, $b ) = array( $b, $a ); + function gcd($a, $b) { + if ($b > $a) { + list($a, $b) = array($b, $a); } - // init $c to 1 $c = 1; // the magic loop (thanks, Euclid :-) - while( $c > 0 ) - { + while ($c > 0) { $c = $a % $b; $a = $b; $b = $c; } - // return result return $a; } - - // }}} +} ?>