473,406 Members | 2,843 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Overflow with 'long' variable type?

Robbie
180 100+
Hi all, I'm relatively new to VB but I know that an overflow occurs when you try to make a variable be too high or low for what you've prepared it for, e.g. 300 when you've prepared it for use as a byte.
Here's what happening - I am trying to make a function which 'decodes' a color number (I mean, the kind of values object.BackColor can be, and which the RGB() function gives back) into its separate R, G and B values each from 0 to 255.
But here's the problem. To do that, I use this code:

Expand|Select|Wrap|Line Numbers
  1.     Dim ColumnValue As Long
  2.     Dim CurrentColumn As Integer
  3.  
  4.     For CurrentColumn = 255 To 0 Step -1
  5.  
  6.         ColumnValue = 255 * 255 * CurrentColumn 'This is where it overflows
  7.  
(This is the part of it which is causing an overflow)
CurrentColumn is sometimes needed to be higher than 255, that's why I didn't make it a 'byte' type.
When I test this (i.e. F5, not compiling to an EXE) VB says it overflows. Even when I type into the Immediate Window:
Expand|Select|Wrap|Line Numbers
  1. ?255*255*255
it still overflows.

According to this site, a 'long' data type in VB can be up to 2,147,483,647 (which is 10 digits long), and yet 255^3 is only 16,581,375 (8 digits long).

Why does VB overflow when it tries to do this? (Especially why does it also overflow in the Immediate Window?)
Mar 30 '07 #1
10 19645
Killer42
8,435 Expert 8TB
The problem is that VB is converting things internally to the smallest format that it thinks it can get away with. And it seems to make very poor choices in this regard. Most likely it is converting to Integer in this case.

Try this example in the immediate window...

? 255! * 255! * 255!

Then try this modified line...
Expand|Select|Wrap|Line Numbers
  1. ColumnValue = 255! * 255! * CurrentColumn 'This used to overflow
What we are doing is using the data type definition character "!" to ensure that all of the values used in the calculation are Long values. Then VB doesn't convert down.
Apr 1 '07 #2
Robbie
180 100+
Ah, I see. Thank you, that makes it work fine!
Out of interest though, you said that sort of 'forces' VB to keep it a Long data type. Can you force it into keeping it as other data types?
Just wondering if it might be useful to know in the future.
Apr 1 '07 #3
Killer42
8,435 Expert 8TB
Ah, I see. Thank you, that makes it work fine!
Out of interest though, you said that sort of 'forces' VB to keep it a Long data type. Can you force it into keeping it as other data types?
Just wondering if it might be useful to know in the future.
Sure.

It's not actually forcing VB to do anything in particular about the calculation, just specifying that the 255 (in this case) is a Long value. We could have put 255# to make it a double-precision decimal value. (This would be less efficient and make things slower, but unless you were doing the calculation millions of times the difference would be too small to notice.)

It's just that when setting up a calculation, VB takes into account all of the data types involved (or maybe just the "smallest" - I forget). So ensuring your data is the correct type can affect the way it works, as you've seen. You could have achieved the same effect by, say, declaring something like:
Public Const TwoFiftyFive As Long = 255
and then using that constant in your calculation.
Apr 1 '07 #4
Robbie
180 100+
Sure.

It's not actually forcing VB to do anything in particular about the calculation, just specifying that the 255 (in this case) is a Long value. We could have put 255# to make it a double-precision decimal value. (This would be less efficient and make things slower, but unless you were doing the calculation millions of times the difference would be too small to notice.)

It's just that when setting up a calculation, VB takes into account all of the data types involved (or maybe just the "smallest" - I forget). So ensuring your data is the correct type can affect the way it works, as you've seen. You could have achieved the same effect by, say, declaring something like:
Public Const TwoFiftyFive As Long = 255
and then using that constant in your calculation.
Okay, I see now. But actually, I am doing it about 200,000 times so the speed is quite important (because it needs to do it for every pixel in an image). Although it's managing it now at about quarter of a second for a thumbnail-sized image, which is OK for how I'm using it.

I also managed to make it work by doing:
Expand|Select|Wrap|Line Numbers
  1. ColumnValue = (255 ^ 1) * 255
  2.  
...which also seems kinda weird...

But thanks for the info. :)
Apr 2 '07 #5
Killer42
8,435 Expert 8TB
Okay, I see now. But actually, I am doing it about 200,000 times so the speed is quite important (because it needs to do it for every pixel in an image). Although it's managing it now at about quarter of a second for a thumbnail-sized image, which is OK for how I'm using it.

I also managed to make it work by doing:
Expand|Select|Wrap|Line Numbers
  1. ColumnValue = (255 ^ 1) * 255
  2.  
...which also seems kinda weird...

But thanks for the info. :)
No problem.

Performing a calculation 200,000 times is really not much for a modern processor. I doubt you'll see a big difference, though it may be enough to notice. Might be interesting to find out, though.

I always try to use Long wherever possible, for processing efficiency. Unless I'm short of memory or need a really huge array, in which case I might try to save space by using Integer or Byte if possible. On a 32 bit processor, Long is (supposedly) the fastest size to process.
Apr 2 '07 #6
vijaydiwakar
579 512MB
ur problem is solved
try this code
see this is the game of explicit type conversions
use explicit type conversions
as shown here
Expand|Select|Wrap|Line Numbers
  1.  
  2. Dim a&
  3. Private Sub Form_Load()
  4. a = Clng(255) * Clng(255) * Clng(255)
  5. End Sub
  6.  
  7.  
