473,396 Members | 2,011 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.

Differentr Values for intval(float)

Consider the following code:

$BB = -2181087916;
$AA = (int)$BB;
$AA = intval($BB);

On some systems, $AA will be int(-2147483648), which is actually
consistent with the documentation.

On most systems, however, $AA will be int(2113879380), which is the
same value truncated at 32 bits.

I actually need the latter behavior, as it needs to duplicate Delphi
code, which, like C, behaves the same way.

This is encryption code that does a bunch of calculations on signed
32-bit numbers and ignores any overflows.

Any ideas how I can do this?

--Bruce
Feb 18 '06 #1
2 1524
Bruce wrote:
Consider the following code:

$BB = -2181087916;
$AA = (int)$BB;
$AA = intval($BB);

On some systems, $AA will be int(-2147483648), which is actually
consistent with the documentation.

On most systems, however, $AA will be int(2113879380), which is the
same value truncated at 32 bits.

I actually need the latter behavior, as it needs to duplicate Delphi
code, which, like C, behaves the same way.

This is encryption code that does a bunch of calculations on signed
32-bit numbers and ignores any overflows.

Any ideas how I can do this?

--Bruce


It's never a good idea to depend on hardware dependent features like the
handling of overflow. They have a tendency to break when you move to a
different hardware platform.

I guess you could do some testing and bit manipulation on it, but that's
adding more complexity to the code (and more chances for it to break).

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Feb 18 '06 #2
On 2006-02-18, Bruce <bv********@gmail.com> wrote:
Consider the following code:

$BB = -2181087916;
$AA = (int)$BB;
$AA = intval($BB);

On some systems, $AA will be int(-2147483648), which is actually
consistent with the documentation.

On most systems, however, $AA will be int(2113879380), which is the
same value truncated at 32 bits.

I actually need the latter behavior, as it needs to duplicate Delphi
code, which, like C, behaves the same way.

This is encryption code that does a bunch of calculations on signed
32-bit numbers and ignores any overflows.

Any ideas how I can do this?


after that do this.

$AA= (( $AA + 2147483648 ) & 4294967295) - 2147483648 ;

it should give the result you want on 64-bit machines
and be harmless on 32-bit machines.

Bye.
Jasen
Feb 19 '06 #3

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

Similar topics

11
by: Christian Christmann | last post by:
Hi, just a general question. Which data type is more precise for large values: float or double? Thank you. Chris
34
by: Sona | last post by:
I need to find a minimum of three float values.. what would be the most efficient way of doing this? Can someone please share a #define macro or something with me for doing this? Thanks Sona
7
by: Johnathan Doe | last post by:
I can google search to find the range of values that can be represented in a float by reading up on the IEEE std, but is that the same as how many distinct values that can go in a float type? ...
6
by: Dave win | last post by:
Hi all: I'm confused with the expression "(float *())". Book says that this is a cast. But, I have no idea of this expr. why could this expr ignore the variable??? Thanx!!!
6
by: Anders Würtz | last post by:
i have an assignment to iterate through a collection containing different types of numeric values (float, double, int, byte, short etc.) and to add 1 to all of them. I tried with array and...
60
by: Erick-> | last post by:
hi all... I've readed some lines about the difference between float and double data types... but, in the real world, which is the best? when should we use float or double?? thanks Erick
1
by: psbasha | last post by:
Hi , I have a list as shown below, when access the list at particular location ,say pointList.The values what I have stored is different to the output.There is a decimal precision change in the...
23
by: JDT | last post by:
Hi, It seems that using floats as the first tupelo for an STL map (shown below) can cause problems but I don't remember what the problems were. Is it common practice to use a structure like...
1
by: Gabriel Genellina | last post by:
En Fri, 29 Aug 2008 14:41:53 -0300, Ron Brennan <brennan.ron@gmail.com> escribi�: What is a "multipart"? I know MIME multipart messages but they don't seem to apply here... From your other...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...

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.