473,387 Members | 1,863 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,387 software developers and data experts.

checking for overflow

Hi,
Is this code which checks if a and b when multiplied overflow correct?

/*begin code*/
#include <stdio.h>
#include <limits.h>

int check_overflow(unsigned long a,unsigned long b)
{
unsigned long t=ULONG_MAX/b;
unsigned long t1;
t1=ULONG_MAX-t*b;
if ( (a > t) || ((a == t) && (t1 > 0)) ) return 1;
return 0;
/* return ( (a > t) || ((a == t) && (t1 > 0)) );*/
}
/*end code*/

thanks in advance,
Arin
Nov 14 '05 #1
3 1948
On Sat, 5 Jun 2004, Arin Chaudhuri wrote:
Hi,
Is this code which checks if a and b when multiplied overflow correct?

/*begin code*/
#include <stdio.h>
#include <limits.h>

int check_overflow(unsigned long a,unsigned long b)
{
unsigned long t=ULONG_MAX/b;
unsigned long t1;
t1=ULONG_MAX-t*b;
if ( (a > t) || ((a == t) && (t1 > 0)) ) return 1;
return 0;
/* return ( (a > t) || ((a == t) && (t1 > 0)) );*/
}
/*end code*/

thanks in advance,
Arin

My mistake.
Checking (a>t) should be sufficient.
Sorry.

#include <stdio.h>
#include <limits.h>

int check_overflow(unsigned long a,unsigned long b)
{
unsigned long t=ULONG_MAX/b;
return (t>a);
}
Arin

Nov 14 '05 #2

On Sat, 5 Jun 2004, Arin Chaudhuri wrote:
[snip]

I am having a bad day, I meant
#include <stdio.h>
#include <limits.h>

int check_overflow(unsigned long a,unsigned long b)
{
unsigned long t=ULONG_MAX/b;
return (t<a);
}
Arin
Nov 14 '05 #3
Arin Chaudhuri wrote:
On Sat, 5 Jun 2004, Arin Chaudhuri wrote:
[snip]

I am having a bad day, I meant
#include <stdio.h>
#include <limits.h>

int check_overflow(unsigned long a,unsigned long b)
{
unsigned long t=ULONG_MAX/b;
return (t<a);
}


This looks almost right: It returns 1 if the product
of `a' and `b' would exceed `ULONG_MAX', or 0 if it would
not. However, it will malfunction if `b' is zero. A
possible fix:

int check_overflow(unsigned long a, unsigned long b) {
return b > 0 && a > ULONG_MAX / b;
}

--
Er*********@sun.com

Nov 14 '05 #4

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

Similar topics

7
by: Jim Cook | last post by:
We have a macro which takes various index constants as an argument and offsets into an array. The macro can be an Lvalue or Rvalue. The index is not zero based. I would like a compile time error...
4
by: Victor | last post by:
Hello, I've got a situation in which the number of (valid) recursive calls I make will cause stack overflow. I can use getrlimit (and setrlimit) to test (and set) my current stack size. ...
0
by: jacob navia | last post by:
I have added overflow checking as an optional feature of lcc-win32. This means integer addition/subtraction/multiplication are tested for overflow. If the overflow is detected, I call a user...
14
by: sathya_me | last post by:
Dear clc, I have a variable void *a; Since variable "a" can be assigned (point to) any type and also any type can be assigned to "a" (i.e means "a" = any typed variable; any typed variable =...
8
by: Ian Pilcher | last post by:
When adding two values of type time_t, how can I check for overflow? Maybe I'm just brain-cramped today, but I can't figure out how to do it. Thanks! --...
2
by: Marlene Stebbins | last post by:
I am entering numbers into my program from the command line. I want to check whether they are > INT_MAX. Sounds simple, but I've discovered that if(x <= INT_MAX) { /* use x in some calculation...
6
by: Sean Capstick | last post by:
Is there a way to stop overflow checking at runtime in VB.NET? I have one math calculation that must behave like another system, it could generate a number > 2 billion (<4) which then needs to go...
66
by: Johan Tibell | last post by:
I've written a piece of code that uses sockets a lot (I know that sockets aren't portable C, this is not a question about sockets per se). Much of my code ended up looking like this: if...
2
by: jou00jou | last post by:
Hi, I have trouble using sscanf and fgets to check for overflow. I will post the assignment specification so I could help whoever would kindly like to offer his/her help. ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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...

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.