This article explains about overflow especially for Small Basic programmer.


Overflow

In a computer, a number will be contained in a limited size of memory.  The size depends on it's data type.  Microsoft Small Basic is one of the .NET framework programming language.  Internally, Small Basic uses some of those data (variable) type such as Decimal, Double, Int32 and so on.  So, if a number is larger than the memory size, overflow occurs when assigning.

Number Description
"-79228162514264337593543950336"  Treated as text (not number) - becomes zero for math calculation
"-79228162514264337593543950335"  Can be assigned as literal text, Decimal exception with operations
 -79228162514264333200000000000 Decimal exception at assigning as literal number
 -79228162514264333199999999999 Can be assigned as literal number, value rounded as -79228162514264300000000000000
"-79228162514264333195497438208" Can be assigned as literal text, Decimal exception with operations
"-79228162514264333195497438207" Can be assigned as literal text
 -79228162514264300000000000001 Can be assigned as literal number, value rounded as -79228162514264300000000000000
 -79228162514264300000000000000 Can be assigned as literal number
 -0.0000000000000000000000000001 Can be assigned as literal number
 -0.000000000000000000000000000099999999999999999 Can be assigned as literal number, value rounded as -0.0000000000000000000000000001
 -0.000000000000000000000000000050000000000000007 Can be assigned as literal number, value rounded as -0.0000000000000000000000000001
 -0.000000000000000000000000000050000000000000006 Value rounded as 0
 0.000000000000000000000000000050000000000000006 Value rounded as 0
 0.000000000000000000000000000050000000000000007 Can be assigned as literal number, value rounded as 0.0000000000000000000000000001
 0.000000000000000000000000000099999999999999999 Can be assigned as literal number, value rounded as 0.0000000000000000000000000001
 0.0000000000000000000000000001 Can be assigned as literal number
 2147483648 Available with Math.GetRandomNumber()
 2147483649 Int32 exception with Math.GetRandomNumber() 
 79228162514264300000000000000 Can be assigned as literal number
 79228162514264300000000000001 Can be assigned as literal number, value rounded as 79228162514264300000000000000
"79228162514264333195497438207" Can be assigned as literal text
"79228162514264333195497438208" Can be assigned as literal text, Decimal exception with operations
 79228162514264333199999999999 Can be assigned as literal number, value rounded as 79228162514264300000000000000
 79228162514264333200000000000 Decimal exception at assigning as literal number
"79228162514264337593543950335" Can be assigned as literal text, Decimal exception with operations
"79228162514264337593543950336"  Treated as text (not number) - becomes zero for math calculation

Decimal Overflow

Decimal is a real number data type.  Small Basic uses Decimal for variables and parameters of many operations.
Decimal allows from -79228162514264337593543950335 (-2^96+1) to 79228162514264337593543950335 (2^96-1). And the nearest numbers to zero are -0.0000000000000000000000000001 (-1E-28) and 0.0000000000000000000000000001 (1E-28).

Literal Assignment

Following code assigns the numbers as texts.  These numbers are smaller or larger than Decimal significand.

number = "-79228162514264337593543950336"
TextWindow.WriteLine(number - 1)  ' -1
TextWindow.WriteLine(number + 0)  ' -792281625142643375935439503360
TextWindow.WriteLine(number + 1)  ' -792281625142643375935439503361
number = "79228162514264337593543950336"
TextWindow.WriteLine(number - 1)  ' -1
TextWindow.WriteLine(number + 0)  ' 792281625142643375935439503360
TextWindow.WriteLine(number + 1)  ' 792281625142643375935439503361

Following code occurs exception errors and the program will end.

number = -79228162514264333200000000000 ' Error
number = 79228162514264333200000000000  ' Error

Following code assigns the numbers to the variables properly.

number = "-79228162514264333195497438207"
TextWindow.WriteLine(number)  ' -79228162514264333195497438207
number = -79228162514264300000000000000
TextWindow.WriteLine(number)  ' -79228162514264300000000000000
number = 79228162514264300000000000000
TextWindow.WriteLine(number)  ' 79228162514264300000000000000
number = "79228162514264333195497438207"
TextWindow.WriteLine(number)  ' 79228162514264333195497438207

Operation Call

Following code occurs exception errors and the program will end.

number = "79228162514264333195497438208"
TextWindow.WriteLine(Math.Abs(number))  ' Error
TextWindow.WriteLine(Math.Floor(number))  ' Error
TextWindow.WriteLine(Math.Ceiling(number))  ' Error
TextWindow.WriteLine(Math.Round(number))  ' Error
TextWindow.WriteLine(Math.Max(number, number))  ' Error
TextWindow.WriteLine(Math.Min(number, number))  ' Error

Double Overflow

Double is a 64-bit real data type.  Double allows from -1.79769313486232e308 to 1.79769313486232e308.
Following code assigns the numbers to the variables, but some overflows occur and the numbers will be rounded.  Decimal significand has 29 digits.  But Double significand has 15 digits.  This rounding must be caused by Double overflow.

number = -79228162514264333199999999999
TextWindow.WriteLine(number)  ' -79228162514264300000000000000
number = 79228162514264333199999999999
TextWindow.WriteLine(number)  ' 79228162514264300000000000000

Int32 Overflow

Int32 is a 32-bit integer data type.  Math.GetRandomNumber() operation uses Int32 internally.
Int32 allows from -2147483648 (-2^31) to 2147483647 (2^31-1) .  If the parameter of Math.GetRandomNumber() is out of the latter range, following error occurs.

TextWindow.WriteLine(Math.GetRandomNumber(2147483648))  ' Error

See Also