Is it a correct or dependable method of detecting
overflow in signed integers?
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
/* non-zero if overflow, else 0 */
static int
overflow ( int a, int b )
{
return a+b < a || a+b < b;
}
int
main ( void )
{
int a = INT_MAX - 10,
b = INT_MAX - 20;
printf ( "%s\n", overflow ( a, b ) ? "Overflow" : "Normal" );
return EXIT_SUCCESS;
}
--
Vijay Kumar R Zanvar
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
----- Original Message -----
From: "Fred L. Kleinschmidt" <fred.l.kleinschmidt@boeing.com>
Newsgroups: comp.lang.c
To: "Vijay Kumar R Zanvar" <vijoeyz@hotpop.com>
Sent: Friday, March 12, 2004 11:12 PM
Subject: Re: signed int overflow
Vijay Kumar R Zanvar wrote:
>
> Is it a correct or dependable method of detecting
> overflow in signed integers?
>
> #include <stdio.h>
> #include <stdlib.h>
> #include <limits.h>
>
> /* non-zero if overflow, else 0 */
> static int
> overflow ( int a, int b )
> {
> return a+b < a || a+b < b;
> }
>
> int
> main ( void )
> {
> int a = INT_MAX - 10,
> b = INT_MAX - 20;
>
> printf ( "%s\n", overflow ( a, b ) ? "Overflow" : "Normal" );
> return EXIT_SUCCESS;
> }
int overflow( int a, int b )
{
if ( a < 0 ) {
return (b < INT_MIN - a);
}
else {
return (b > INT_MAX - a);
}
}
--
Fred L. Kleinschmidt
Boeing Associate Technical Fellow
Technical Architect, Common User Interface Services
M/S 2R-94 (206)544-5225
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Vijay Kumar R Zanvar wrote:
>
> Is it a correct or dependable method of detecting
> overflow in signed integers?
>
> #include <stdio.h>
> #include <stdlib.h>
> #include <limits.h>
>
> /* non-zero if overflow, else 0 */
> static int
> overflow ( int a, int b )
> {
> return a+b < a || a+b < b;
> }
>
> int
> main ( void )
> {
> int a = INT_MAX - 10,
> b = INT_MAX - 20;
>
> printf ( "%s\n", overflow ( a, b ) ? "Overflow" : "Normal" );
> return EXIT_SUCCESS;
> }
The correct way to do it, is without summing a plus b.
If there is signed int overflow, then you have undefined behavior.
--
pete
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"Vijay Kumar R Zanvar" <vijoeyz@hotpop.com> wrote in message
news:c2s8se$21cltj$1@ID-203837.news.uni-berlin.de...
> Is it a correct or dependable method of detecting
> overflow in signed integers?
>
> /* non-zero if overflow, else 0 */
> static int
> overflow ( int a, int b )
> {
> return a+b < a || a+b < b;
> }
No. You can't detect whether a+b overflows _after_ you've added them
because you've already invoked undefined behaviour. Try...
return (a < 0) ? (b < INT_MIN - a)
: (b > INT_MAX - a);
--
Peter
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Vijay Kumar R Zanvar wrote:
>
> Is it a correct or dependable method of detecting
> overflow in signed integers?
>
> #include <stdio.h>
> #include <stdlib.h>
> #include <limits.h>
>
> /* non-zero if overflow, else 0 */
> static int
> overflow ( int a, int b )
> {
> return a+b < a || a+b < b;
> }
>
> int
> main ( void )
> {
> int a = INT_MAX - 10,
> b = INT_MAX - 20;
>
> printf ( "%s\n", overflow ( a, b ) ? "Overflow" : "Normal" );
> return EXIT_SUCCESS;
> }
Incorrect. The results of any overflow are undefined behaviour.
Try (untested):
int overflowonadd(int a, int b)
{
int temp;
if ((a >= 0) && (b < 0)) overflow = 0;
else if ((b >= 0) && (a < 0)} overflow = 0;
else {
/* signs same, potential overflow */
}
}
You go on from there. This will involve forming differences from
INT_MAX and INT_MIN and comparing the results with the other
operand. Things depend on the signs.
--
Chuck F (cbfalconer@yahoo.com) (cbfalconer@worldnet.att.net)
<http://cbfalconer.home.att.net>; USE worldnet address!