The Zeno Book Zeno 0.1 zeno-lang.org

5 Numbers and Operators

Zeno has two kinds of plain number. An int is a whole number, of 64 bits, from  − 263 to 263 − 1: a little over nine billion billion either way. A float is a number with a fraction, the 64-bit floating point number that every processor with a floating point unit has. Chapters later in the book add numbers with a unit and whole numbers with a smaller size. This chapter is the plain two, and the operators that work on them.

5.1 Writing numbers

print(42, -7, 1_000_000)          # _ can split the digits, anywhere
print(0xFF, 0b1010_0101)          # hexadecimal and binary
print(3.14, -0.5, 2.5e-7, 1e10)   # floats
42 -7 1000000
255 165
3.14 -0.5 2.5e-7 10000000000.0

A float always prints with a decimal point or an exponent, so that it can never be taken for an int: 3.0 prints as 3.0, not 3.

5.2 Arithmetic

Operator Means Example
+ add 7 + 2 is 9
- subtract, or negate 7 - 2 is 5
* multiply 7 * 2 is 14
/ divide, always giving a float 7 / 2 is 3.5
// divide and round down 7 // 2 is 3
% & the remainder of // & 7 % 2 is 1 the remainder of // 7 % 2 is 1
** power 7 ** 2 is 49
print(7 + 2, 7 - 2, 7 * 2, 7 / 2, 7 // 2, 7 % 2, 7 ** 2)
print(10 / 5, 2 ** 0.5)
9 5 14 3.5 3 1 49
2.0 1.4142135623730951

Division

/ always gives a float, even when the numbers divide exactly. To divide whole numbers and keep a whole number, say so with //.

In C, 7 / 2 is 3 and 7.0 / 2 is 3.5: one operator does two different things depending on what it is given, and the remainder vanishes without a word. In Zeno the operator decides what happens, not the values. (Decision 2.1.)

// rounds down, toward minus infinity, and % takes the sign of the number on its right. Together they always satisfy (a // b) * b + a % b == a:

print(-7 // 2, -7 % 2, 7 % -2)
print(5.5 // 2, 5.5 % 2)
-4 1 -1
2.0 1.5

That is what makes i % n a safe position in a list of length n for any i, negative or not.

Dividing by zero is an error, with every division operator and with both kinds of number:

print(1 / 0)
error: uncaught ZeroDivisionError: division by zero
 --> main.zeno:1:7
  |
1 | print(1 / 0)
  |       ^

Powers

2 ** 10 is an int. A whole number to a negative power would be a fraction, so it is refused, and the message says what to write:

print(2 ** -1)
error: uncaught ValueError: a whole number cannot be raised to a negative power. Use a float, as in `2.0 ** -1`
 --> main.zeno:1:7
  |
1 | print(2 ** -1)
  |       ^

If 2 ** -1 were 0.5, the type of a ** b would depend on the value of b, which nothing can know before the program runs. The operator decides the type, not the values. (Decision 2.3a.)

Overflow

An int that would go past its largest or smallest value is an OverflowError, never a wrong number:

let big = 9223372036854775807
print(big + 1)
error: uncaught OverflowError: the result is too big for an int
 --> main.zeno:2:7
  |
2 | print(big + 1)
  |       ^

In C, an overflow either wraps round without a word or is undefined. A wrong number that looks plausible is the worst kind of mistake. When a program really does want a number to wrap round, as a checksum does, it says so with wrap, which Sized Numbers and Bits explains. (Decision 2.2.)

A float that grows too large becomes inf, as floats do everywhere:

print(1e308 * 10, -1e308 * 10)
inf -inf

5.3 Mixing ints and floats

An int and a float can meet in arithmetic, and the answer is a float:

print(1 + 0.5, 3 * 1.5, 1 == 1.0)
1.5 4.5 true

Nothing else changes kind by itself. Text is never turned into a number, or a number into text, without being asked:

print("3" + 4)
error: `+` does not work between str and int
 --> main.zeno:1:7
  |
1 | print("3" + 4)
  |       ^

1 error found. Nothing was run.

Making an int a float in arithmetic is what people expect from arithmetic. Every other automatic change is a guess at what the programmer meant, and JavaScript shows where those guesses lead: "3" + 4 is "34" there. (Decision 2.3.)

5.4 Changing kind on purpose

Written Gives
int(x) a whole number: from text, or from a float by dropping the fraction
float(x) a float: from text or from an int
str(x) the value as text, as print shows it
print(int("42"), int(" 7 "), int(9.99), int(-9.99))
print(float("1e3"), float(3), str(2.50) + "!")
42 7 9 -9
1000.0 3.0 2.5!

Text that is not a number is a ValueError, which a program can catch (Errors):

print(int("forty"))
error: uncaught ValueError: cannot turn "forty" into an int
 --> main.zeno:1:7
  |
1 | print(int("forty"))
  |       ^

5.5 Comparing

Operator Means
==, != equal, not equal
<, <= less than, less than or equal
>, >= greater than, greater than or equal

A comparison gives a bool. Comparisons chain, as they do in mathematics: 0 <= i < n means 0 <= i and i < n, with i worked out once.

let i = 3
print(0 <= i < 10, 1 < 2 < 3, 3 > 2 > 2)
true true false

In C, 0 <= i < n means (0 <= i) < n, which compares true or false with a number, compiles without a word, and is a classic mistake. Refusing chains altogether, as Rust does, is safe but loses the readable form. Zeno gives the line the meaning it looks like it has. (Decision 1.16.)

Comparing two things that can never be equal is a mistake, and Zeno says so instead of quietly answering false. It is nearly always a number that was read as text and never turned into one:

let typed = "1"
print(typed == 1)
error: `==` between str and int is always false
 --> main.zeno:2:7
  |
2 | print(typed == 1)
  |       ^

1 error found. Nothing was run.

Floats are kept in binary, so most decimal fractions are close to what you wrote but not exactly it. That is true in every language, and it is why floats are compared with care:

print(0.1 + 0.2, 0.1 + 0.2 == 0.3)
print(abs((0.1 + 0.2) - 0.3) < 1e-9)
0.30000000000000004 false
true

5.6 True and false

A bool is true or false, and three words work on them: and, or and not.

let warm = true
let raining = false
print(warm and not raining, warm or raining, not warm)
true true false

and and or stop as soon as the answer is known: in a and b, b is not worked out at all when a is false. That makes len(xs) > 0 and xs[0] == 1 safe for an empty list.

A condition must be a real bool. A number, a list or a piece of text is not true or false by itself:

let items = [1, 2]
if items {
    print("not empty")
}
error: a condition must be true or false, but this is list[int]
 --> main.zeno:2:4
  |
2 | if items {
  |    ^

1 error found. Nothing was run.

"Truthiness" means remembering which values count as false: is it 0, "", [], none, 0.0? The answer differs between languages and causes mistakes such as treating a real reading of 0 as no reading at all. Saying what you test, len(items) > 0, is clearer. (Decision 2.4.)

5.7 Changing a variable: the = forms

Every operator that takes two numbers has a form that changes a variable, x op= y, which means exactly x = x op y:

+= -= *= /= //= %= **= &= |= ^= <<= >>=

var n = 10
n -= 3        # 7
n *= 2        # 14
n //= 3       # 4
n **= 2       # 16
print(n)
var f = 1.0
f /= 4
print(f)
16
0.25

The variable is read first and the right-hand side worked out second, as in x = x + y. /= on an int is refused, because / gives a float and an int variable cannot hold one: use //=.

var half = 9
half /= 2
error: the new value should be int, but this is float
 --> main.zeno:2:9
  |
2 | half /= 2
  |         ^

1 error found. Nothing was run.

5.8 Which operator goes first

When operators are mixed, some bind tighter than others, as in school arithmetic: 2 + 3 * 4 is 14. From the loosest to the tightest:

Operators
or loosest
and
not
== != < <= > >= in is comparisons, which chain
| bit or
^ bit exclusive or
& bit and
<< >> shifts
+ -
* / // % &
-x ~x negation, bit not
** tightest
print(2 + 3 * 4 ** 2, (2 + 3) * 4, -2 ** 2)
50 20 -4

Brackets always win, and they are the best way to make a long expression plain to the reader. The bit operators are in Sized Numbers and Bits, and the whole table again, with every operator, is Operators.

5.9 Built-in functions for numbers

print(abs(-4.5), min(3, 1, 2), max(2, 8.5), sum([1, 2, 3]))
print(min([5, 3, 9]), max([5, 3, 9]))
4.5 1 8.5 6
3 9

min and max take either several numbers or one list. Square roots, rounding, logarithms and the trigonometric functions are in the math module (The math Module).

int is a 64-bit whole number, float a 64-bit floating point number. / always gives a float and // rounds down. An int that overflows is an error, never a wrong number. Ints and floats mix in arithmetic and nowhere else. Comparisons chain, conditions must be real bools, and every operator on two numbers has an = form.

  1. Work out -17 // 5 and -17 % 5 on paper, then check with Zeno. Check that (a // b) * b + a % b == a holds.

  2. What is the largest power of two that fits in an int? Find it by trying 2 ** n for larger and larger n.

  3. Write if 1 <= x <= 10 and x % 2 == 0 for a few values of x. Put brackets where Zeno would put them.

  4. Print 1 / 3 * 3. Is it exactly 1? Now try 0.1 * 3.