0

One thing that makes me particularly like about Smalltalk is that it has the power to do arithemtic calculations of numbers with the base of different integers. I guess no other language can do the same.

Please see the codes below.

Transcript show: 16raf * 32; cr.
Transcript show: 7r21 - 5r32; cr.

The output is

5600
-2

I understand that if the number is hexadecimal(16-based), abcdef can be employed. But what if the integer I want to be the base is 250. On some position, there's 60. How can I type that number in squeak ?

  • I am not sure whether you can use the custom base notation with such high bases at all, since the character pool is limited (and anything beyond the ascii letters would raise discussions about which character encoding determined the digit values). But note that the language is called Smalltalk, not SmallTalk. – JayK Oct 23 '17 at 07:11

1 Answers1

0

Short answer: you cannot type arbitrary numbers for arbitrary bases above 36 without changing the parser.

Longer answer:

You can use arbitrary base above 36, but you will run into trouble print and write numbers that would need symbols above 36.

You can check all the symbols for a base:

base := 36.
number := 0.
1 to: base - 1 do: [ :i |
    number := number * base + i
].
number printStringBase: base.

the above results in the following

'123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'

This is also hard-coded when printing in SmallInteger>>printOn:base:length:padded:

Note that for a number that is smaller than base, printStringBase: will just use ascii directly.

36 printStringBase: 37 '['

But even if you were to remove the hardcoded limitation and used ascii directly, you aren't helping yourself.

Sooner or later you will need ascii symbols that have different meaning in the syntax. For example following Z (ascii 90) is [ (ascii 91), which is used to start block.

So 37r2[ will be parsed into 37r2 and [ will be reserved for block (and thus will result in syntax error).

But otherwise you can use any base

2001rSpaceOdyssey -> 57685915098460127668088707185846682264
Peter Uhnak
  • 9,617
  • 5
  • 38
  • 51