]> granicus.if.org Git - php/commitdiff
Add the ability to take the logarithm of any base by adding a base parameter
authorJason Greene <jason@php.net>
Mon, 11 Nov 2002 05:21:35 +0000 (05:21 +0000)
committerJason Greene <jason@php.net>
Mon, 11 Nov 2002 05:21:35 +0000 (05:21 +0000)
to log()
Added regression tests for the new form

ext/standard/math.c
ext/standard/tests/math/log.phpt

index dafaf057883ddf99e582dee1291ac58be4a8a73f..9be11538c61291832fdfc3877a873f210d5f607e 100644 (file)
@@ -520,19 +520,35 @@ PHP_FUNCTION(log1p)
 /* }}} */
 
 #endif
-/* {{{ proto float log(float number)
-   Returns the natural logarithm of the number */
+/* {{{ proto float log(float number, [float base])
+   Returns the natural logarithm of the number, or the base log if base is specified */
 
 PHP_FUNCTION(log)
 {
-       zval **num;
-
-       if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &num) == FAILURE) {
-               WRONG_PARAM_COUNT;
+       zval **num, **base;
+       
+       switch (ZEND_NUM_ARGS()) {
+               case 1:
+                       if (zend_get_parameters_ex(1, &num) == FAILURE) {
+                               WRONG_PARAM_COUNT;
+                       }
+                       convert_to_double_ex(num);
+                       RETURN_DOUBLE(log(Z_DVAL_PP(num)));
+               case 2:
+                       if (zend_get_parameters_ex(2, &num, &base) == FAILURE) {
+                               WRONG_PARAM_COUNT;
+                       }
+                       convert_to_double_ex(num);
+                       convert_to_double_ex(base);
+               
+                       if (Z_DVAL_PP(base) <= 0.0) {
+                               php_error(E_WARNING, "base must be greater than 0", Z_DVAL_PP(base));
+                               RETURN_FALSE;
+                       }
+                       RETURN_DOUBLE(log(Z_DVAL_PP(num)) / log(Z_DVAL_PP(base)));
+               default:
+                       WRONG_PARAM_COUNT;
        }
-       convert_to_double_ex(num);
-       Z_DVAL_P(return_value) = log(Z_DVAL_PP(num));
-       Z_TYPE_P(return_value) = IS_DOUBLE;
 }
 
 /* }}} */
index 344c72fa0de240596d8d54ec22a80daf701708fe..285b19c853157133ebce91cf4232368cb7366282 100644 (file)
@@ -5,17 +5,40 @@ log() tests
 --FILE--
 <?php // $Id$
 echo "On failure, please mail result to php-dev@lists.php.net\n";
-for ($x=0, $count=0; $x < 200; $x++) {
+for ($x = 0, $count= 0; $x < 200; $x++) {
     $x2 = (int) exp(log($x));
     // e ^ log(x) should be close in range to x
     if (($x2 < ($x + 2)) && ($x2 > ($x - 2))) { 
         $count++; 
-    }
-    else {
+    } else {
         print "$x : $x2\n";
     }
 }
 print $count . "\n";
+
+// Now test the base form of log
+for ($base = 2; $base < 11; $base++) {
+    for ($x = 0, $count= 0; $x < 50; $x++) {
+        $x2 = (int) pow($base, log($x, $base));
+        // base ^ log(x) should be close in range to x
+        if (($x2 < ($x + 2)) && ($x2 > ($x - 2))) { 
+            $count++; 
+        } else {
+             print "base $base: $x : $x2\n";
+        }
+    }
+    print $count . "\n";
+}
+?>
 --EXPECT--
 On failure, please mail result to php-dev@lists.php.net
 200
+50
+50
+50
+50
+50
+50
+50
+50
+50