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

From: Date: Tue, 15 Mar 2016 13:16:58 +0000
Subject: Bug #71816 [Opn->Nab]: Assigning literal equivalent of PHP_INT_MIN results in double
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-199838@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 Updated by: danack@php.net Reported by: patrick at patrickmcdougle dot com Summary: Assigning literal equivalent of PHP_INT_MIN results in double -Status: Open +Status: Not a bug 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: >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. Previous Comments: ------------------------------------------------------------------------ [2016-03-15 04:47:17] 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-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

« previous php.bugs (#199838) next »