PC Review


Reply
Thread Tools Rate Thread

This is weird...

 
 
Brian
Guest
Posts: n/a
 
      8th Oct 2003
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


 
Reply With Quote
 
 
 
 
Rafael Pivato
Guest
Posts: n/a
 
      8th Oct 2003

"Brian" <(E-Mail Removed)> escreveu na mensagem
news:(E-Mail Removed)...
> 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


----
Rafael Pivato


 
Reply With Quote
 
 
 
 
Brian
Guest
Posts: n/a
 
      8th Oct 2003
Nope, already tried to append the L.


"Rafael Pivato" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
>
> "Brian" <(E-Mail Removed)> escreveu na mensagem
> news:(E-Mail Removed)...
> > 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
>
>
> ----
> Rafael Pivato
>
>



 
Reply With Quote
 
Ting Liang
Guest
Posts: n/a
 
      8th Oct 2003
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.

 
Reply With Quote
 
Armin Zingler
Guest
Posts: n/a
 
      8th Oct 2003
"Rafael Pivato" <(E-Mail Removed)> schrieb
>
> "Brian" <(E-Mail Removed)> escreveu na mensagem
> news:(E-Mail Removed)...
> > 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


Also doesn't work here (VB 2003). Seems to be a code editor bug.
This works: m = &H8000000000000000


--
Armin

 
Reply With Quote
 
Armin Zingler
Guest
Posts: n/a
 
      8th Oct 2003
"Ting Liang (MSFT)" <(E-Mail Removed)> schrieb

> > > Dim m as Long = -9223372036854775808



> 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*

--
Armin

 
Reply With Quote
 
Rafael Pivato
Guest
Posts: n/a
 
      8th Oct 2003
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" <(E-Mail Removed)> escreveu na mensagem
news:(E-Mail Removed)...
> Nope, already tried to append the L.
>
>
> "Rafael Pivato" <(E-Mail Removed)> wrote in message
> news:%(E-Mail Removed)...
> >
> > "Brian" <(E-Mail Removed)> escreveu na mensagem
> > news:(E-Mail Removed)...
> > > 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
> >
> >
> > ----
> > Rafael Pivato
> >
> >

>
>



 
Reply With Quote
 
Herfried K. Wagner [MVP]
Guest
Posts: n/a
 
      8th Oct 2003
(E-Mail 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.

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
<http://www.mvps.org/dotnet>
 
Reply With Quote
 
Brian
Guest
Posts: n/a
 
      8th Oct 2003
Thanks for all of your input. I found out the cause of this problem. If you
want to check it out yourself go to:

http://support.microsoft.com/default...b;en-us;820657



"Armin Zingler" <(E-Mail Removed)> wrote in message
news:e%(E-Mail Removed)...
> "Rafael Pivato" <(E-Mail Removed)> schrieb
> >
> > "Brian" <(E-Mail Removed)> escreveu na mensagem
> > news:(E-Mail Removed)...
> > > 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

>
> Also doesn't work here (VB 2003). Seems to be a code editor bug.
> This works: m = &H8000000000000000
>
>
> --
> Armin
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off



Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 09:44 AM.