From 5193c6c35d31e27888ce771d6ac445cc08beae02 Mon Sep 17 00:00:00 2001 From: Mark Baker Date: Sun, 10 Jun 2012 22:06:14 +0000 Subject: [PATCH] Envelope case validation and fix return values git-svn-id: https://phpexcel.svn.codeplex.com/svn/trunk@91574 2327b42d-5241-43d6-9e2a-de5ac946f064 --- Classes/PHPExcel/Calculation/MathTrig.php | 136 ++++++++++++++++++---- 1 file changed, 111 insertions(+), 25 deletions(-) diff --git a/Classes/PHPExcel/Calculation/MathTrig.php b/Classes/PHPExcel/Calculation/MathTrig.php index 346a5ffd..35217b67 100644 --- a/Classes/PHPExcel/Calculation/MathTrig.php +++ b/Classes/PHPExcel/Calculation/MathTrig.php @@ -97,40 +97,58 @@ class PHPExcel_Calculation_MathTrig { * @param float $yCoordinate The y-coordinate of the point. * @return float The inverse tangent of the specified x- and y-coordinates. */ - public static function ATAN2($xCoordinate, $yCoordinate) { - $xCoordinate = (float) PHPExcel_Calculation_Functions::flattenSingleValue($xCoordinate); - $yCoordinate = (float) PHPExcel_Calculation_Functions::flattenSingleValue($yCoordinate); + public static function ATAN2($xCoordinate = NULL, $yCoordinate = NULL) { + $xCoordinate = PHPExcel_Calculation_Functions::flattenSingleValue($xCoordinate); + $yCoordinate = PHPExcel_Calculation_Functions::flattenSingleValue($yCoordinate); - if (($xCoordinate == 0) && ($yCoordinate == 0)) { - return PHPExcel_Calculation_Functions::DIV0(); + $xCoordinate = ($xCoordinate !== NULL) ? $xCoordinate : 0.0; + $yCoordinate = ($yCoordinate !== NULL) ? $yCoordinate : 0.0; + + if (((is_numeric($xCoordinate)) || (is_bool($xCoordinate))) && + ((is_numeric($yCoordinate))) || (is_bool($yCoordinate))) { + $xCoordinate = (float) $xCoordinate; + $yCoordinate = (float) $yCoordinate; + + if (($xCoordinate == 0) && ($yCoordinate == 0)) { + return PHPExcel_Calculation_Functions::DIV0(); + } + + return atan2($yCoordinate, $xCoordinate); } - - return atan2($yCoordinate, $xCoordinate); - } // function REVERSE_ATAN2() + return PHPExcel_Calculation_Functions::VALUE(); + } // function ATAN2() /** * CEILING * * Returns number rounded up, away from zero, to the nearest multiple of significance. + * For example, if you want to avoid using pennies in your prices and your product is + * priced at $4.42, use the formula =CEILING(4.42,0.05) to round prices up to the + * nearest nickel. * - * @param float $number Number to round - * @param float $significance Significance + * Excel Function: + * CEILING(number[,significance]) + * + * @access public + * @category Mathematical and Trigonometric Functions + * @param float $number The number you want to round. + * @param float $significance The multiple to which you want to round. * @return float Rounded Number */ - public static function CEILING($number,$significance=null) { + public static function CEILING($number, $significance = NULL) { $number = PHPExcel_Calculation_Functions::flattenSingleValue($number); $significance = PHPExcel_Calculation_Functions::flattenSingleValue($significance); - if ((is_null($significance)) && (PHPExcel_Calculation_Functions::getCompatibilityMode() == PHPExcel_Calculation_Functions::COMPATIBILITY_GNUMERIC)) { + if ((is_null($significance)) && + (PHPExcel_Calculation_Functions::getCompatibilityMode() == PHPExcel_Calculation_Functions::COMPATIBILITY_GNUMERIC)) { $significance = $number/abs($number); } if ((is_numeric($number)) && (is_numeric($significance))) { - if (self::SIGN($number) == self::SIGN($significance)) { - if ($significance == 0.0) { - return 0; - } + if ($significance == 0.0) { + return 0.0; + } elseif (self::SIGN($number) == self::SIGN($significance)) { return ceil($number / $significance) * $significance; } else { return PHPExcel_Calculation_Functions::NaN(); @@ -146,11 +164,16 @@ class PHPExcel_Calculation_MathTrig { * Returns the number of combinations for a given number of items. Use COMBIN to * determine the total possible number of groups for a given number of items. * + * Excel Function: + * COMBIN(numObjs,numInSet) + * + * @access public + * @category Mathematical and Trigonometric Functions * @param int $numObjs Number of different objects * @param int $numInSet Number of objects in each combination * @return int Number of combinations */ - public static function COMBIN($numObjs,$numInSet) { + public static function COMBIN($numObjs, $numInSet) { $numObjs = PHPExcel_Calculation_Functions::flattenSingleValue($numObjs); $numInSet = PHPExcel_Calculation_Functions::flattenSingleValue($numInSet); @@ -170,7 +193,16 @@ class PHPExcel_Calculation_MathTrig { * EVEN * * Returns number rounded up to the nearest even integer. + * You can use this function for processing items that come in twos. For example, + * a packing crate accepts rows of one or two items. The crate is full when + * the number of items, rounded up to the nearest two, matches the crate's + * capacity. * + * Excel Function: + * EVEN(number) + * + * @access public + * @category Mathematical and Trigonometric Functions * @param float $number Number to round * @return int Rounded Number */ @@ -195,7 +227,13 @@ class PHPExcel_Calculation_MathTrig { * FACT * * Returns the factorial of a number. + * The factorial of a number is equal to 1*2*3*...* number. * + * Excel Function: + * FACT(factVal) + * + * @access public + * @category Mathematical and Trigonometric Functions * @param float $factVal Factorial Value * @return int Factorial */ @@ -228,6 +266,11 @@ class PHPExcel_Calculation_MathTrig { * * Returns the double factorial of a number. * + * Excel Function: + * FACTDOUBLE(factVal) + * + * @access public + * @category Mathematical and Trigonometric Functions * @param float $factVal Factorial Value * @return int Double Factorial */ @@ -255,11 +298,16 @@ class PHPExcel_Calculation_MathTrig { * * Rounds number down, toward zero, to the nearest multiple of significance. * + * Excel Function: + * FLOOR(number[,significance]) + * + * @access public + * @category Mathematical and Trigonometric Functions * @param float $number Number to round * @param float $significance Significance * @return float Rounded Number */ - public static function FLOOR($number,$significance=null) { + public static function FLOOR($number, $significance = NULL) { $number = PHPExcel_Calculation_Functions::flattenSingleValue($number); $significance = PHPExcel_Calculation_Functions::flattenSingleValue($significance); @@ -284,20 +332,27 @@ class PHPExcel_Calculation_MathTrig { /** * GCD * - * Returns the greatest common divisor of a series of numbers + * Returns the greatest common divisor of a series of numbers. + * The greatest common divisor is the largest integer that divides both + * number1 and number2 without a remainder. * - * @param $array Values to calculate the Greatest Common Divisor - * @return int Greatest Common Divisor + * Excel Function: + * GCD(number1[,number2[, ...]]) + * + * @access public + * @category Mathematical and Trigonometric Functions + * @param mixed $arg,... Data values + * @return integer Greatest Common Divisor */ public static function GCD() { $returnValue = 1; - $allPoweredFactors = array(); + $allValuesFactors = array(); // Loop through arguments foreach(PHPExcel_Calculation_Functions::flattenArray(func_get_args()) as $value) { if (!is_numeric($value)) { return PHPExcel_Calculation_Functions::VALUE(); } elseif ($value == 0) { - break; + continue; } elseif($value < 0) { return PHPExcel_Calculation_Functions::NaN(); } @@ -306,6 +361,10 @@ class PHPExcel_Calculation_MathTrig { $allValuesFactors[] = $myCountedFactors; } $allValuesCount = count($allValuesFactors); + if ($allValuesCount == 0) { + return 0; + } + $mergedArray = $allValuesFactors[0]; for ($i=1;$i < $allValuesCount; ++$i) { $mergedArray = array_intersect_key($mergedArray,$allValuesFactors[$i]); @@ -351,6 +410,11 @@ class PHPExcel_Calculation_MathTrig { * * Casts a floating point value to an integer * + * Excel Function: + * INT(number) + * + * @access public + * @category Mathematical and Trigonometric Functions * @param float $number Number to cast to an integer * @return integer Integer value */ @@ -373,8 +437,16 @@ class PHPExcel_Calculation_MathTrig { * LCM * * Returns the lowest common multiplier of a series of numbers + * The least common multiple is the smallest positive integer that is a multiple + * of all integer arguments number1, number2, and so on. Use LCM to add fractions + * with different denominators. * - * @param $array Values to calculate the Lowest Common Multiplier + * Excel Function: + * LCM(number1[,number2[, ...]]) + * + * @access public + * @category Mathematical and Trigonometric Functions + * @param mixed $arg,... Data values * @return int Lowest Common Multiplier */ public static function LCM() { @@ -427,7 +499,7 @@ class PHPExcel_Calculation_MathTrig { * @param float $base The base of the logarithm. If base is omitted, it is assumed to be 10. * @return float */ - public static function LOG_BASE($number = NULL, $base=10) { + public static function LOG_BASE($number = NULL, $base = 10) { $number = PHPExcel_Calculation_Functions::flattenSingleValue($number); $base = (is_null($base)) ? 10 : (float) PHPExcel_Calculation_Functions::flattenSingleValue($base); @@ -442,6 +514,13 @@ class PHPExcel_Calculation_MathTrig { /** * MDETERM * + * Returns the matrix determinant of an array. + * + * Excel Function: + * MDETERM(array) + * + * @access public + * @category Mathematical and Trigonometric Functions * @param array $matrixValues A matrix of values * @return float */ @@ -476,6 +555,13 @@ class PHPExcel_Calculation_MathTrig { /** * MINVERSE * + * Returns the inverse matrix for the matrix stored in an array. + * + * Excel Function: + * MINVERSE(array) + * + * @access public + * @category Mathematical and Trigonometric Functions * @param array $matrixValues A matrix of values * @return array */