Overflow - Am I going crazy? 
Author Message
 Overflow - Am I going crazy?

Why does this give an overflow?

Dim bob as Long
bob = 36001 - (10 * 3600)

I don't understand! Is it VB being stupid or is it me?
Grrrrrrrrrr

Dave


ICQ# 3263311, Fax: +44 (0870) 1640 113
'I think I'll have a lie down. No I won't, I'll go and hit some guests.'
Basil Fawlty, Fawlty Towers
To e-mail me, replace 'nospam' with ' btcf'



Sun, 29 Apr 2001 03:00:00 GMT  
 Overflow - Am I going crazy?
I tried it on my machine, and it also overflows. Just doing bob = 10 * 3600
was enough to overflow. It must be treating it as an integer variable. I
also tried dim bob& , but with the same overflow. Can't help, but at least
you're not alone!
Quote:

>Why does this give an overflow?

>Dim bob as Long
>bob = 36001 - (10 * 3600)

>I don't understand! Is it VB being stupid or is it me?
>Grrrrrrrrrr

>Dave
>' btcf'



Sun, 29 Apr 2001 03:00:00 GMT  
 Overflow - Am I going crazy?
Its VB being stupid - try:

bob = 36001 - CLng(10) * 3600

Quote:

>Why does this give an overflow?

>Dim bob as Long
>bob = 36001 - (10 * 3600)

>I don't understand! Is it VB being stupid or is it me?
>Grrrrrrrrrr

>Dave


>ICQ# 3263311, Fax: +44 (0870) 1640 113
>'I think I'll have a lie down. No I won't, I'll go and hit some guests.'
>Basil Fawlty, Fawlty Towers
>To e-mail me, replace 'nospam' with ' btcf'



Sun, 29 Apr 2001 03:00:00 GMT  
 Overflow - Am I going crazy?

Quote:

>Why does this give an overflow?

>Dim bob as Long
>bob = 36001 - (10 * 3600)

10 and 3600 are both 16-bit integers to VB because it uses the smallest
datatype possible.   When VB does math operations it stores the result in a
temporary variable that matches the size of the largest operand.  The
expression (10*3600) evaluates to 36000 and when VB stores it in the
temporary 16-bit integer it overflows.

Try: 36001 - (10 * 3600&)
Or: 36001 - (Clng(10) * 3600)

As long as you force one operand to be a large enough type the error should
go away.



Sun, 29 Apr 2001 03:00:00 GMT  
 Overflow - Am I going crazy?

Quote:

>Why does this give an overflow?

>Dim bob as Long
>bob = 36001 - (10 * 3600)

>I don't understand! Is it VB being stupid or is it me?

It's VB.  Since the "variables" in the statement are
essentially undeclared, VB treats them as it initially
"sees" them, i.e. the 36001 is a Long, but the 10 and
3600 are Integers.  So, when performing the operations,
VB's result is set to the larger datatype, however since
the 10 and 3600 are considered Integers, VB is trying to
store the result as an integer, and thus since 10 * 3600 >
32767, this causes an overflow.
If you add CLng() around the 10 or the 3600, VB
will convert that operation to a Long.

--
Sam
--
Sam Jones             | Work  : (770) 663-8963x153 | Vote Democrat -
Clientlink, Inc.      | Home  : (770) 399-6210     | It's easier than
3025 Windward Parkway | Alt   : (770) 399-6238     | getting a job.
Alpharetta, Ga. 30005 |
----------------

Pursuant to US Code, Title 47, Chapter 5, Subchapter II, Sec. 227,
any and all nonsolicited commercial E-mail sent to this address is
subject to a download and archival fee in the amount of $500 US.
E-mailing denotes acceptance of these terms.

Quote:
>Grrrrrrrrrr

>Dave


>ICQ# 3263311, Fax: +44 (0870) 1640 113
>'I think I'll have a lie down. No I won't, I'll go and hit some guests.'
>Basil Fawlty, Fawlty Towers
>To e-mail me, replace 'nospam' with ' btcf'



