Magnitudes & Numbers

A subclass of Magnitude only needs to implement < and = to get the rest.

19.1 The Magnitude hierarchy

A subclass of Magnitude only needs to implement < and = to get the rest.

Magnitude                 (<, <=, >, >=, between:and:, max:, min:)
 ├── Character
 ├── Date, Time, DateAndTime, Duration (Pharo: Timespan, Month, Week, Year …)
 └── Number               (arithmetic, conversion, testing)
      ├── Integer
      │    ├── SmallInteger         (immediate, 61-bit range on 64-bit VMs)
      │    └── LargePositiveInteger / LargeNegativeInteger   (arbitrary precision)
      ├── Fraction                  (exact rational)
      ├── Float                     (BoxedFloat64, SmallFloat64 on 64-bit)
      └── ScaledDecimal             (exact decimal with a print scale)

19.2 Automatic coercion

Integers grow and shrink between SmallInteger and LargeInteger automatically; operations mix types through generality coercion:

SmallInteger maxVal.                    "→ 1152921504606846975 on 64-bit"
SmallInteger maxVal + 1.                "→ a LargePositiveInteger, no overflow"
100 factorial printString size.         "→ 158 digits"
(2 raisedTo: 100) class.                "→ LargePositiveInteger"
(1/3) class.                            "→ Fraction"
(2/4).                                  "→ (1/2) — normalized"
(1/3) + (1/6).                          "→ (1/2)"
(1/3) + 0.5.                            "→ 0.8333333333333333 (Float wins)"
3 + (1/2).                              "→ (7/2)"
0.1 + 0.2.                              "→ 0.30000000000000004"
0.1s2 + 0.2s2.                          "→ 0.30s2 — exact"
(1/3) asFloat.                          "→ 0.3333333333333333"
0.75 asExactFraction.                   "→ (3/4)"

19.3 Arithmetic

7 + 2.   7 - 2.   7 * 2.   7 / 2.       "→ 9 5 14 (7/2)"
7 // 2.                                  "→ 3  (integer division, rounds toward negative infinity)"
7 \\ 2.                                  "→ 1  (modulo, sign follows divisor)"
-7 // 2.                                 "→ -4"
-7 \\ 2.                                 "→ 1"
-7 quo: 2.                               "→ -3 (truncates toward zero)"
-7 rem: 2.                               "→ -1"
2 ** 10.                                 "→ 1024 (Pharo); also 2 raisedTo: 10"
2 raisedTo: 0.5.                         "→ 1.4142135623730951"
16 sqrt.     27 log: 3.     100 log.    1 exp.    2 ln.
-5 abs.      5 negated.     3 squared.  17 isPrime.   12 gcd: 18.   4 lcm: 6.
3.7 floor.   3.2 ceiling.   3.5 rounded.   3.7 truncated.   3.14159 roundTo: 0.01.
10 sign.     0 isZero.      7 even.     7 odd.     7 reciprocal.     "→ (1/7)"
5 between: 1 and: 10.       3 max: 9.   Float pi.  Float infinity.  Float nan isNaN.

19.4 Conversions and printing

42 printString.                 "→ '42'"
255 printStringBase: 16.        "→ 'FF'"
255 printString: 2.             "→ '11111111'"
'42' asNumber.     '3.5' asNumber.     '42' asInteger.
42 asFloat.        3.99 asInteger.     "→ 3"
65 asCharacter.    $A asInteger.       "→ $A 65"
42 printPaddedWith: $0 to: 6.          "→ '000042'"
3.14159 printShowingDecimalPlaces: 2.  "→ '3.14'"
1/3 printString.                        "careful: parses as 1 / (3 printString) → error"
(1/3) printString.                      "→ '(1/3)'"

19.5 Bits and random numbers

12 bitAnd: 10.    12 bitOr: 10.    12 bitXor: 10.    1 bitShift: 10.    "→ 8 14 6 1024"
10 atRandom.      #(#red #green #blue) atRandom.     Random new next.   (Random seed: 42) nextInteger: 100.

19.6 Dates and times

Points and rectangles: (3 @ 4) + (1 @ 1) → (4@5), (3 @ 4) dist: 0 @ 0 → 5.0, (0 @ 0 corner: 100 @ 50) area → 5000.

Date today.                    Time now.                 DateAndTime now.
Date today addDays: 30.        (Date year: 2026 month: 12 day: 25) dayOfWeekName.
3 days.  2 hours.  90 seconds.  500 milliSeconds.        "Duration literals via messages"
DateAndTime now + 2 hours.
(Date today - (Date year: 2000 month: 1 day: 1)) days.
'2026-10-25' asDate.           DateAndTime fromString: '2026-10-25T10:00:00Z'.
[ 30 factorial ] timeToRun.    "→ a Duration"