Bug #62240 [Nab]: 5.4.3 regression, converting from float to int gives incorrect output

From: Date: Thu, 07 Jun 2012 07:25:04 +0000
Subject: Bug #62240 [Nab]: 5.4.3 regression, converting from float to int gives incorrect output
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-171090@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=62240&edit=1 ID: 62240 User updated by: zuallauz at gmail dot com Reported by: zuallauz at gmail dot com Summary: 5.4.3 regression, converting from float to int gives incorrect output Status: Not a bug Type: Bug Package: *General Issues -Operating System: Ubuntu 12.04 +Operating System: Ubuntu 12.04 32-bit PHP Version: 5.4.3 Block user comment: N Private report: N New Comment: I have put in a quick hack for now by converting from float to string first then converting to int. This seems to get the correct output: $num = (int) (string) log(8, 2); var_dump($num); Output: int(3) I don't think the problem lies in the log() function at all as that correctly retrieves the float(3) result. It's the conversion from the float to integer that is doing the incorrect calculation. Likely the log(8, 2) does not result in a perfect 3, it might actually be 2.9999999999999999 so there could be a conversion error happening. My test machine is running Ubuntu server 32-bit, so maybe the 64-bit edition has enough precision to do the integer conversion properly. I was talking to a colleague today and he mentioned there were some float to integer optimisations done in 5.3.11 that changed things? Previous Comments: ------------------------------------------------------------------------ [2012-06-06 14:05:13] rasmus@php.net Yes, I doubt upgrading to 5.4.4 is likely to fix your case. I have no idea why your libmath log() call isn't returning a more accurate value here. I tested this on a 64-bit Ubuntu 12.04 and couldn't reproduce it. ------------------------------------------------------------------------ [2012-06-06 12:57:40] cataphract@php.net The only change in log() between these two versions is the handling of the case where base == 1, as you can see with: git diff php-5.3.3 php-5.4.3 -- ext/standard/math.c log() is taken from math.h, so I guess the explanation here is that you were using a different libm. ------------------------------------------------------------------------ [2012-06-06 08:53:43] zuallauz at gmail dot com Ok, so is 5.4.4 likely to be released this month? I will have to put a manual hack in my app until it is released I think. ------------------------------------------------------------------------ [2012-06-06 08:41:31] rasmus@php.net I am not able to reproduce this in PHP 5.4.4 ------------------------------------------------------------------------ [2012-06-06 08:28:54] 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) ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=62240&edit=1

« previous php.bugs (#171090) next »