|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #79955 converting large floats to int leads to overflow or the arithmetical unit
Submitted: 2020-08-11 20:20 UTC Modified: 2020-08-11 20:35 UTC
From: peter dot fargas at informatik-handwerk dot de Assigned: cmb (profile)
Status: Not a bug Package: Math related
PHP Version: 8.0.0beta1 OS:
Private report: No CVE-ID: None
 [2020-08-11 20:20 UTC] peter dot fargas at informatik-handwerk dot de
(int](PHP_MAX_INT + 1) returns \PHP_INT_MIN.
Increasing the argument passed, zero result can be achieved.

Same goes for the \intval() function.

This happens for all current PHP versions - reported by

7.2.0 - 7.2.33, 7.3.0 - 7.3.21, 7.4.0 - 7.4.9
8.0.0alpha1 - beta1

Test script:

echo '---- INTMAX vs INTMAX+1'. \PHP_EOL;
echo '---- INTMAX to float and back'. \PHP_EOL;
echo '---- INTMAX+1 to float and back'. \PHP_EOL;
echo '---- ...rendezvous point is...'. \PHP_EOL;

Expected result:
one of:
- \PHP_MAX_INT (just as the documentation states), a sign stable result
- type error
- zero

Actual result:
overflow or the arithmetical unit


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2020-08-11 20:35 UTC]
-Status: Open +Status: Not a bug -Assigned To: +Assigned To: cmb
 [2020-08-11 20:35 UTC]
From the docs[1]:

| If PHP encounters a number beyond the bounds of the integer
| type, it will be interpreted as a float instead. Also, an
| operation which results in a number beyond the bounds of the
| integer type will return a float instead.

and (float to int conversion):

| If the float is beyond the boundaries of integer (usually +/-
| 2.15e+9 = 2^31 on 32-bit platforms and +/- 9.22e+18 = 2^63 on
| 64-bit platforms other than Windows prior to PHP 7), the result is
| undefined, since the float doesn't have enough precision to give
| an exact integer result. No warning, not even a notice will be
| issued when this happens!

[1] <>
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Feb 25 03:01:28 2024 UTC