Article View: comp.std.c
Article #4640Safe Multiply, std ok.
From: Norman Hamer
Date: Thu, 11 Jan 1996 15:04
Date: Thu, 11 Jan 1996 15:04
20 lines
968 bytes
968 bytes
Daniel- Sure there's a way. It's slow and evil (at least in this
incarnation), but it makes a lot more sense for floats...
#include <math.h>
#include <limits.h>
int bad_mult(int num1, int num2)
{
return (log(abs(num1)) * log(abs(num2)) > log(INT_MAX)) ? 1 : 0;
}
... now, this'll give one false negative (if the two numbers multiply to
produce INT_MIN on a 2's complement machine or something where INT_MIN isn't
real close in magnitude to INT_MAX), but that could be taken care of by
slight additional code to check signs and compare against log(abs(INT_MIN))
if necessary.
On the other hand, this is really going to slow down simple integer
multiplies. ;)
Technical Details
Message-ID:
<646916@mavenry.altcit.eskimo.com>Path:
arc1-ca.pugleaf.net!news.sprintlink.net!eskimo!mavenryShow Full Headers
Complete Article Headers
From: maven@mavenry.altcit.eskimo.com (Norman Hamer) Message-ID: <646916@mavenry.altcit.eskimo.com> Path: arc1-ca.pugleaf.net!news.sprintlink.net!eskimo!mavenry Newsgroups: comp.std.c Subject: Safe Multiply, std ok. X-AltCit-ID: 646916 Date: Thu, 11 Jan 96 15:04:37 GMT