473,396 Members | 1,774 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 1876

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: pj | last post by:
Why does M$ Query Analyzer display all numbers as positive, no matter whether they are truly positive or negative ? I am having to cast each column to varchar to find out if there are any...
5
by: Subrahmanyam Arya | last post by:
Hi Folks , I am trying to solve the problem of reading the numbers correctly from a serial line into an intel pentium processor machine . I am reading 1 byte and 2byte data both positive...
11
by: tlyczko | last post by:
Hello Rob B posted this wonderful code in another thread, http://groups.google.com/group/comp.lang.javascript/browse_thread/thread/c84d8538025980dd/6ead9d5e61be85f0#6ead9d5e61be85f0 I could not...
17
by: Sri | last post by:
How do you add an n-bit number in C? Regards, Sri
15
by: jaks.maths | last post by:
How to convert negative integer to hexadecimal or octal number? Ex: -568 What is the equivalent hexadecimal and octal number??
11
by: drtimhill | last post by:
I'm just starting out on Python, and am stumped by what appears an oddity in the way negative indices are handled. For example, to get the last character in a string, I can enter "x". To get the...
39
by: Frederick Gotham | last post by:
I have a general idea about how negative number systems work, but I'd appreciate some clarification if anyone would be willing to help me. Let's assume we're working with an 8-Bit signed integer,...
1
by: sconnors | last post by:
I am currently working on an application that runs on Windows XP SP2 with .NET 2.0, Linux 2.4.19-16mdkenterprise and Solaris 5.9. The language is C++. When I compare the output numbers, Windows...
20
by: Casey | last post by:
Is there an easy way to use getopt and still allow negative numbers as args? I can easily write a workaround (pre-process the tail end of the arguments, stripping off any non-options including...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.