Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Wrap around arithmetic

Status
Not open for further replies.

xwb

Programmer
Jul 11, 2002
6,828
GB
How do I do wraparound arithmetic without the system throwing an exception? I wish to create a sort of hash function based on time of day
Code:
dim secs as integer
dim hash as integer
secs = hour(now) * 3600 + minute(now) * 60 + second(now)
hash = secs * 9977553311 * 2244660088

It woks in C and Fortran but not in VB. Is there a way of saying just do it, and ignore exceptions.
 
Ignore the exception? What good would that do you? You are overflowing your integer. Use something like a Double instead of an Integer.
 
But in general, here is how to handle exceptions in VB.Net

Code:
Try
  'Put some code here which may cause an exception
Catch ex As Exception
  'Put some code here to handle that exception, or put nothing.  -- Note, you can just use Catch (without ex As Exception) if you do not need to examine the ex or any other derived exception type instance.  
Finally
  'Put some code here to occur regardless if an exception occurs (optional)
End Try
 
I know there will be an arithmetic overflow. What I want it to do is ignore the overflow/truncation and just give me the end result. For example, if we have 4 bit words

10 + 9 = 19 = 13 hex
with the truncation, this would end up as 3. If I use a double, I might loose precision due to rounding and truncation
 
If the number can exceed the type first you are doing it wrong. If you really want to do it in spite of that then you need to use another type. You have strings, objects, Double, Int64, UInt32/64(if the result can only be positive, etc. I suggest just reading up on VB.Net data types.

I have to admit it has been a long time since I've programmed in C, but I don't remember you being able to exceed data type limitations.

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Is this what you're looking for?
Project Properties > Compile > Advanced Compile Options... > Remove integer overflow checks
 
You can also use the Decimal data type. It WILL hold very large values. I tested it both with vs 2008 vb.net and vs 2010 vb.net.
 
Thanks DaveInIowa.

On 2003 (which probably nobody uses), it is Configuration Properties > Optimizations > Remove ...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top