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 32-bit
 PHP Version:        5.4.3
 Block user comment: N
 Private report:     N

 New Comment:

Yeah originally I had compiled my own PHP using the flags in the first post. I 
re-downloaded php-5.4.3.tar.bz2 from PHP.net and just did a basic ./configure 
&& make then ran log(8,2) using sapi/cli/php test.php and the output was still 
the same incorrect result:

float(3)
int(2)

However I have just tried the same thing on my other machine running 32bit 
Ubuntu 10.04 with 5.4.3 and it outputs correctly:

float(3)
int(3)

So maybe there's a screw loose/bad memory in the first machine or something. I 
don't have an explanation for it. Probably not a bug after all, sorry!


Previous Comments:
------------------------------------------------------------------------
[2012-06-07 09:14:24] [email protected]

That's interesting because that is exactly what is happening when you call 
PHP's 
log() function. log(8,2) in PHP ends up being log(8)/log(2) in C. Did you 
compile 
your PHP yourself? If not, could you try grabbing the 5.4.3 tarball and doing a 
simple: ./configure && make
Building just the cli version is enough. Then run your log(8,2) test with 
sapi/cli/php test.php

------------------------------------------------------------------------
[2012-06-07 08:48:37] zuallauz at gmail dot com

Hmm, I get the same output with 64 zeros:

3.0000000000000000000000000000000000000000000000000000000000000000

------------------------------------------------------------------------
[2012-06-07 08:08:25] [email protected]

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.

------------------------------------------------------------------------
[2012-06-07 07:50:15] zuallauz at gmail dot com

Actually I may just use the code below now that I've been reading up on the 
perils of converting from float to int in PHP:

$num = log(8, 2);
$num = (int) number_format($num, 0, '.', '');

------------------------------------------------------------------------
[2012-06-07 07:25:04] zuallauz at gmail dot com

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?

------------------------------------------------------------------------


The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at

    https://bugs.php.net/bug.php?id=62240


-- 
Edit this bug report at https://bugs.php.net/bug.php?id=62240&edit=1

Reply via email to