jason           Mon Nov 11 00:21:35 2002 EDT

  Modified files:              
    /php4/ext/standard  math.c 
    /php4/ext/standard/tests/math       log.phpt 
  Log:
  Add the ability to take the logarithm of any base by adding a base parameter
  to log()
  Added regression tests for the new form
  
  
Index: php4/ext/standard/math.c
diff -u php4/ext/standard/math.c:1.93 php4/ext/standard/math.c:1.94
--- php4/ext/standard/math.c:1.93       Thu Oct 24 15:15:40 2002
+++ php4/ext/standard/math.c    Mon Nov 11 00:21:35 2002
@@ -19,7 +19,7 @@
    +----------------------------------------------------------------------+
 */
 
-/* $Id: math.c,v 1.93 2002/10/24 19:15:40 helly Exp $ */
+/* $Id: math.c,v 1.94 2002/11/11 05:21:35 jason Exp $ */
 
 #include "php.h"
 #include "php_math.h"
@@ -520,19 +520,35 @@
 /* }}} */
 
 #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: php4/ext/standard/tests/math/log.phpt
diff -u php4/ext/standard/tests/math/log.phpt:1.1 
php4/ext/standard/tests/math/log.phpt:1.2
--- php4/ext/standard/tests/math/log.phpt:1.1   Sun Nov 10 22:45:04 2002
+++ php4/ext/standard/tests/math/log.phpt       Mon Nov 11 00:21:35 2002
@@ -3,19 +3,42 @@
 --POST--
 --GET--
 --FILE--
-<?php // $Id: log.phpt,v 1.1 2002/11/11 03:45:04 jason Exp $
+<?php // $Id: log.phpt,v 1.2 2002/11/11 05:21:35 jason Exp $
 echo "On failure, please mail result to [EMAIL PROTECTED]\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 [EMAIL PROTECTED]
 200
+50
+50
+50
+50
+50
+50
+50
+50
+50



-- 
PHP CVS Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to