Valhalla Legends Forums Archive | .NET Platform | Overflow problem [VB.net]

AuthorMessageTime
Zeller
When given the values (all intagers)

a=650820
b=4974863
c=41
d=165

I get an overflow error on this line
[code]
Dim myInt As Integer = ((a - b) * 500) / c / d
[/code]

I cant change these values going into the formula. When I do this in the calculater it comes out to -319589 which should fit into an integer. I need a way to make this formula work. help
May 15, 2004, 2:34 PM
Adron
[quote author=Zeller link=board=37;threadid=6814;start=0#msg60225 date=1084631675]
When given the values (all intagers)

a=650820
b=4974863
c=41
d=165

I get an overflow error on this line
[code]
Dim myInt As Integer = ((a - b) * 500) / c / d
[/code]

I cant change these values going into the formula. When I do this in the calculater it comes out to -319589 which should fit into an integer. I need a way to make this formula work. help
[/quote]

I'm not sure about VB.NET, but in regular VB, an integer is only 16 bits. It has a range from -32768 to 32767.
May 15, 2004, 2:53 PM
Zeller
The Int64 value type represents integers with values ranging from negative 9,223,372,036,854,775,808 through positive 9,223,372,036,854,775,807


Even when I made myInt an 64-bit integer I still got on overflow error.

EDIT: I fixed the problem by making a and b int64's. I wonder why that fixed it though :-\
May 15, 2004, 2:59 PM
Adron
[quote author=Zeller link=board=37;threadid=6814;start=0#msg60228 date=1084633154]
The Int64 value type represents integers with values ranging from negative 9,223,372,036,854,775,808 through positive 9,223,372,036,854,775,807


Even when I made myInt an 64-bit integer I still got on overflow error.

EDIT: I fixed the problem by making a and b int64's. I wonder why that fixed it though :-\
[/quote]

The intermediate results of the calculation have to be in range too.

(a - b) * 500 = 2162021500 > 2147483647 > 32767

2147483647 is the limit for a 32-bit signed integer and 32767 is the limit for a 16-bit signed integer. Your value is just above the limit for 32-bit signed. If you want to an exact integer calculation, you need either unsigned 32-bit integers or signed 64-bit integers.
May 15, 2004, 3:34 PM
K
I don't think it would be wise to use unsigned 32bit integers here, since we're subtracting b from a where b > a.
May 15, 2004, 3:49 PM
Adron
Oh, right. Nothing to stop him from swapping them and implying the sign though.
May 15, 2004, 4:24 PM
Myndfyr
The problem is due to the fact that the middle operation (mult. by 500) results in an overflow on the Int32 type (which is the underlying type mapped to by Integer).

-2162021500

The valid range for Int32 is:

-2147483648 to 2147483647

Use the type Long.

Alternatively, you could calculate (a - b) * (500 / c / d). However, since you have rounding, you'll probably come up with 0. I suggest using type Decimal if you want to approach it this way.
May 15, 2004, 11:52 PM

Search