Sun, 29 Apr 2001 03:00:00 GMT  
 Overflow - Am I going crazy?
VB seems to casting the results of ( 10 * 3600) as an integer, since the
most an integer can hold is 32767, 36000 overflows. Also note that 3276 will
pass and 3277 will fail for the same reason. I thought I remembered reading
somewhere that VB held data internally as variant if it was not specifically
typed.
Quote:

> Why does this give an overflow?

> Dim bob as Long
> bob = 36001 - (10 * 3600)

> I don't understand! Is it VB being stupid or is it me?
> Grrrrrrrrrr

> Dave


> ICQ# 3263311, Fax: +44 (0870) 1640 113
> 'I think I'll have a lie down. No I won't, I'll go and hit some guests.'
> Basil Fawlty, Fawlty Towers
> To e-mail me, replace 'nospam' with ' btcf'



Sun, 29 Apr 2001 03:00:00 GMT  
 Overflow - Am I going crazy?

Quote:


> >Why does this give an overflow?

> >Dim bob as Long
> >bob = 36001 - (10 * 3600)

> >I don't understand! Is it VB being stupid or is it me?

> It's VB.  Since the "variables" in the statement are
> essentially undeclared, VB treats them as it initially
> "sees" them, i.e. the 36001 is a Long, but the 10 and
> 3600 are Integers.  So, when performing the operations,
> VB's result is set to the larger datatype, however since
> the 10 and 3600 are considered Integers, VB is trying to
> store the result as an integer, and thus since 10 * 3600 >
> 32767, this causes an overflow.
> If you add CLng() around the 10 or the 3600, VB
> will convert that operation to a Long.

A good rule of thumb when working with explict numbers is to use type
declaration characters to avoid overflow and other unwanted errors...

result = 36001& - (10& * 3600&)

A better rule of thumb is to never do this in the first place.  Use your
calculator to find that this result will always be 1, and declare a
constant.

Constant bob = 1&

Boden Larson
SGM Biotech, Inc.

- Show quoted text -

Quote:
> --
> Sam
> --
> Sam Jones             | Work  : (770) 663-8963x153 | Vote Democrat -
> Clientlink, Inc.      | Home  : (770) 399-6210     | It's easier than
> 3025 Windward Parkway | Alt   : (770) 399-6238     | getting a job.
> Alpharetta, Ga. 30005 |
> ----------------

> Pursuant to US Code, Title 47, Chapter 5, Subchapter II, Sec. 227,
> any and all nonsolicited commercial E-mail sent to this address is
> subject to a download and archival fee in the amount of $500 US.
> E-mailing denotes acceptance of these terms.

> >Grrrrrrrrrr

> >Dave


> >ICQ# 3263311, Fax: +44 (0870) 1640 113
> >'I think I'll have a lie down. No I won't, I'll go and hit some guests.'
> >Basil Fawlty, Fawlty Towers
> >To e-mail me, replace 'nospam' with ' btcf'



Sun, 29 Apr 2001 03:00:00 GMT  
 Overflow - Am I going crazy?


Quote:
>A better rule of thumb is to never do this in the first place.  Use your
>calculator to find that this result will always be 1, and declare a
>constant.

Well they were originally in there as variables but after much tinkering
I realised that the problem still existed with definite numbers. Anyway,
the matter seems to have been cleared up for me now, thanks all
concerned.

Dave

--

ICQ# 3263311, Fax: +44 (0870) 1640 113
A Smith & Wesson beats four aces every time
To e-mail me, replace 'nospam' with ' btcf'



Sun, 29 Apr 2001 03:00:00 GMT  
 Overflow - Am I going crazy?
It is VB being stupid! VB by default places every real number which it comes
across into the lowest value variable into which it will fit - so that it
treats the 10 as though it was an integer (maximum value 3278). However,
when it now multiplies this integer by 3600 (to produce 36000) it finds that
the value exceeds 32768 and so produces an "overflow" error. Silly - I know,
but that is VB for you! The best way to avoid this is to:

