Bug #71816 [Opn]: Assigning literal equivalent of PHP_INT_MIN results in double

From: Date: Tue, 15 Mar 2016 04:47:18 +0000
Subject: Bug #71816 [Opn]: Assigning literal equivalent of PHP_INT_MIN results in double
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-199828@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=71816&edit=1

 ID:                 71816
 User updated by:    patrick at patrickmcdougle dot com
 Reported by:        patrick at patrickmcdougle dot com
 Summary:            Assigning literal equivalent of PHP_INT_MIN results
                     in double
 Status:             Open
 Type:               Bug
 Package:            *General Issues
 Operating System:   Ubuntu Server 64-bit
 PHP Version:        7.0.4
 Block user comment: N
 Private report:     N

 New Comment:

So in order to have a variable set to PHP_INT_MIN you have to either set the variable to the
constant or set it to one more than PHP_INT_MIN (as a literal) then subtract one?


Previous Comments:
------------------------------------------------------------------------
[2016-03-14 20:59:22] danack@php.net

Your code is equivalent to doing:

$foo = 9223372036854775808;
$foo = -$foo;

The number on the first line is above PHP_INT_MAX - and so it gets converted to a float....before
being converted to a negative value.

That probably is the correct behaviour, compared to trying to make it resolved at compile time
because having the two lines:

$foo = -9223372036854775808;
$foo = -(9223372036854775808);

resulting in different behaviour would be even more surprising.

------------------------------------------------------------------------
[2016-03-14 02:26:10] patrick at patrickmcdougle dot com

Description:
------------
Assigning a variable the PHP_INT_MIN literal to a variable results in a double instead of an
integer. I would expect the min representable integer to remain an integer. See below.

Test script:
---------------
vagrant@vagrant-ubuntu-trusty-64:~$ php -v
PHP 7.0.4-1+deb.sury.org~trusty+1 (cli) ( NTS )
Copyright (c) 1997-2016 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies
    with Zend OPcache v7.0.6-dev, Copyright (c) 1999-2016, by Zend Technologies
vagrant@vagrant-ubuntu-trusty-64:~$ php -a
Interactive mode enabled

php > echo PHP_INT_MIN; 
-9223372036854775808
php > $foo = -9223372036854775808;
php > print_r($foo); 
-9.2233720368548E+18
php > echo gettype($foo); 
double
php > $foo = -9223372036854775807;
php > print_r($foo);
-9223372036854775807
php > echo gettype($foo);
integer



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



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


Thread (4 messages)

« previous php.bugs (#199828) next »