469,607 Members | 1,882 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,607 developers. It's quick & easy.

adding negative numbers on Solaris

I get an unexpected result when I add two negative numbers in PHP on
SPARC/Solaris 8. The same program works fine on Intel/Linux. I'm using
PHP 4.3.1 on both systems.

Here is my program, test.php.

<?php
$a = 0x81234567;
$b = 0xF0000002;
printx($a, $b);
printx($a, intval($b));
printx(intval($a), $b);
printx(intval($a), intval($b));

function printx($a, $b) {
$c = $a + $b;
printf("%08X (%u) %7s + %7s = %7s\n", $c, $c,
gettype($a), gettype($b), gettype($c));
}
?>
I run it from the shell prompt like this:
php -f test.php

Output on Intel/Linux, PHP 4.3.1

71234569 (1898136937) double + double = double
71234569 (1898136937) double + integer = double
71234569 (1898136937) integer + double = double
71234569 (1898136937) integer + integer = double

This is the answer that I expect. I am adding two 32-bit integers, and the
value overflows.

Here is what happens when I run it on
SPARC/Solaris 8, PHP 4.3.1

FFFFFFFF (4294967295) double + double = double
71234569 (1898136937) double + integer = double
71234569 (1898136937) integer + double = double
80000000 (2147483648) integer + integer = double

When both arguments are negative doubles or both are
negative integers, then it gives the wrong answer.

What is causing this, and how do I avoid this strange behavior?
Jul 17 '05 #1
0 1683

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Subrahmanyam Arya | last post: by
17 posts views Thread by Sri | last post: by
15 posts views Thread by jaks.maths | last post: by
11 posts views Thread by drtimhill | last post: by
39 posts views Thread by Frederick Gotham | last post: by
20 posts views Thread by Casey | last post: by
reply views Thread by guiromero | last post: by
reply views Thread by devrayhaan | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.