(1) Always pre Dimension all variables which you use and state their type
(eg Dim Bob as Long)

(2) Always follow any "real" number by the appropriate sign so as to force
Visual Basic to treat it as a particular type of variable (see "Data Types"
in your help file). The sign for Long is "&" - so your problem will be
resolved if you use: Bob = 36001 - (10& * 3600). I, personally, would always
use:

Dim Bob as Long
Bob = 36001& - (10& * 3600&)

Cheers

Mike

Quote:

>Why does this give an overflow?

>Dim bob as Long
>bob = 36001 - (10 * 3600)

>I don't understand! Is it VB being stupid or is it me?
>Grrrrrrrrrr

>Dave


>ICQ# 3263311, Fax: +44 (0870) 1640 113
>'I think I'll have a lie down. No I won't, I'll go and hit some guests.'
>Basil Fawlty, Fawlty Towers
>To e-mail me, replace 'nospam' with ' btcf'



Mon, 30 Apr 2001 03:00:00 GMT  
 Overflow - Am I going crazy?


Quote:
>I tried it on my machine, and it also overflows. Just doing bob = 10 * 3600
>was enough to overflow. It must be treating it as an integer variable. I
>also tried dim bob& , but with the same overflow. Can't help, but at least
>you're not alone!


>>Why does this give an overflow?

>>Dim bob as Long
>>bob = 36001 - (10 * 3600)

>>I don't understand! Is it VB being stupid or is it me?
>>Grrrrrrrrrr

>>Dave
>>' btcf'

It's not treating bob as an integer variable: it is treating the temporary result (10 * 3600) as an integer.


"For God So Loved The World, That He Gave His
 Only Begotten Son, That Whosoever Believeth
 In Him Should Not Perish, But Have Everlasting
 Life"John3:16  * http://pw2.netcom.com/~cbrtjr/wrdthing.html *



Tue, 01 May 2001 03:00:00 GMT  
 Overflow - Am I going crazy?
Mark & David,

The following:

    bob = 36001 - (10 * 3600)

gives an overflow because VB (I am currently using VB4P)  assumes that since
all operands within the parentheses are integers (values within the integer
range), the result from the parenthese piece will be integer - but the actual
result (36,000) is a tad over the max for a positive integer (32,767).  I
'fixed' the problem by data-typing the operands like so:

    bob = 36001 - (10& * 3600&)

The obvious solution would be to use long vars as the operands instead of
hard-coding constants in a formula (or anywhere else, for that matter).  The
following code is tested and works (I put it in the Form1.Load event), however
I would discourage using variable names like what, this, and that.

    Dim bob as long, what as long, this as long, that as long

    what  =  36001: this  = 10: that  = 3600
    bob = what - (this * that)

    Form1.Label1.Caption = Str(bob)  'this shows a 1 in the label

Mike Yetz

Quote:

> I tried it on my machine, and it also overflows. Just doing bob = 10 * 3600
> was enough to overflow. It must be treating it as an integer variable. I
> also tried dim bob& , but with the same overflow. Can't help, but at least
> you're not alone!


> >Why does this give an overflow?

> >Dim bob as Long
> >bob = 36001 - (10 * 3600)

> >I don't understand! Is it VB being stupid or is it me?
> >Grrrrrrrrrr

> >Dave
> >' btcf'



Tue, 01 May 2001 03:00:00 GMT  
 
 [ 11 post ] 

 Relevant Pages 

1. ** HELP WITH vb 4.0 I am going crazy**

2. HELP!!! Cmd.CreateParameter I am getting crazy

3. 50 users running Access, am I crazy.

4. is this a bug or am i crazy?

5. Am I crazy, or is the Timer control?

6. Am I crazy or what?

7. Please, save a soul from going crazy ...

8. going crazy with Instr

9. I'm going crazy..

10. help me please ..i'm going crazy

11. Cast not valid - going crazy

12. Help Please with UploadFile---Going Crazy

 

 
Powered by phpBB® Forum Software