Quantcast
Channel: Small Basic
Viewing all articles
Browse latest Browse all 669

Small Basic - Limits of Literals

$
0
0

I found three types of limits about numeric literals in Small Basic.  Following explanation is about positive numbers, but negative numbers have the similar limits.

Rounded

Numeric literal larger than 1015 will be rounded at substitution.  In following code, the second one is rounded.  But the third one (text) is not rounded.

n = 1000000000000000
TextWindow.WriteLine(n)
n = 1000000000000001
TextWindow.WriteLine(n)
n = "1000000000000001"
TextWindow.WriteLine(n)
1000000000000000
1000000000000000
1000000000000001

Substitution Error

In following code, the first substitution succeeds.  But the second one fails with a run time error "Value was either too large or too small for a Decimal."

n = 79228162514264333199999999999
TextWindow.WriteLine(n)
n = 79228162514264333200000000000
TextWindow.WriteLine(n)
79228162514264300000000000000

Unhandled Exception: System.OverflowException: Value was either too large or too
 small for a Decimal.
   at System.Decimal..ctor(Double value)
   at Microsoft.SmallBasic.Library.Primitive.op_Implicit(Double value)
   at _SmallBasicProgram._Main()

Treated as Text

In following code, the first text is not treated as a number.  The second one is treated as a number, so causes run time error when increased.

n = "79228162514264337593543950336"
TextWindow.WriteLine(n)
TextWindow.WriteLine(n - 1)
TextWindow.WriteLine(n + 1)
n = "79228162514264337593543950335"
TextWindow.WriteLine(n)
TextWindow.WriteLine(n - 1)
TextWindow.WriteLine(n + 1)
79228162514264337593543950336
-1
792281625142643375935439503361
79228162514264337593543950335
79228162514264337593543950334

Unhandled Exception: System.OverflowException: Value was either too large or too
 small for a Decimal.
   at System.Decimal.FCallAddSub(Decimal& d1, Decimal& d2, Byte bSign)
   at Microsoft.SmallBasic.Library.Primitive.Add(Primitive addend)
   at _SmallBasicProgram._Main()

See Also


Viewing all articles
Browse latest Browse all 669

Trending Articles