Hi CLC,
Suppose I want to evaluate (x*y)/z where x,y,z are unsigned int's.
In some cases, though (x*y) might overflow the (xy)/z (the actual
product) might be representable as an unsigned int, I was wondering
how to get the correct answer in such a case.
In one specific case the answer is easy to see : x is the only "large"
number among the three- y and z are small.
Then calculate the answer as (x/z)*y + ((x -(x/z)*z)*y)/z
The following program illustrates my point:
#include <stdio.h>
#include <limits.h>
typedef unsigned int uint;
int
main(void)
{
uint x=(UINT_MAX-1)/2,y=4,z=5,w;
w=(x*y)/z;
printf("x=%u y=%u z=%u w=%u\n",x,y,z,w);
w= (x/z)*y + (x - (x/z)*z )*y/z;
printf("x=%u y=%u z=%u w=%u\n",x,y,z,w);
return 0;
}
I was wondering, if the above trick can be extended. Are there some
other ways to deal with such overflows?
TIA, 2 1333
"MWB" <ma********************@yahoo.com> wrote in message
news:f4**************************@posting.google.c om... Hi CLC, Suppose I want to evaluate (x*y)/z where x,y,z are unsigned int's. In some cases, though (x*y) might overflow the (xy)/z (the actual product) might be representable as an unsigned int, I was wondering how to get the correct answer in such a case. In one specific case the answer is easy to see : x is the only "large" number among the three- y and z are small. Then calculate the answer as (x/z)*y + ((x -(x/z)*z)*y)/z
I would cast to a double for the calculation, and then back to unsigned int.
This is likely to be faster, and easier to understand.
Allan
Hiho, Suppose I want to evaluate (x*y)/z where x,y,z are unsigned int's. In some cases, though (x*y) might overflow the (xy)/z (the actual product) might be representable as an unsigned int, I was wondering how to get the correct answer in such a case.
1) How to arrive at optimal x,y,z:
If you want to do it "right" without subdividing into special cases, you
calculate the greatest common divisor a of x and z and reduce the
fraction (x*y)/z to (x1*y)/z1 where x1 = x/a, z1 = z/a and repeat that
arriving at x1*y1/z2, where y1 = y/b, z2 = z1/b and b is gcd(y, z1).
The greatest common divisor can be calculated by using the Euclidean
algorithm. In your case, you can do without some of the tests but
the algo looks like that in C for ints (the while loop is the algorithm
itself:
int gcd (int a, int b)
{
int r;
/* get nonnegative numbers: Works only for a,b>=-INT_MAX */
if (a < 0)
a = -a;
if (b < 0)
b = -b;
// force a >= b
if (b > a) {
r = a; a = b; b = r;
}
// exclude special case
if (b == 0) {
if (a)
return(a);
else // gcd(0, 0) ist unendlich gross...
return(-1);
}
// Euclidean algorithm without saving the coefficients
while ( (r = a % b) ) {
a = b;
b = r;
}
return(b);
}
If you really want to apply tricks, then sort x1, y1 in a way you need
them.
In one specific case the answer is easy to see : x is the only "large" number among the three- y and z are small. Then calculate the answer as (x/z)*y + ((x -(x/z)*z)*y)/z The following program illustrates my point:
#include <stdio.h> #include <limits.h>
typedef unsigned int uint;
int main(void) { uint x=(UINT_MAX-1)/2,y=4,z=5,w; w=(x*y)/z; printf("x=%u y=%u z=%u w=%u\n",x,y,z,w); w= (x/z)*y + (x - (x/z)*z )*y/z; printf("x=%u y=%u z=%u w=%u\n",x,y,z,w); return 0; }
I was wondering, if the above trick can be extended. Are there some other ways to deal with such overflows?
TIA,
2) "Tricks"
After having reduced the fraction, you can do as suggested and go
to types which can hold the product.
C99:
If ULLONG_MAX >= UINT_MAX*UINT_MAX, then I would use unsigned long long
for the actual computation. This has the advantage of yielding the
correct result which can be tested against UINT_MAX.
C89:
If you won't go up to UINT_MAX but only to some value MAX and this
value can be represented by less than or equal to half the bits of your
double mantissa, that is for n mantissa bits MAX*MAX<=pow(2,n)-1 holds,
then you can use double. Even if that is not true, you can at least
make some predictions about the quality of your computations and have
the advantage of readable code.
long double is of course also an option :-)
If the range of values you need is too large for both of these
ways, then use two unsigneds to store the result of x*y, one high
and one low "word". Just have a look around on the web.
Cheers
Michael This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Karthik |
last post by:
Hi,
I am writing this application that needs a lot of arithmetic
calculations.
I was wondering if C++ language specifies any way of detecting
arithmetic overflows.
Let us consider the following...
|
by: datapro01 |
last post by:
Running DB2 8.1.6A on AIX 5.1
We are experience package cache overflows.
The high water mark for package cache is showing as 16,108,513 bytes,
or
approximately 3933 4K pages.
The package...
|
by: hikums |
last post by:
I have taken a table snapshot, and noticed two tables with high
overflows. What should be done, if any.
Table Schema = CASTING
Table Name = ABC
Table Type = User
Data...
|
by: db2udbgirl |
last post by:
Table level snapshot showed me a entry as below
Table Schema = CARD
Table Name = DEALER
Table Type = User
Data Object Pages = 3480
Index Object Pages = 2622
Rows...
|
by: David Beardsley |
last post by:
I'm working with several stored procedures processing a large volume of
data. After monitoring their execution using Quest's Spotlight
functionality I noticed a fair number of overflow accesses...
|
by: Yevgen Muntyan |
last post by:
Hey,
Is it correct that number of value bits in int and unsigned
int representation may be the same? If it is so, then INT_MIN
may be -(INT_MAX+1) (in mathematical sense), i.e. abs(INT_MIN)
is...
|
by: Racerx |
last post by:
Hi All,
I use db2 v8.1 on AIX 5L
Evrytikme I take a snapshot for one my databases I can see that there
are Hash join overflows and small hash join overflows..
Jus need to know what shud i...
|
by: Arquitecto |
last post by:
Hi ,i would like to give u a piece of code and discuss if it is
possible to call the hidden_function without modify code to call it .
I have read on internet about buffer overflows and i can...
|
by: krishna81m |
last post by:
I know that in C++ if we create two char arrays char ch1; ch2; and then do strcpy(ch1,"Hello"); this is wrong and if ch1 and ch2 are allocated one by the other, the memory copies part of the array...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |