Dealing with very large numbers

Question ID: 104442
8
0

While not normally needed, how does one go about utilizing math functions with very large numbers, without getting an "overflow" error? Numbers treated as integers go from -2,147,483,648 to 2,147,483,647; yet, how could one utilize, say, 1e+15 or 3e+50? I’ve tried using CDbl and CLng, neither of which seem to work, at least not with carrying out functions which allow their use.

For example, below wanting to convert a trillion to a base 2 number. An "overflow" results on the astrieck line regardless of using CDbl or CLng anywhere.

Base = 2
Convert = 1000000000000
Function BaseNumber(Base,Convert)
Dim i, j, x, y
Dim aPlace()
i = 0
j = 0
x = Convert
While x>=0
i = i+1
x = Convert-Base^i
Wend
ReDim aPlace(i)

’ Converts to new base
aPlace(j) = Convert
While j=0
BaseNumber = BaseNumber & aPlace(y)
y = y-1
Wend
End Function

Marked as spam
Posted by (Questions: 1, Answers: 0)
Asked on January 10, 2012 10:03 am
5 views
Answers (1)
3
Private answer

You need to declare your integer as a long as opposed to an integer. Use this syntax:

** Dim BaseNumber As Double **

The following are the sizes of data types for visual basic:

*Long (long integer) 4 bytes -2,147,483,648 to 2,147,483,647

*Single (single-precision floating-point) 4 bytes -3.402823E38 to -1.401298E-45 for negative values; 1.401298E-45 to 3.402823E38 for positive values. About 6 or 7 significant figures accuracy.

*Double (double-precision floating-point) 8 bytes -1.79769313486231E308 to -4.94065645841247E-324 for negative values; 4.94065645841247E-324 to 1.79769313486232E308 for positive values. About 15 or 16 significant figures accuracy.

*Currency (scaled integer) 8 bytes -922,337,203,685,477.5808 to 922,337,203,685,477.5807

*Decimal 14 bytes +/-79,228,162,514,264,337,593,543,950,335 with no decimal point; +/-7.9228162514264337593543950335 with 28 places to the right of the decimal; smallest non-zero number is +/-0.0000000000000000000000000001

Marked as spam
Posted by (Questions: 4, Answers: 41)
Answered on February 28, 2013 3:38 pm