Discussion Locked

Visual Basic

0 helpful
6 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

I want to divide one real number variable by another and assign the result to another variable. I always get an overflow error. I would like the answer to be rounded to the nearest thousanths. How can I declare these variables to make it work?

Ex. vari1 = vari2 / vari3

6 replies

by glen_mcleod

Declare all variable as Double. This should fix your overflow error. Once you've gotten the results of your division, you can use the Round() function. It takes your number as the first parameter, and the number of digits after the decimal as thesecond parameter.

Glen

by stillatit

(I don't know how much you know, so forgive me if I over-explain.)

The division operation returns a value of type double, so start by declaring your variables as type double.
Dim vari2 as Double
A typical real number variable might be of type single, which gives you a smaller range of legal values.

Here are the definitions of single and double from the help file. Look at the minimum and maximum values. The number following the 'E' is the exponent of ten by which you multiply the rest of the value -- the same way you would write the value using scientific notation. (2.7E04 = 27000 2.7E-04 = 0.00027)

SINGLE 4 bytes A single-precision floating-point value with a range of ? 3.402823E38 to ? 1.401298E-45 for negative values, 1.401298E-45 to 3.402823E38 for positive values, and 0.
DOUBLE 8 bytes A double-precision floating-point value with a range of ? 1.79769313486232E308 to ? 4.94065645841247E-324 for negative values, 4.94065645841247E-324 to 1.79769313486232E308 for positive values, and 0.

(continued in comment below)

by stillatit

(continued)

If the divisor (vari3 in your example) is very close to zero, so that the magnitude of your answer exceeds the maximum value allowed for the variable type, you are going to overflow. There are various ways to trap the error, as described in the on-line manual under "Designing an Error Handler" and other sections. If your divisor is zero, you will always raise the divide by zero error. If you pretty much know what the values will be, it is possible to change the magnitude in a previous statement to give you a legal value, then adjust your answer later.
Example: if I know that x/y will give me an answer which is one million (10E6) times the legal value, I could do something like
z = y * 1.0E06
ans = x / z
The variable ans will then have a legal value which is really one million times the answer you are looking for. This is useful only if you are then going to use ans in more calculations which will have answers in the legal range for the variables you are using.You can round your answer to thousandths by multiplying by 1000, adding 0.5, applying the int function, then dividing by 1000.
myroundedvalue = int(myvalue * 1000 +0.5) / 1000

This discussion is closed to new replies.