Path: utzoo!attcan!utgpu!jarvis.csri.toronto.edu!rutgers!tut.cis.ohio-state.edu!unmvax!deimos.cis.ksu.edu!uxc!uxc.cso.uiuc.edu!m.cs.uiuc.edu!robison From: robison@m.cs.uiuc.edu Newsgroups: comp.lang.c Subject: Re: How to multiply two ints and check Message-ID: <4700038@m.cs.uiuc.edu> Date: 17 May 89 22:46:00 GMT References: <434@skye.ed.ac.uk> Lines: 25 Nf-ID: #R:skye.ed.ac.uk:434:m.cs.uiuc.edu:4700038:000:574 Nf-From: m.cs.uiuc.edu!robison May 17 17:46:00 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@CS.UIUC.EDU