This is weird...

  • Thread starter Thread starter Brian
  • Start date Start date
B

Brian

Look at the code below. All three variables are initialized to the same
value, -9223372036854775808. The first two work OK but the third throws a
overflow error. Why? (Framework v1.0.3705)

'-- OK, no overflow
Dim i as Long = Long.MinValue
Dim k as Long = CLng(-2 ^ 63)

'-- Overflow error
Dim m as Long = -9223372036854775808
 
Brian said:
Look at the code below. All three variables are initialized to the same
value, -9223372036854775808. The first two work OK but the third throws a
overflow error. Why? (Framework v1.0.3705)

'-- OK, no overflow
Dim i as Long = Long.MinValue
Dim k as Long = CLng(-2 ^ 63)

'-- Overflow error
Dim m as Long = -9223372036854775808

You should add the literal type charecter 'L'.
Something like -9223372036854775808L
 
The 1st example returns a Long that is well in range.
The 2nd example tries to convert the number to long since you explicitly
told it to do so using CLng(operation). The computer doesn't try figure
out exact value at compile time and because you told it to use CLng() to
change whatever number resulted into a long it assumes you know what you
are doing so it doesn't complain.
The 3nd example is obvious to the compiler the number you typed in will
never behave as the value as it is entered so it's an error.
 
Rafael Pivato said:
You should add the literal type charecter 'L'.
Something like -9223372036854775808L

Also doesn't work here (VB 2003). Seems to be a code editor bug.
This works: m = &H8000000000000000
 
Ting Liang (MSFT) said:
The 3nd example is obvious to the compiler the number you typed in

thirdond? ;-)
will never behave as the value as it is entered so it's an error.

Isn't it a Long literal within the valid range for Long literals?



Seem to be still the same code editor programmers like for VB6: Try this in
VB6:

Dim l As Long
l = -2147483648&

It also doesn't work.

*LOL*
 
hmmmmmm...



I think that the compiler misunderstood.

He is trying to make negative the absolute value, which is an overflow for
Long.

In this case you should use the hexadecimal literal 0x8000000000000000
(&H8000000000000000 in VB)





---

Rafael Pivato





Brian said:
Nope, already tried to append the L.


throws
 
(e-mail address removed) (Ting Liang (MSFT)) scripsit:
The 3nd example is obvious to the compiler the number you typed in will
never behave as the value as it is entered so it's an error.

I don't understand that. -9223372036854775807 is in the range of the
Long datatype. When specifying it in hex format, it will work.
 
Back
Top