473,396 Members | 1,965 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.

Integer overflow handling on P3 vs. P4

Sample code:

static void Main(string[] args)
{
double inf = 0.1 / 0.0;
int test2 = (int)(inf * 0.0F);
Console.WriteLine( test2.ToString() );
}

On a P3 this gives the result "0", on a P4 it gives -2147483648 which is
int.MinValue.

Anyone know why this happens? Is it .Net which compiled wrongly on a P4,
or is it the P4 which is erroneus?

Of course if I compile with overflow checking it throws an exception,
and it's good practise to check the value before you divide with it. (I
made this example out from a graph component I downloaded which failed
on a P4.)

Regards,
Mikael Svenson
Nov 15 '05 #1
9 1820
surely either result is acceptable since either result is incorrect because
the answer is undefined.

why be too bothered about how processors divide zero when it's something
that should never occur?

LATER! =o)
Dan.

"Mikael Svenson" <mi*****@powertech.no> wrote in message
news:3F***************@powertech.no...
Sample code:

static void Main(string[] args)
{
double inf = 0.1 / 0.0;
int test2 = (int)(inf * 0.0F);
Console.WriteLine( test2.ToString() );
}

On a P3 this gives the result "0", on a P4 it gives -2147483648 which is
int.MinValue.

Anyone know why this happens? Is it .Net which compiled wrongly on a P4,
or is it the P4 which is erroneus?

Of course if I compile with overflow checking it throws an exception,
and it's good practise to check the value before you divide with it. (I
made this example out from a graph component I downloaded which failed
on a P4.)

Regards,
Mikael Svenson
Nov 15 '05 #2
I'm not that bothered :) but it is still interesting. And mathematically
0 should be the correct answer. We've also seen a strange rounding error
at work, where 2.24 becomes 2.2400000021. That's on a P4 as well.
Haven't tried it on a P3 yet. So could there possible be P4 bug ;)

-m

Daniel Bass wrote:

surely either result is acceptable since either result is incorrect because
the answer is undefined.

why be too bothered about how processors divide zero when it's something
that should never occur?

LATER! =o)
Dan.

"Mikael Svenson" <mi*****@powertech.no> wrote in message
news:3F***************@powertech.no...
Sample code:

static void Main(string[] args)
{
double inf = 0.1 / 0.0;
int test2 = (int)(inf * 0.0F);
Console.WriteLine( test2.ToString() );
}

On a P3 this gives the result "0", on a P4 it gives -2147483648 which is
int.MinValue.

Anyone know why this happens? Is it .Net which compiled wrongly on a P4,
or is it the P4 which is erroneus?

Of course if I compile with overflow checking it throws an exception,
and it's good practise to check the value before you divide with it. (I
made this example out from a graph component I downloaded which failed
on a P4.)

Regards,
Mikael Svenson

Nov 15 '05 #3
I'm not that bothered :) but it is still interesting. And mathematically
0 should be the correct answer. We've also seen a strange rounding error
at work, where 2.24 becomes 2.2400000021. That's on a P4 as well.
Haven't tried it on a P3 yet. So could there possible be P4 bug ;)

-m

Daniel Bass wrote:

surely either result is acceptable since either result is incorrect because
the answer is undefined.

why be too bothered about how processors divide zero when it's something
that should never occur?

LATER! =o)
Dan.

"Mikael Svenson" <mi*****@powertech.no> wrote in message
news:3F***************@powertech.no...
Sample code:

static void Main(string[] args)
{
double inf = 0.1 / 0.0;
int test2 = (int)(inf * 0.0F);
Console.WriteLine( test2.ToString() );
}

On a P3 this gives the result "0", on a P4 it gives -2147483648 which is
int.MinValue.

Anyone know why this happens? Is it .Net which compiled wrongly on a P4,
or is it the P4 which is erroneus?

Of course if I compile with overflow checking it throws an exception,
and it's good practise to check the value before you divide with it. (I
made this example out from a graph component I downloaded which failed
on a P4.)

Regards,
Mikael Svenson

Nov 15 '05 #4
Mikael Svenson <mi*****@powertech.no> wrote:
I'm not that bothered :) but it is still interesting. And mathematically
0 should be the correct answer. We've also seen a strange rounding error
at work, where 2.24 becomes 2.2400000021.
I wouldn't be convinced that that's a rounding error until I'd seen
more details - see
http://www.pobox.com/~skeet/csharp/floatingpoint.html
That's on a P4 as well.
Haven't tried it on a P3 yet. So could there possible be P4 bug ;)


