| | |
Arithmetic Overflow problem when hashing
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Apr 2007
Posts: 110
Reputation:
Solved Threads: 2
Hey guys, I am having issues understanding how exactly to mod something to avoid overflow.
say i have this hash function:
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).
say i have this hash function:
c++ Syntax (Toggle Plain Text)
int universalHash(const string &x,int B) { long long sum; long long power; sum=0; pow = 1; for (int i=0;i<(signed)x.length();i++) { // cout << "sum= " << sum << endl; // cout << "pow= " << pow << endl; sum = sum + (x[i]-'a') * power; power = power*26; } return sum; }
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).
c++ Syntax (Toggle Plain Text)
int universalHash(const string &x,int B) { long long sum; long long pow; sum=0; pow = 1; for (int i=0;i<(signed)x.length();i++) { cout << "sum= " << sum << endl; cout << "pow= " << pow << endl; sum += (((x[i]-'a')%B) * (pow%B))%B; pow = ((pow%B)*(26%B)); } return sum; }
If you want to obtain hash index in a table of size B, don't forget:
1. Make unsigned int calculations (
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/?
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/?
![]() |
Similar Threads
- Why did I still get the error "Divide overflow" (Assembly)
- help with c++ coursework - code so far included. (C++)
- User controls stick when Tables overflow (ASP.NET)
- Help with e^x problem please. (C++)
- Factorial problem? (Java)
- Expanding Box Problem (HTML and CSS)
- Index was out of range. Must be non-negative and less than the size of the collection (VB.NET)
- Help - Problem with array (C)
- can't start computer (Windows 95 / 98 / Me)
Other Threads in the C++ Forum
- Previous Thread: how to convert text fine to bin file
- Next Thread: Very Basic Encryption
| Thread Tools | Search this Thread |
api array based beginner binary c++ c/c++ calculator char char* class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






