How to multiply two ints and check
robison at m.cs.uiuc.edu
robison at m.cs.uiuc.edu
Thu May 18 08:46:00 AEST 1989
Re: "How to multiply two ints and check"
There is a much simpler, faster, and machine-independent way to check
for multiplication overflow --- check the product by division. To wit:
int vmul(n1,n2,p_ovf)
int n1,n2;
int *p_ovf;
{
int product = n1*n2;
if( n2!=0 && product/n2 != n1 )
*p_ovf = YES;
else
*p_ovf = NO;
return product;
}
This code doesn't need to know sizeof(int), and even works on decimal machines!
Arch D. Robison
University of Illinois at Urbana-Champaign
UUCP: {pur-ee,convex}!uiucdcs!robison
Internet: robison at CS.UIUC.EDU
More information about the Comp.lang.c
mailing list