php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Return to Bug #60789
Patch pow_float_to_int_cast.patch revision 2012-01-18 13:00 UTC by ircmaxell at gmail dot com

Patch pow_float_to_int_cast.patch for Math related Bug #60789

Patch version 2012-01-18 13:00 UTC

Return to Bug #60789 | Download this patch
Patch Revisions:

Developer: ircmaxell@gmail.com

# This patch file was generated by NetBeans IDE
# Following Index: paths are relative to: C:\Users\ircmaxell\Documents\NetBeansProjects\PHP_Trunk
# This patch can be applied using context Tools: Patch action on respective folder.
# It uses platform neutral UTF-8 encoding and \n newlines.
# Above lines and this line are ignored by the patching process.
Index: ext/standard/math.c
--- ext/standard/math.c Base (BASE)
+++ ext/standard/math.c Locally Modified (Based On LOCAL)
@@ -608,6 +608,11 @@
 	convert_scalar_to_number(zbase TSRMLS_CC);
 	convert_scalar_to_number(zexp TSRMLS_CC);
 
+	if (M_REPRESENTABLE_AS_LONG_P(zbase) && M_REPRESENTABLE_AS_LONG_P(zexp)) {
+		convert_to_long(zbase);
+		convert_to_long(zexp);
+	}
+
 	/* if both base and exponent were longs, we'll try to get a long out */
 	if (Z_TYPE_P(zbase) == IS_LONG && Z_TYPE_P(zexp) == IS_LONG && Z_LVAL_P(zexp) >= 0) {
 		long l1 = 1, l2 = Z_LVAL_P(zbase), i = Z_LVAL_P(zexp);
Index: ext/standard/php_math.h
--- ext/standard/php_math.h Base (BASE)
+++ ext/standard/php_math.h Locally Modified (Based On LOCAL)
@@ -85,6 +85,8 @@
 
 #include <math.h>
 
+#define M_REPRESENTABLE_AS_LONG_P(a) (Z_TYPE_P(a) == IS_LONG || (Z_TYPE_P(a) == IS_DOUBLE && (double) (long) Z_DVAL_P(a) == Z_DVAL_P(a)))
+
 #ifndef M_E
 #define M_E			2.7182818284590452354   /* e */
 #endif
Index: ext/standard/tests/math/pow.phpt
--- ext/standard/tests/math/pow.phpt Base (BASE)
+++ ext/standard/tests/math/pow.phpt Locally Modified (Based On LOCAL)
@@ -36,29 +36,29 @@
  4	=== pow( 2, 2)
  0.25 === pow(-2,-2.0)
 -0.5  === pow(-2,-1.0)
- 1.0  === pow(-2, 0.0)
--2.0  === pow(-2, 1.0)
- 4.0  === pow(-2, 2.0)
+ 1  === pow(-2, 0.0)
+-2  === pow(-2, 1.0)
+ 4  === pow(-2, 2.0)
  1.0  === pow(-1,-2.0)
 -1.0  === pow(-1,-1.0)
- 1.0  === pow(-1, 0.0)
--1.0  === pow(-1, 1.0)
- 1.0  === pow(-1, 2.0)
+ 1  === pow(-1, 0.0)
+-1  === pow(-1, 1.0)
+ 1  === pow(-1, 2.0)
  TRUE === is_infinite(pow(0,-2.0))
  TRUE === is_infinite(pow(0,-1.0))
- 1.0  === pow( 0, 0.0)
- 0.0  === pow( 0, 1.0)
- 0.0  === pow( 0, 2.0)
+ 1  === pow( 0, 0.0)
+ 0  === pow( 0, 1.0)
+ 0  === pow( 0, 2.0)
  1.0  === pow( 1,-2.0)
  1.0  === pow( 1,-1.0)
- 1.0  === pow( 1, 0.0)
- 1.0  === pow( 1, 1.0)
- 1.0  === pow( 1, 2.0)
+ 1  === pow( 1, 0.0)
+ 1  === pow( 1, 1.0)
+ 1  === pow( 1, 2.0)
  0.25 === pow( 2,-2.0)
  0.5  === pow( 2,-1.0)
- 1.0  === pow( 2, 0.0)
- 2.0  === pow( 2, 1.0)
- 4.0  === pow( 2, 2.0)
+ 1  === pow( 2, 0.0)
+ 2  === pow( 2, 1.0)
+ 4  === pow( 2, 2.0)
  2147483648 === pow(2,31)
 -2147483648 ~== pow(-2,31)
  1000000000 === pow(10,9)
@@ -68,54 +68,54 @@
 sqrt(2) ~== pow(2,1/2)
  0.25 === pow(-2.0,-2.0)
 -0.5  === pow(-2.0,-1.0)
- 1.0  === pow(-2.0, 0.0)
--2.0  === pow(-2.0, 1.0)
- 4.0  === pow(-2.0, 2.0)
+ 1  === pow(-2.0, 0.0)
+-2  === pow(-2.0, 1.0)
+ 4  === pow(-2.0, 2.0)
  1.0  === pow(-1.0,-2.0)
 -1.0  === pow(-1.0,-1.0)
- 1.0  === pow(-1.0, 0.0)
--1.0  === pow(-1.0, 1.0)
- 1.0  === pow(-1.0, 2.0)
+ 1  === pow(-1.0, 0.0)
+-1  === pow(-1.0, 1.0)
+ 1  === pow(-1.0, 2.0)
  TRUE === is_infinite(pow(0.0,-2.0))
  TRUE === is_infinite(pow(0.0,-1.0))
- 1.0  === pow( 0.0, 0.0)
- 0.0  === pow( 0.0, 1.0)
- 0.0  === pow( 0.0, 2.0)
+ 1  === pow( 0.0, 0.0)
+ 0  === pow( 0.0, 1.0)
+ 0  === pow( 0.0, 2.0)
  1.0  === pow( 1.0,-2.0)
  1.0  === pow( 1.0,-1.0)
- 1.0  === pow( 1.0, 0.0)
- 1.0  === pow( 1.0, 1.0)
- 1.0  === pow( 1.0, 2.0)
+ 1  === pow( 1.0, 0.0)
+ 1  === pow( 1.0, 1.0)
+ 1  === pow( 1.0, 2.0)
  0.25 === pow( 2.0,-2.0)
  0.5  === pow( 2.0,-1.0)
- 1.0  === pow( 2.0, 0.0)
- 2.0  === pow( 2.0, 1.0)
- 4.0  === pow( 2.0, 2.0)
+ 1  === pow( 2.0, 0.0)
+ 2  === pow( 2.0, 1.0)
+ 4  === pow( 2.0, 2.0)
  0.25 === pow(-2.0,-2)
 -0.5  === pow(-2.0,-1)
- 1.0  === pow(-2.0, 0)
--2.0  === pow(-2.0, 1)
- 4.0  === pow(-2.0, 2)
+ 1  === pow(-2.0, 0)
+-2  === pow(-2.0, 1)
+ 4  === pow(-2.0, 2)
  1.0  === pow(-1.0,-2)
 -1.0  === pow(-1.0,-1)
- 1.0  === pow(-1.0, 0)
--1.0  === pow(-1.0, 1)
- 1.0  === pow(-1.0, 2)
+ 1  === pow(-1.0, 0)
+-1  === pow(-1.0, 1)
+ 1  === pow(-1.0, 2)
  TRUE === is_infinite(pow( 0.0,-2))
  TRUE === is_infinite(pow( 0.0,-1))
- 1.0  === pow( 0.0, 0)
- 0.0  === pow( 0.0, 1)
- 0.0  === pow( 0.0, 2)
+ 1  === pow( 0.0, 0)
+ 0  === pow( 0.0, 1)
+ 0  === pow( 0.0, 2)
  1.0  === pow( 1.0,-2)
  1.0  === pow( 1.0,-1)
- 1.0  === pow( 1.0, 0)
- 1.0  === pow( 1.0, 1)
- 1.0  === pow( 1.0, 2)
+ 1  === pow( 1.0, 0)
+ 1  === pow( 1.0, 1)
+ 1  === pow( 1.0, 2)
  0.25 === pow( 2.0,-2)
  0.5  === pow( 2.0,-1)
- 1.0  === pow( 2.0, 0)
- 2.0  === pow( 2.0, 1)
- 4.0  === pow( 2.0, 2)
+ 1  === pow( 2.0, 0)
+ 2  === pow( 2.0, 1)
+ 4  === pow( 2.0, 2)
  2.0  === pow(   4, 0.5)
  2.0  === pow( 4.0, 0.5)
  3.0  === pow(  27, 1/3)
@@ -126,16 +126,16 @@
 LONG_MIN+1 === pow(LONG_MIN+1,1)
 (LONG_MAX-1)*(LONG_MAX-1) ~== pow(LONG_MAX-1,2)
 (LONG_MIN+1)*(LONG_MIN+1) ~== pow(LONG_MIN+1,2)
-(float)(LONG_MAX-1) === pow(LONG_MAX-1,1.0)
-(float)(LONG_MIN+1) === pow(LONG_MIN+1,1.0)
+(LONG_MAX-1) === pow(LONG_MAX-1,1.0)
+(LONG_MIN+1) === pow(LONG_MIN+1,1.0)
 (LONG_MAX-1)*(LONG_MAX-1) ~== pow(LONG_MAX-1,2.0)
 (LONG_MIN+1)*(LONG_MIN+1) ~== pow(LONG_MIN+1,2.0)
 LONG_MAX === pow(LONG_MAX,1)
 LONG_MIN === pow(LONG_MIN,1)
 LONG_MAX*LONG_MAX ~== pow(LONG_MAX,2)
 LONG_MIN*LONG_MIN ~== pow(LONG_MIN,2)
-(float)LONG_MAX === pow(LONG_MAX,1.0)
-(float)LONG_MIN === pow(LONG_MIN,1.0)
+LONG_MAX === pow(LONG_MAX,1.0)
+LONG_MIN === pow(LONG_MIN,1.0)
 LONG_MAX*LONG_MAX ~== pow(LONG_MAX,2.0)
 LONG_MIN*LONG_MIN ~== pow(LONG_MIN,2.0)
 TESTS;
Index: ext/standard/tests/math/pow_basiclong_64bit.phpt
--- ext/standard/tests/math/pow_basiclong_64bit.phpt Base (BASE)
+++ ext/standard/tests/math/pow_basiclong_64bit.phpt Locally Modified (Based On LOCAL)
@@ -340,9 +340,9 @@
 --- testing: -9223372036854775807, -9223372036854775808 ---
 float(0)
 --- testing: -9.2233720368548E+18, 0 ---
-float(1)
+int(1)
 --- testing: -9.2233720368548E+18, 1 ---
-float(-9.2233720368548E+18)
+int(-9223372036854775808)
 --- testing: -9.2233720368548E+18, -1 ---
 float(-1.0842021724855E-19)
 --- testing: -9.2233720368548E+18, 7 ---
@@ -358,7 +358,7 @@
 --- testing: -9.2233720368548E+18, -2147483648 ---
 float(0)
 --- testing: -9.2233720368548E+18, 9223372036854775807 ---
-float(INF)
+float(-INF)
 --- testing: -9.2233720368548E+18, -9223372036854775808 ---
 float(0)
 ===DONE===
Index: ext/standard/tests/math/pow_variation3.phpt
--- ext/standard/tests/math/pow_variation3.phpt Locally New
+++ ext/standard/tests/math/pow_variation3.phpt Locally New
@@ -0,0 +1,45 @@
+--TEST--
+Test pow() function : usage variations - casting to int where possible
+--FILE--
+<?php
+/* Prototype  : number pow  ( number $base  , number $exp  )
+ * Description: Exponential expression.
+ * Source code: ext/standard/math.c
+ */
+
+echo "*** Testing pow() : casting to int ***\n";
+
+$tests = array(
+	array(1, 1),
+	array((float) 1, 1),
+	array(1, (float) 1),
+	array((float) 1, (float) 1),
+	array(10, 100),
+);
+
+foreach ($tests as $k => $test) {
+	echo "\n-- Iteration " . ($k + 1) . " --\n";
+	var_dump(pow($test[0], $test[1]));
+}
+
+
+?>
+===Done===
+--EXPECTF--
+*** Testing pow() : casting to int ***
+
+-- Iteration 1 --
+int(1)
+
+-- Iteration 2 --
+int(1)
+
+-- Iteration 3 --
+int(1)
+
+-- Iteration 4 --
+int(1)
+
+-- Iteration 5 --
+float(1.0E+100)
+===Done===
\ No newline at end of file
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 17:01:58 2024 UTC