Arithmetic & Comparison

Beamtalk uses standard math precedence (not Smalltalk's left-to-right). This chapter covers all the numeric operators, integer vs float arithmetic, and the equality/comparison operators — including some Beamtalk-specific ones.

Integer arithmetic

3 + 4   // => 7
10 - 6  // => 4
3 * 7   // => 21

Integer division always yields a Float:

10 / 4  // => 2.5
10 / 2  // => 5.0

Truncating integer division:

10 div: 3   // => 3
-7 div: 2   // => -3

Modulo with % (remainder, same sign as dividend):

10 % 3   // => 1
-10 % 3  // => -1

Exponentiation (right-associative):

2 ** 10        // => 1024
// Right-associative: 2 ** 3 ** 2 is the same as 2 ** (3 ** 2)
2 ** 3 ** 2    // => 512

Float arithmetic

3.14 + 2.0   // => 5.140000000000001
1.5 * 4.0    // => 6.0
7.5 / 2.5    // => 3.0

Mixed integer/float:

3 + 1.5   // => 4.5
2 * 3.0   // => 6.0

Float-specific operations:

3.7 floor      // => 3
3.2 ceiling    // => 4
3.7 truncated  // => 3
3.5 rounded    // => 4
3.14 isNaN     // => false

Operator precedence (standard math)

Highest to lowest within binary messages:

**         exponentiation
* / %      multiplicative
+ - ++     additive
< > <= >=  comparison
= == =:=   equality (lowest binary)
2 + 3 * 4    // => 14
10 - 2 * 3   // => 4
2 ** 3 + 1   // => 9

Parentheses override:

(2 + 3) * 4  // => 20

Comparison operators

3 < 5   // => true
5 > 3   // => true
3 <= 3  // => true
4 >= 5  // => false

Equality

Beamtalk has several equality operators:

5 =:= 5      // => true
5 =:= 5.0    // => false
5 == 5.0     // => true
5 =/= 6      // => true
5 /= 5.0     // => false

Strings and symbols use =:=:

"hello" =:= "hello"  // => true
#foo =:= #foo        // => true
#foo =:= #bar        // => false

Object identity (same object in memory):

x := "hello"  // => _
x == x        // => true

Numeric utility messages

-5 abs    // => 5
5 negated  // => -5
3 max: 7   // => 7
3 min: 7   // => 3

Between (inclusive):

5 between: 1 and: 10   // => true
15 between: 1 and: 10  // => false

GCD and LCM:

12 gcd: 8  // => 4
4 lcm: 6   // => 12

Square root (returns Float):

9 sqrt  // => 3.0
2 sqrt  // => 1.4142135623730951

Converting between numeric types

42 asFloat      // => 42.0
3.9 asInteger   // => 3
3.9 floor       // => 3
3.9 ceiling     // => 4
3.9 rounded     // => 4
3.1 rounded     // => 3

Summary

Key difference from classic Smalltalk: standard math precedence, not left-to-right.

Exercises

1. Hypotenuse. Compute the hypotenuse of a right triangle with sides 3 and 4 using sqrt. What value do you get?

Hint

((3 * 3) + (4 * 4)) sqrt5.0. The Pythagorean theorem: sqrt(a² + b²).

2. GCD and LCM. Find the GCD and LCM of 24 and 36. Then verify that (a gcd: b) * (a lcm: b) =:= a * b (a fundamental identity).

Hint

24 gcd: 3612, 24 lcm: 3672. And 12 * 72 =:= 24 * 36true (both equal 864).

3. Clamping a value. Using max: and min:, write an expression that clamps a value to the range 0–100. Test with values -5, 50, and 150.

Hint
(-5 max: 0) min: 100    // => 0
(50 max: 0) min: 100    // => 50
(150 max: 0) min: 100   // => 100

max: 0 ensures the floor, min: 100 ensures the ceiling.

Next: Chapter 6 — Strings