typestar

Numbers in Smalltalk

Fractions stay exact; integers grow without limit.

"Numbers answer messages; fractions stay exact."
Transcript show: (17 / 4) printString; cr.
Transcript show: (17 / 4) asFloat printString; cr.
Transcript show: (17 // 4) printString, ' rem ',
    (17 \\ 4) printString; cr.

"Fractions are first-class: a third plus a sixth is a half."
Transcript show: (1/3 + (1/6)) printString; cr.

Transcript show: 2 sqrt printString; cr.
Transcript show: (120 gcd: 340) printString; cr.
Transcript show: 10 factorial printString; cr.
Transcript show: (255 printStringBase: 16); cr.

How it works

  1. 17 / 4 answers the fraction 17/4, not 4.25.
  2. // and \\ divide and take the remainder.
  3. 10 factorial and printStringBase: 16 just work.

Keywords and builtins used here

The run, in numbers

Lines
13
Characters to type
514
Tokens
99
Three-star pace
60 tpm

At the three-star pace of 60 tokens a minute, this run takes about 99 seconds.

Type this snippet

Step 3 of 3 in Reflection, step 28 of 31 in Language basics.

← Previous Next →

Numbers in other languages