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
17 / 4answers the fraction 17/4, not 4.25.//and\\divide and take the remainder.10 factorialandprintStringBase: 16just work.
Keywords and builtins used here
TranscriptasFloatcrfactorialprintStringsqrt
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.
Step 3 of 3 in Reflection, step 28 of 31 in Language basics.