overflow with double precision

MetLife

Active Member
Joined
Jul 2, 2012
Messages
319
Office Version
  1. 365
Hi,

I am getting the error "overflow", when I assign a variable of type double a number >32,000. What is going on?

dim guess as double
guess = 73000 'no overflow error
guess = 73 * 1000 'gets an overflow error

I remember this error happening in the past.
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
I don't recall the whole story offhand, but it has to do with intermediate results. 73 and 1000 are implicitly defined as integer, so there's an intermediate result which is eventually assigned to "guess". The intermediate result is what overflowed. You can get around it by defining the 2 multipliers as double, either with the Dim, or the # symbol:

Code:
Sub TEST1()
Dim guess As Double
Dim a As Double, b As Double

    a = 73
    b = 1000
    
    guess = a * b     ' works
    
    guess = 73# * 1000#    ' works
    
    guess = 73 * 1000     ' fails
    
End Sub
 
Upvote 0
@EricW.... You've almost got it (the explanation). The problem is: 73 and 1000 are small enough for VBA to consider type Integer. Consequently, the arithmetic is done as type Integer. Thus, the product cannot exceed 32767.

It is sufficient to put the "#" suffix (implicit type Double) on one constant. 73# * 1000 will coerce 1000 to type Double, and Double arithmetic will be performed.

PS.... If you cannot remember these "magic" suffixes, simply type 73.0 or 1000.0. The decimal point is sufficient for VBA to recognize the constant as type Double, even though the decimal fraction is zero.

In fact, in this case, it is sufficient to use the "&" suffix to cause type Long arithmetic to be done.

But it really does not matter much in modern computers.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,162
Messages
6,170,432
Members
452,326
Latest member
johnshaji

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top