473,804 Members | 2,127 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Compare two multiples which could overflow

I have three values:

UInt64 a, b, c, d;

I need to determine if: (a*b) <=(c*d). Naturally, just doing the
multiplication and comparing would tell me the result. However, the
values could potentially overflow. For example, if:

a = UInt64.MaxValue ;
b = 2;
c = UInt64.MaxValue / 3;
d = 4;

I still need to be able to conclude ab cd. I can determine if
wrapping will occur on multiplication by doing:

if (a <= 1 || b <= 1)
{
return false;
}

return ((UInt64.MaxVal ue / a) < b);

But it still doesn't help me determine ab cd. Anybody have any
thoughts on how I could determine this? Thanks.
Oct 3 '08
10 1525
ag*******@gmail .com wrote:
On Oct 4, 6:53*am, "Rudy Velthuis" <newsgro...@rve lthuis.dewrote:
Very simple. They are like numbers in base n instead of base 10. Say
the first number is u + v*n + w*n*n and the other one is x + y*n +
z*n*n. First we must compare w*n*n with z*n*n. But, if w*n*n >
z*n*n, then w z, so we only have to compare w and z, and we can
skip the n*n on both sides (since n*n is positive). If they are
equal, we must compare v and y, and if they are equal too, we
compare u and x. Of course this only works because all factors (u,
v, w, x, y, z) < n(*)

Hey Rudy,

Thanks for explaining. I studied this quite a bit, and I am pretty
sure I understand the approach you are taking here. Basically, how I
evaluated it is:

a*d <=c*b
is
(aL+aH*n)*(dL+d H*n) <=(cL+cH*n)*(bL +bH*n)
is
(aL*dL)+(aL*dH* n)+(aH*n*dL)+(a H*n*dH*n) <=>
(cL*bL)+(cL*bH* n)+(cH*n*bL) +(cH*n*bH*n)

Break each side into three pieces:

1) High product: aH*dH <=cH*bH
2) Medium product: aL*dH+aH*dL <=cL*bH+cH*bL
3) Low product: aL*dL <=cL*bL

On the medium product, n was elimated simply by dividing each side by
n:

((aL*dH*n)+(aH* n*dL))/n <=((cL*bH*n)+(c H*n*bL))/n

That is how we get: aL*dH+aH*dL <=cL*dH+cH*dL

I think the only question I have left is regarding your bitshifting.
I understand why you are bitshifting (in the worst case scenario, the
sum of each medium product could overflow). However, I don't
understand why are are adding the bits and only checking for 2. For
example, you have:
Adding two bits can have 3 possible results: 0, 1, and 2 (binary: 0 1
and 10). Only 2 will not fit in one bit, and the overflow must be added
to rest of the Medium Product.
--
Rudy Velthuis http://rvelthuis.de

"The average person thinks he isn't." -- Father Larry Lorenzoni
Oct 7 '08 #11

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

Similar topics

37
25374
by: Claude Gagnon | last post by:
Hi, How can we compare sign of numbers in C++ ? Bye, Claude
2
2920
by: david | last post by:
hi: The file can be PDF or Word format. Any help? thx
14
15558
by: purifier | last post by:
Hi everyone, I thought of writing a program which finds the greater of two numbers.. of course with a small condition... without using "if" and conditional operators... I got an idea on how to implement the program... I have an alternative too... that is to use bitwise comparison in the folowing manner: Accept the input Subtract one number from the other Check for the MSB(Most Significant Bit) in the result... See if the result is...
122
5353
by: Einar | last post by:
Hi, I wonder if there is a nice bit twiddling hack to compare a large number of variables? If you first store them in an array, you can do: for (i = 0; i < n; i++) { if (array != value) { /* array differs from value, do something*/
49
14915
by: raju | last post by:
hi can we compare two integers without using relational operators (== != < <= > >=) thanks rajesh s
3
1638
by: Stiemied | last post by:
Working on a Access query to pay multiples of 2 hours of time worked by 75. My problem is I'm not sure how to exclude or remove time that is not a multiple of 2 (round up or down any decimal to nearest whole number). Any help would be greatly appreciated. (/120)*75
1
1430
Wiccadwitch
by: Wiccadwitch | last post by:
Could someone please help??? I'm using paypal's shopping cart which allows for 2 options (such as colors or sizes). I need up to six options with 40 or so colors to choose from. I tried adding 6 but in paypal.com cart it only displays #'s 1 and 6. 2-5 get cut out. Paypal tech gave a website of www.members.aol.com/paypalhelper and said to create select multiples. I did this but there's still something wrong with my code. I can select...
5
1284
by: Patrick A | last post by:
All, I need a little help understanding which concepts / functions / methods / strategies I should use to accomplish what I'll describe below. Note - the real implementation isn't about music - I just thought it would be easier to understand, if not more fun to think about. I have a table with 8,000 rows. Let's call that the "TBL_Fav_Songs" table. It contains a list of my favorite songs, in the column |
3
1917
by: stateemk | last post by:
Hi, I'm probably making this much harder than it needs to be, but I just can't think of how to do this. Each year, a database is created with all employees on it. I need to pull all the employees from the last five years and delete any duplicates. Each table has the exact same fields. So basically, I'm trying to combine tables from 2005, 2006, 2007, 2008 and 2009. Most of the employees will stay the same from year to year, so by combining...
0
9712
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10343
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10089
tracyyun
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9171
agi2029
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7634
isladogs
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6862
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5530
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5673
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4308
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.