Actually, I don't think it's a bug. If you look at the C# language
specifications, 0.1/0.0 should be +infinity, and then
0*infinity should be NaN. The tricky bit is what the integer cast
should do. The ECMA spec (section 13.2.1) says (about double to integer
conversions):

<quote>
In an unchecked context, the conversion always succeeds, and proceeds
as follows:

If the value of the source operand is within the range of the
destination type, then it is rounded towards zero to the nearest
integral value of the destination type, and this integral value is the
result of the conversion.

Otherwise, the result of the conversion is an unspecified value of the
destination type.
</quote>

Now, as far as I can see, NaN *isn't* within the range of int, so the
result is an unspecified value - in other words, there's no wrong
answer.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #5
Mikael Svenson <mi*****@powertech.no> wrote:
I'm not that bothered :) but it is still interesting. And mathematically
0 should be the correct answer. We've also seen a strange rounding error
at work, where 2.24 becomes 2.2400000021.
I wouldn't be convinced that that's a rounding error until I'd seen
more details - see
http://www.pobox.com/~skeet/csharp/floatingpoint.html
That's on a P4 as well.
Haven't tried it on a P3 yet. So could there possible be P4 bug ;)


Actually, I don't think it's a bug. If you look at the C# language
specifications, 0.1/0.0 should be +infinity, and then
0*infinity should be NaN. The tricky bit is what the integer cast
should do. The ECMA spec (section 13.2.1) says (about double to integer
conversions):

<quote>
In an unchecked context, the conversion always succeeds, and proceeds
as follows:

If the value of the source operand is within the range of the
destination type, then it is rounded towards zero to the nearest
integral value of the destination type, and this integral value is the
result of the conversion.

Otherwise, the result of the conversion is an unspecified value of the
destination type.
</quote>

Now, as far as I can see, NaN *isn't* within the range of int, so the
result is an unspecified value - in other words, there's no wrong
answer.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #6
Thanks for the answer :)

But it's still weird that the same binary produces different result on a
P3 and a P4 with the same Framework installed.

-m

"Jon Skeet [C# MVP]" wrote:

Mikael Svenson <mi*****@powertech.no> wrote:
I'm not that bothered :) but it is still interesting. And mathematically
0 should be the correct answer. We've also seen a strange rounding error
at work, where 2.24 becomes 2.2400000021.


I wouldn't be convinced that that's a rounding error until I'd seen
more details - see
http://www.pobox.com/~skeet/csharp/floatingpoint.html
That's on a P4 as well.
Haven't tried it on a P3 yet. So could there possible be P4 bug ;)


Actually, I don't think it's a bug. If you look at the C# language
specifications, 0.1/0.0 should be +infinity, and then
0*infinity should be NaN. The tricky bit is what the integer cast
should do. The ECMA spec (section 13.2.1) says (about double to integer
conversions):

<quote>
In an unchecked context, the conversion always succeeds, and proceeds
as follows:

If the value of the source operand is within the range of the
destination type, then it is rounded towards zero to the nearest
integral value of the destination type, and this integral value is the
result of the conversion.

Otherwise, the result of the conversion is an unspecified value of the
destination type.
</quote>

Now, as far as I can see, NaN *isn't* within the range of int, so the
result is an unspecified value - in other words, there's no wrong
answer.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 15 '05 #7
Mikael Svenson <mi*****@powertech.no> wrote:
Thanks for the answer :)

But it's still weird that the same binary produces different result on a
P3 and a P4 with the same Framework installed.


Maybe - maybe not. It may well be something to do with how the NaNs are
stored in memory - there are many different potential bit patterns for
NaN, and it could well be that the P4 differentiates between different
NaNs more or less than the P3. Certainly an interesting thing to see
though :)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #8
It's not always the case that you want to treat divide by zero as a special
case that your code should handle. In some cases (ray tracer geommetry, for
example), the INF result you get from dividing by zero is the desired result
and continues to function correctly in the calculations. So you can get a
decent speed up on a tight looped algorithm by removing a zero check.

Niall

"Daniel Bass" <da********@NOSPAMpostmaster.co.uk> wrote in message
news:O9*************@TK2MSFTNGP12.phx.gbl...
surely either result is acceptable since either result is incorrect because the answer is undefined.

why be too bothered about how processors divide zero when it's something
that should never occur?

LATER! =o)
Dan.

"Mikael Svenson" <mi*****@powertech.no> wrote in message
news:3F***************@powertech.no...
Sample code:

static void Main(string[] args)
{
double inf = 0.1 / 0.0;
int test2 = (int)(inf * 0.0F);
Console.WriteLine( test2.ToString() );
}

On a P3 this gives the result "0", on a P4 it gives -2147483648 which is
int.MinValue.

Anyone know why this happens? Is it .Net which compiled wrongly on a P4,
or is it the P4 which is erroneus?

Of course if I compile with overflow checking it throws an exception,
and it's good practise to check the value before you divide with it. (I
made this example out from a graph component I downloaded which failed
on a P4.)

Regards,
Mikael Svenson

Nov 15 '05 #9
The P4 has a totally new architecture for floating point (as well as some of
the P3 circuitry for the older instructions). I vaguely recall someone
asking about what SSE2 instructions the JIT uses, and the answer was that it
only uses them for casts and shifts or some such thing. This is a pretty
vague recollection, but if it's correct, it could explain the difference in
results, considering that SSE2 is new to the P4.

Niall

"Mikael Svenson" <mi*****@powertech.no> wrote in message
news:3F***************@powertech.no...
Thanks for the answer :)

But it's still weird that the same binary produces different result on a
P3 and a P4 with the same Framework installed.

-m

"Jon Skeet [C# MVP]" wrote:

Mikael Svenson <mi*****@powertech.no> wrote:
I'm not that bothered :) but it is still interesting. And mathematically 0 should be the correct answer. We've also seen a strange rounding error at work, where 2.24 becomes 2.2400000021.


I wouldn't be convinced that that's a rounding error until I'd seen
more details - see
http://www.pobox.com/~skeet/csharp/floatingpoint.html
That's on a P4 as well.
Haven't tried it on a P3 yet. So could there possible be P4 bug ;)


Actually, I don't think it's a bug. If you look at the C# language
specifications, 0.1/0.0 should be +infinity, and then
0*infinity should be NaN. The tricky bit is what the integer cast
should do. The ECMA spec (section 13.2.1) says (about double to integer
conversions):

<quote>
In an unchecked context, the conversion always succeeds, and proceeds
as follows:

If the value of the source operand is within the range of the
destination type, then it is rounded towards zero to the nearest
integral value of the destination type, and this integral value is the
result of the conversion.

Otherwise, the result of the conversion is an unspecified value of the
destination type.
</quote>

Now, as far as I can see, NaN *isn't* within the range of int, so the
result is an unspecified value - in other words, there's no wrong
answer.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 15 '05 #10

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

Similar topics

2
by: REH | last post by:
If the is_modulo field of the numeric_limits class is true for signed integer types, can I assume that overflow for such types is defined behavior? If so, is the behavior the same regardless of...
25
by: Ashutosh Iddya | last post by:
Hi , I am performing an integer count of a particular operation in my program. After a sufficiently large value an overflow occurs. At the moment I have gone around the problem by declaring it...
25
by: junky_fellow | last post by:
Is there any way by which the overflow during addition of two integers may be detected ? eg. suppose we have three unsigned integers, a ,b, c. we are doing a check like if ((a +b) > c) do...
9
by: Chris Botha | last post by:
Hi, I have an UInt32 and want to stick the value into an Integer and get an Overflow exception, BUT using C# the same value can be casted into an int and the value is as expected. The Hex value is...
40
by: Robert Seacord | last post by:
The CERT/CC has released a beta version of a secure integer library for the C Programming Language. The library is available for download from the CERT/CC Secure Coding Initiative web page at:...
13
by: Freaker85 | last post by:
Hello, I am new at programming in C and I am searching a manner to parse a string into an integer. I know how to do it in Java, but that doesn't work in C ;o) I searched the internet but I...
31
by: Pesso | last post by:
What happens if you multiple two integers and the result overflows the MAX_INT in C? Is there a way to trap the condition when it happens?
42
by: thomas.mertes | last post by:
Is it possible to use some C or compiler extension to catch integer overflow? The situation is as follows: I use C as target language for compiled Seed7 programs. For integer computions the C...
6
by: Chris Becke | last post by:
I *know* my CPU has opcodes that can do this - when adding (or subtracting) there is a carry flag that can be invoked to make the result essentially 1 bit longer than the data size used in...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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.