|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2012-06-06 08:28 UTC] zuallauz at gmail dot com
Description: ------------ Ok this code correctly works on 5.3.3 but I have since upgraded to 5.4.3 and it now incorrectly casts the float to an integer. It occurs when converting the output from the log() function to an integer. It should be a simple float to int conversion. However in the current stable PHP it outputs the wrong integer for no apparent reason. I have compiled PHP 5.4.3 release with the following if that helps: ./configure --with-zlib --with-gd --with-jpeg-dir --with-png-dir --enable-gd-native-ttf --with-freetype-dir --enable-bcmath --enable-sockets --with-openssl --with-pdo-mysql=mysqlnd --with-mysql-sock=/var/run/mysqld/mysqld.sock --enable-mbstring --enable-mbregex --with-curl --with-apxs2=/usr/local/apache2/bin/apxs --with-config-file-path=/usr/local/apache2/conf I consider this a critical bug so please fix ASAP. Test script: --------------- $logarithm = log(8, 2); var_dump($logarithm); $int = (int) $logarithm; var_dump($int); Expected result: ---------------- Expected output as it is in PHP 5.3.3: float(3) int(3) Actual result: -------------- Actual output in PHP 5.4.3: float(3) int(2) PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Tue Dec 16 23:00:01 2025 UTC |
Well, float to int conversion issues isn't a PHP-specific thing. Try this little C program on your machine, for example: #include <stdio.h> #include <math.h> int main(char *argv[], int argc) { printf("%.64f\n",log(8)/log(2)); } If you name the file, "a.c" you can just type: make a then run it with: ./a My output on 64-bit Ubuntu: 10:07am x220:~> ./a 3.0000000000000000000000000000000000000000000000000000000000000000 It would be interesting to see your output.Strange. The PHP log function just looks like this (from ext/standard/math.c): PHP_FUNCTION(log) { double num, base = 0; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "d|d", &num, &base) == FAILURE) { return; } if (ZEND_NUM_ARGS() == 1) { RETURN_DOUBLE(log(num)); } if (base <= 0.0) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "base must be greater than 0"); RETURN_FALSE; } if (base == 1) { RETURN_DOUBLE(php_get_nan()); } else { RETURN_DOUBLE(log(num) / log(base)); } } Since you are calling it as log(8,2) you are hitting the last case there. So the only code executed is: RETURN_DOUBLE(log(num) / log(base)); And the RETURN_DOUBLE macro just sets the return value to the double returned by dividing those two log calls. There should be no difference between the little test program and PHP here. Although.. I think gcc might be playing tricks on us here because I used a constant. Try this instead: #include <stdio.h> #include <math.h> int main(char *argv[], int argc) { double base = 2.0; double num = 8.0; printf("%.64f\n",log(num)/log(base)); } Then compile it using: gcc a.c -o a -lm Do you get the same result on both machines?