|
-
Sep 14th, 2009, 01:51 PM
#1
Thread Starter
Lively Member
[RESOLVED] error 6 overflow
i'm getting the overflow error for the caltest variable and also for the calc variable, the function, for these numbers, in order 1 through 5
27340, 25660, 30490, 28690, 25550, that are passed to the function
Code:
Function calc(date1Reach As Integer, date2Reach As Integer, date3Reach As Integer, date4Reach As Integer, date5Reach As Integer) As Double
Dim calctest As Double
On Error GoTo err_trap
calctest = (date1Reach + date2Reach + date3Reach + date4Reach) / 4
calc = (((date1Reach + date2Reach + date3Reach + date4Reach) / 4) _
- ((date2Reach + date3Reach + date4Reach + date5Reach) / 4)) _
/ ((date2Reach + date3Reach + date4Reach + date5Reach) / 4)
i dont see how (27340+25660+30490+ 28690)/4 could cause an overflow for a double? and as well for numbers in this range how it could throw an overflow for the longer calculation for the value of the function, calc?
-
Sep 14th, 2009, 01:59 PM
#2
Re: error 6 overflow
Most likely (date1Reach + date2Reach + date3Reach + date4Reach) when added exceeds max integer value. Maybe same reason why calc is overflowing.
One way this can be avoided is by forcing a non-Integer value into the equation:
(0# + date1Reach + date2Reach + date3Reach + date4Reach)
Because 0#, which is a Double, is used in the equation, the entire equation should be interpreted as Double vs Integer. Worth a try
-
Sep 14th, 2009, 02:04 PM
#3
Re: error 6 overflow
As implied by LaVolpe, the problem is the data types that VB uses while doing the calculation - for some reason it will use the same data type as the elements (so in this case Integer) rather than either a larger one (like Long) or the one that the result will be stored in (in this case Double).
In addition to the solution LaVolpe posted, you can convert one of the elements to a larger data type:
Code:
calctest = (CLng(date1Reach) + date2Reach + date3Reach + date4Reach) / 4
-
Sep 14th, 2009, 03:02 PM
#4
Thread Starter
Lively Member
Re: error 6 overflow
thanks very much for the help, that was a weird situation. In the end I changed the function variable list to (date1reach as long, etc) and that solved problem as well.
the numbers originally come from an access db where they are stored as longs, so it was my dumb mistake to cast them as integers in the function call.
from a vb architecture point of view, in retrospect, i guess it makes sense to cast an interim result (within parens) into the largest variable type included in the group, some type of bucket has to be chosen by the program behind the scenes.
-
Sep 14th, 2009, 04:21 PM
#5
Re: error 6 overflow
Now that we've helped you, you can help us by marking the thread as resolved. If you have JavaScript enabled you can do that easily by pulling down the Thread Tools menu and selecting the Mark Thread Resolved item. Also if someone has been particularly helpful you have the ability to affect their forum "reputation" by rating their post.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|