php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #71816 Assigning literal equivalent of PHP_INT_MIN results in double
Submitted: 2016-03-14 02:26 UTC Modified: 2016-03-15 13:16 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: patrick at patrickmcdougle dot com Assigned:
Status: Not a bug Package: *General Issues
PHP Version: 7.0.4 OS: Ubuntu Server 64-bit
Private report: No CVE-ID: None
 [2016-03-14 02:26 UTC] 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


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-03-14 20:59 UTC] 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-15 04:47 UTC] patrick at patrickmcdougle dot com
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?
 [2016-03-15 13:16 UTC] danack@php.net
-Status: Open +Status: Not a bug
 [2016-03-15 13:16 UTC] danack@php.net
>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?

That is my understanding. The constants are there both to have set values, and to make it easy to use them in code, without having to worry about how the source code is going to be compiled to running code.

It's the same as other constants like NAN and INF that have a known and set value, but those values are also inconvenient to express in PHP source code - and so using the constants is better.

I'm going to set this issue to "Not a bug", as I don't think there's anything to address, and everything is working as designed. Feel free to re-open the issue if you think there is something that needs to be addressed.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Wed May 15 20:01:35 2024 UTC