Apr 2 '07 #7
Killer42
8,435 Expert 8TB
ur problem is solved
try this code
see this is the game of explicit type conversions
use explicit type conversions
as shown here
Expand|Select|Wrap|Line Numbers
  1. Dim a&
  2. Private Sub Form_Load()
  3. a = Clng(255) * Clng(255) * Clng(255)
  4. End Sub
I have to disagree on this one.

You are executing the Clng() function at runtime. Three times! This surely has to be more work than telling the compiler to use the correct format to begin with. Or to put it more succinctly, I believe
a = 255! * 255! * 255!
will be processed much more efficiently than
a = Clng(255) * Clng(255) * Clng(255)
and just for once, shouldn't require any more memory (probably slightly less, in fact).

I do agree that the conversion functions such as Clng() are useful in many cases. But not for this simple example using literal values.
Apr 2 '07 #8
vijaydiwakar
579 512MB
I have to disagree on this one.

You are executing the Clng() function at runtime. Three times! This surely has to be more work than telling the compiler to use the correct format to begin with. Or to put it more succinctly, I believe
a = 255! * 255! * 255!
will be processed much more efficiently than
a = Clng(255) * Clng(255) * Clng(255)
and just for once, shouldn't require any more memory (probably slightly less, in fact).

I do agree that the conversion functions such as Clng() are useful in many cases. But not for this simple example using literal values.
this is explecit type conversions
it helps compilers to solve the problem more easily
in ur example u've used single notation and nothing else
Apr 2 '07 #9
Robbie
180 100+
Wouldn't a public const as Killer42 said near the start be fastest of all?
Because then you wouldn't need to be doing any converting data types at all because you made it a Long in the first place.

Also, is using a Long really quicker than using a Byte or Integer? Because I'm using bytes quite a lot, and any speed improvements would be welcome! ^^;
Apr 2 '07 #10
Killer42
8,435 Expert 8TB
Wouldn't a public const as Killer42 said near the start be fastest of all?
Because then you wouldn't need to be doing any converting data types at all because you made it a Long in the first place.

Also, is using a Long really quicker than using a Byte or Integer? Because I'm using bytes quite a lot, and any speed improvements would be welcome! ^^;
Yes, in my opinion a Const defined as Long would be your best bet.

As for the conversion, that's what the disagreement is about, between myself and vijaydiwakar. I believe there's a fundamental difference in our approaches. Using the "!" notation causes the compiler to use a Long value, so that no conversion is necessary at runtime. His method uses an Integer (or Byte, or Variant (:(which I hate) or whatever VB deems appropriate), then executes the Clng( ) function at runtime to convert it, thus requiring more processing and possibly making the code slightly larger.

Also, though this may no longer apply in VB6, a Const or variable used to be faster to access than a literal. In other words, in older versions of BASIC (and I think VB6, but not certain) using MyConstValue is faster than using 255. So in this case, defining a Const of Long format will definitely prevent your overflow problem, and possibly execute faster than using the literal value. It's a win-win situation. :)

As for the difference between data types, it wouldn't be a bad idea to test it for yourself. Just set up a loop that does some arithmetic operation (or whatever) a few million times, on one specific variable. Then try it a few times with each data type, and see what happens. I think you'll find that Long is the fastest. Where speed is not an issue, of course, a shorter type allows you to save space.

But I believe, as I said, using the native data type (which is the four-byte "long integer" type on a 32 bit processor) reduces the work that must be done "behind the scenes" and executes a little faster. On the prior 16 bit processors, this applies to the two-byte integer type. Presumably it will apply to the eight-byte "even longer integer:D" type on a 64 bit processor (and software).


P.S. I normally include in my project a code module called Numbers in which I define all these sort of constants as Public Const.
Apr 2 '07 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

34
by: Andy | last post by:
Hi, Are 1 through 4 defined behaviors in C? unsigned short i; unsigned long li; /* 32-bit wide */ 1. i = 65535 + 3; 2. i = 1 - 3; 3. li = (unsigned long)0xFFFFFFFF + 3; 4. li = 1...
25
by: Ashutosh Iddya | last post by:
Hi , I am performing an integer count of a particular operation in my program. After a sufficiently large value an overflow occurs. At the moment I have gone around the problem by declaring it...
7
by: Vivek Mohan | last post by:
Hi, I am using gcc, and I am a bit confused about the warning message I got when I compiled and ran this small code snippet - int main() { unsigned long x = 3*1024*1024*1024; /* 3GB */...
7
by: Netocrat | last post by:
The code at the bottom illustrates the nature of a situation discussed on comp.lang.asm.x86. Basically an unsigned integer is being used as a negative index in a loop, and it works as though the...
3
by: SheldonMopes | last post by:
I sometimes get a pop-up box that reads "Overflow" and the module that is executing pauses. It doesn't get caught by my error trapping, and it seems to be randow. By random, I mean usually in the...
8
by: starffly | last post by:
In my program, the caculated value is supposed to be no more than the constant named MAXINT,otherwise, overflow error will be informed.however, I cannot test if the value exceeds MAXINT within the...
2
by: Csaba Gabor | last post by:
I have a table that sizes to take up most of the page's width. The (mostly empty) rightmost cell of a particular row starts off as wide as it can, but it might be that it gets something that...
1
by: House817 | last post by:
I consistently get an overflow error at the "SOxBenefits =" (source code below), while coding in a macros from within Excel, Microsoft XP. I've tried the following: 1. setting a variable equal to...
2
srj115
by: srj115 | last post by:
I've been messing with an Access database recently. I'm trying to assign a 14-digit number to a long variable, and I keep getting an overflow message! Dim lng as Long lng = 20081008095613# I...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.