cvs: php4 /pear/Math Fraction.php Util.php

From: Date: Fri, 24 Aug 2001 21:51:04 +0000
Subject: cvs: php4 /pear/Math Fraction.php Util.php
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-659@lists.php.net to get a copy of this message
mj Fri Aug 24 17:51:04 2001 EDT Modified files: /php4/pear/Math Fraction.php Util.php Log: * Made the mathematic classes more coding standard compliant Index: php4/pear/Math/Fraction.php diff -u php4/pear/Math/Fraction.php:1.3 php4/pear/Math/Fraction.php:1.4 --- php4/pear/Math/Fraction.php:1.3 Mon Jul 23 05:57:52 2001 +++ php4/pear/Math/Fraction.php Fri Aug 24 17:51:03 2001 @@ -16,7 +16,7 @@ // | Authors: Sebastian Bergmann <sb@sebastian-bergmann.de> | // +----------------------------------------------------------------------+ // -// $Id: Fraction.php,v 1.3 2001/07/23 09:57:52 sbergmann Exp $ +// $Id: Fraction.php,v 1.4 2001/08/24 21:51:03 mj Exp $ // /** @@ -34,13 +34,15 @@ * $a->add( $b ); * * @author Sebastian Bergmann <sb@sebastian-bergmann.de> -* @version $Revision: 1.3 $ +* @version $Revision: 1.4 $ * @access public +* @package Numbers */ require_once 'Math/Util.php'; -class Math_Fraction { +class Math_Fraction +{ /** * numerator of the fraction * @@ -67,7 +69,8 @@ * @access public */ - function Math_Fraction($numerator, $denominator = 1) { + function Math_Fraction($numerator, $denominator = 1) + { $this->numerator = $numerator; $this->denominator = $denominator; } @@ -82,7 +85,8 @@ * @see sub, mul, div */ - function add($fraction, $overwrite_with_result = true) { + function add($fraction, $overwrite_with_result = true) + { $fraction = $this->_check_fraction($fraction); $result = new Math_Fraction(($this->numerator + $fraction->numerator), @@ -102,7 +106,8 @@ * @see add, mul, div */ - function sub($fraction, $overwrite_with_result = true) { + function sub($fraction, $overwrite_with_result = true) + { $fraction = $this->_check_fraction($fraction); $result = new Math_Fraction(($this->numerator - $fraction->numerator), @@ -122,7 +127,8 @@ * @see add, sub, div */ - function mul($fraction, $overwrite_with_result = true) { + function mul($fraction, $overwrite_with_result = true) + { $fraction = $this->_check_fraction( $fraction ); $result = new Math_Fraction(($this->numerator * $fraction->numerator), @@ -142,7 +148,8 @@ * @see add, sub, mul */ - function div($fraction, $overwrite_with_result = true) { + function div($fraction, $overwrite_with_result = true) + { $fraction = $this->_check_fraction( $fraction ); $result = new Math_Fraction(($this->numerator * $fraction->denominator), @@ -158,7 +165,8 @@ * @access public */ - function normalize() { + function normalize() + { $gcd = Math_Util::gcd($this->numerator, $this->denominator); $this->numerator = $this->numerator / $gcd; @@ -171,7 +179,8 @@ * @access public */ - function dump() { + function dump() + { echo $this->get(); } @@ -182,7 +191,8 @@ * @access public */ - function get() { + function get() + { return $this->numerator . ' / ' . $this->denominator; } @@ -194,7 +204,8 @@ * @access private */ - function _check_fraction($fraction) { + function _check_fraction($fraction) + { if (get_class($fraction) != 'math_fraction') { $fraction = new Math_Fraction( $fraction ); } @@ -211,7 +222,8 @@ * @access private */ - function _return($result, $overwrite_with_result) { + function _return($result, $overwrite_with_result) + { $result->normalize(); if ($overwrite_with_result) { Index: php4/pear/Math/Util.php diff -u php4/pear/Math/Util.php:1.3 php4/pear/Math/Util.php:1.4 --- php4/pear/Math/Util.php:1.3 Mon Jul 23 05:57:52 2001 +++ php4/pear/Math/Util.php Fri Aug 24 17:51:04 2001 @@ -16,34 +16,43 @@ // | Authors: Sebastian Bergmann <sb@sebastian-bergmann.de> | // +----------------------------------------------------------------------+ // -// $Id: Util.php,v 1.3 2001/07/23 09:57:52 sbergmann Exp $ +// $Id: Util.php,v 1.4 2001/08/24 21:51:04 mj Exp $ // -class Math_Util { - /** - * Calculates the Greatest Common Divisor (gcd) of two numbers. - * - * @param int a first number - * @param int b second number - * @return int gcd(a,b) - * @access public - */ +/** + * Mathematic utilities + * + * @author Sebastian Bergmann <sb@sebastian-bergmann.de> + * @version $Revision: 1.4 $ + * @package Numbers + */ +class Math_Util +{ + + /** + * Calculates the Greatest Common Divisor (gcd) of two numbers. + * + * @param int a first number + * @param int b second number + * @return int gcd(a,b) + * @access public + */ + function gcd($a, $b) + { + if ($b > $a) { + list($a, $b) = array($b, $a); + } - function gcd($a, $b) { - if ($b > $a) { - list($a, $b) = array($b, $a); - } + $c = 1; - $c = 1; + // the magic loop (thanks, Euclid :-) + while ($c > 0) { + $c = $a % $b; + $a = $b; + $b = $c; + } - // the magic loop (thanks, Euclid :-) - while ($c > 0) { - $c = $a % $b; - $a = $b; - $b = $c; + return $a; } - - return $a; - } } ?>

« previous php.pear.cvs (#659) next »