none
Small basic cannot handle large numbers?(simple question) RRS feed

  • Question

  • Hi, here is a part of a code I wrote to determine prime numbers, but I found a problem that is small basic sees the number 22222222222222223 as an even number! Anybody knows the reason? Does that mean that small basic cannot handle large numbers? is that the reason?

    a=TextWindow.ReadNumber()
    
    prime= "True"
    c = Math.Remainder(a, 2)
    
    If c=0 Then
      prime="False"
    TextWindow.WriteLine("Didn't you know that "+a+" is an even number? better try a harder number!")
      EndIf

    Thursday, December 27, 2012 6:26 AM

Answers

  • 281474976710655 is the largest number Small Basic can handle. Sorry, your number exceeds that.

    Nonki Takahashi wrote a nice program that show this: FFW588


    Please mark any answers and "vote as helpful" any posts that help you!

    • Proposed as answer by litdevModerator Thursday, December 27, 2012 9:22 PM
    • Marked as answer by Behnam Azizi Friday, December 28, 2012 3:27 AM
    Thursday, December 27, 2012 3:52 PM
    Answerer

All replies

  • 281474976710655 is the largest number Small Basic can handle. Sorry, your number exceeds that.

    Nonki Takahashi wrote a nice program that show this: FFW588


    Please mark any answers and "vote as helpful" any posts that help you!

    • Proposed as answer by litdevModerator Thursday, December 27, 2012 9:22 PM
    • Marked as answer by Behnam Azizi Friday, December 28, 2012 3:27 AM
    Thursday, December 27, 2012 3:52 PM
    Answerer
  • 281474976710655 is the largest number Small Basic can handle.

    That is such an odd number. 2^48? Usually you see 2^32 or 2^64.

    Friday, December 28, 2012 12:33 PM
  • Small Basic uses System.Decimal for this.
    Friday, December 28, 2012 12:55 PM
  • The numbers in SmallBasic are of type Decimal, which are 96 bit or 12 byte number (one bit for a negative sign).  See here http://msdn.microsoft.com/en-us/library/xtba3z33.aspx.

    So the largest positive number should be 2^96-1 = 79228162514264337593543950335. 

    'Show maximum SmallBasic positive number
    maxValue = 0
    power = 1
    For i = 1 To 95
      power = power*2
      maxValue = maxValue + power
    EndFor
    TextWindow.WriteLine(maxValue)
    maxValue = maxValue+1
    TextWindow.WriteLine(maxValue)
    'Add 1 and we crash
    maxValue = maxValue+1
    TextWindow.WriteLine(maxValue)

    Some SmallBasic Math methods truncate to Double to do calculations and some significant figures may be lost.

    power = 1
    For i = 1 To 95
      power = power*2
    EndFor
    TextWindow.WriteLine(power)
    TextWindow.WriteLine(Math.Power(2,95))

    The Math.Remainder seems to fail at 9007199254740993 which is 2^53+1.

    power = 1
    For i = 1 To 53
      power = power*2
    EndFor
    power = power-2
    TextWindow.WriteLine(power+ " : "+Math.Remainder(power,2))
    power = power+1
    TextWindow.WriteLine(power+ " : "+Math.Remainder(power,2))
    power = power+1
    TextWindow.WriteLine(power+ " : "+Math.Remainder(power,2))
    power = power+1
    TextWindow.WriteLine(power+ " : "+Math.Remainder(power,2))

    I'm not quite sure why this is, looks like the significant figure accuracy of a Double or something like that.

    Friday, December 28, 2012 1:33 PM
    Moderator