php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #39680 Type Casting from float into int
Submitted: 2006-11-29 10:19 UTC Modified: 2006-11-29 13:40 UTC
From: diefans at googlemail dot com Assigned:
Status: Not a bug Package: *Math Functions
PHP Version: 5.2.0 OS: Linux
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: diefans at googlemail dot com
New email:
PHP Version: OS:

 

 [2006-11-29 10:19 UTC] diefans at googlemail dot com
Description:
------------
since version 5.2.0 when I cast a negative float smaller than -2147483648 into an int all bits are lost and the minimum Integer value is returned. For positive floats larger than the max integer value all is fine.
It is not possible to perform bitwise calculations with negative overflowing values since all usable information is lost.

Reproduce code:
---------------
echo (int) -40000000000;
echo (int) 40000000000;


Expected result:
----------------
-1345294336
1345294336

Actual result:
--------------
-2147483648
1345294336

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-11-29 11:30 UTC] tony2001@php.net
The logic behind this conversion is quite plain:
if (double > LONG_MAX) {
  long = (unsigned long) double;
} else {
  long = (long) double;
}

You can try to compile a simple program in C and see that the result is the same.
 [2006-11-29 12:21 UTC] diefans at googlemail dot com
So all versions prior 5.2.0 are buggy - right?
 [2006-11-29 13:29 UTC] diefans at googlemail dot com
I tested (compiled PHP 5.2.0) the DVAL_TO_LVAL macro (in Zend/zend_operators.c) from version 5.1.4
 and could reproduce expected values. 
So the DVAL_TO_LVAL macro from version 5.2.0 performs not equal as in 5.1.4

#define DVAL_TO_LVAL(d, l) (l) = (d) > LONG_MAX ? (unsigned long) (d) : (long) (d)

please check this issue, if it is intended to be there -
 [2006-11-29 13:40 UTC] tony2001@php.net
>So all versions prior 5.2.0 are buggy - right?
That's right.
 
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Wed Nov 25 09:01:23 2020 UTC