php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #38045 Integer mathematics problem
Submitted: 2006-07-09 09:30 UTC Modified: 2006-07-09 09:40 UTC
From: igor at oleinikov dot ru Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.1.4 OS: FreeBSD 6.1 Release
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 you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: igor at oleinikov dot ru
New email:
PHP Version: OS:

 

 [2006-07-09 09:30 UTC] igor at oleinikov dot ru
Description:
------------
When I want to calculate integer summ of two integers with overflow, I get unexpected result, becouse result converts to float, then converts to integer.

Important: I've tested this example on two systems with same versions of php (5.1.4) and different versions of freebsd (4.11 and 6.1).
On last operation [$b = (int)($a + $a)], freebsd 4.11 produce expected result: int(0), but freebsd 6.1 produce minimal integer (-2147483648).

How can I implement algorithm with integer mathematics, that requires overflows? i have it successfully working on freebsd 4.11, but it don't work on freebsd 6.1 ((

Reproduce code:
---------------
$a = 0x80000000; //i'd like it would integer, but it float ;)
var_dump($a, dechex($a));
$a = (int)0x80000000; //ok, i'll make it integer myself
var_dump($a, dechex($a));
$b = $a + $a; //int + int = int ?
var_dump($b, dechex($b));
$b = (int)($a + $a);
var_dump($b, dechex($b));


Expected result:
----------------
int(-2147483648) string(8) "80000000"
int(-2147483648) string(8) "80000000"
int(0) string(8) "0" //expected, int + int = int with overflow
int(0) string(8) "0" //expected, conversion with overflow

Actual result:
--------------
float(2147483648) string(8) "80000000" // expected int
int(-2147483648) string(8) "80000000" // when I convert to int myself
float(-4294967296) string(8) "80000000" //expected int, but it overflows and converts to float
int(-2147483648) string(8) "80000000"

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-07-09 09:40 UTC] helly@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

32bit
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Thu Dec 04 23:00:01 2025 UTC