Arithmetic Overflow problem when hashing

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Apr 2007
Posts: 110
Reputation: kylcrow is an unknown quantity at this point 
Solved Threads: 2
kylcrow kylcrow is offline Offline
Junior Poster

Arithmetic Overflow problem when hashing

 
0
  #1
May 29th, 2009
Hey guys, I am having issues understanding how exactly to mod something to avoid overflow.

say i have this hash function:

  1. int universalHash(const string &x,int B) {
  2. long long sum;
  3. long long power;
  4. sum=0;
  5. pow = 1;
  6. for (int i=0;i<(signed)x.length();i++) {
  7. // cout << "sum= " << sum << endl;
  8. // cout << "pow= " << pow << endl;
  9. sum = sum + (x[i]-'a') * power;
  10. power = power*26;
  11. }
  12. return sum;
  13. }

This is wrong because it will cause an overflow so I have to do this equation Mod B (where B is the table size (997) and the string I am passing in is the letters a through p in order.

I found that these are the rules for mod B arithmetic:

x + y Mod B = ((x Mod B) + (y Mod B)) Mod B
x * y Mod B = ((x Mod B) * (y Mod B)) Mod B


Here was my attempt that was not giving the correct answer (28).

  1. int universalHash(const string &x,int B) {
  2. long long sum;
  3. long long pow;
  4. sum=0;
  5. pow = 1;
  6. for (int i=0;i<(signed)x.length();i++) {
  7. cout << "sum= " << sum << endl;
  8. cout << "pow= " << pow << endl;
  9. sum += (((x[i]-'a')%B) * (pow%B))%B;
  10. pow = ((pow%B)*(26%B));
  11. }
  12. return sum;
  13. }
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: Arithmetic Overflow problem when hashing

 
0
  #2
May 29th, 2009
If you want to obtain hash index in a table of size B, don't forget:
1. Make unsigned int calculations ( x[i]-a is signed value).
2. Result is sum % B.
3. As usually, no need to avoid integer overflow.
May be better don't invent a square wheel?
Are you familiar with http://www.partow.net/programming/hashfunctions/?
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC