You have already seen some of the ways in which the ZX Spectrum Next can calculate with numbers. It can perform the four arithmetic operations +, -, * and / (remember that * is used for multiplication, and / is used for division), and it can find the value of a variable, given its name. The example:
tax=sum*20/100
gives just a hint of the very important fact that these calculations can be combined. Such a combination, like sum*20/100, is called an expression; so an expression is just a shorthand way of telling the computer to do several calculations, one after the other. In our example, the expression sum*20/100 means look up the value of the variable called "sum", multiply it by 20, and divide the result by 100.
There's also one more mathematical operation, the modulo which returns the remainder of a division. It is used in the same way as the division operator but is denoted instead by MOD. As an example the direct commands:
PRINT %17 MOD 6
PRINT 17 MOD 6
will both return 5 which is the remainder of the division of 17 by 6; note the percent symbol (%) that prefixes 17 in the first example: this is what defines it as an Integer Expression – We will look at this in a little bit.
It's important here to underline the order in which mathematical expressions are evaluated: For floating point operations, multiplications and divisions are done first. They have higher priority than addition and subtraction. Relative to each other, multiplication and division have the same priority, which means that the multiplications and divisions are done in order from left to right. When they are dealt with, the additions and subtractions come next; these again have the same priority as each other, so we do them in order from left to right. This is very important because one can arrive to a wrong result if they forget said order.
Although all you really need to know is whether one operation has a higher or lower priority than another, the computer does this by having a number between 1 and 16 to represent the priority of each operation: * and / have priority 8, and + and - have priority 6.
This order of calculation is absolutely rigid, but you can circumvent it by using parentheses; anything in parentheses is evaluated first and then treated as a single number.
Notes
Specifically for Integer Expressions, the order of calculations is strictly left-to-right with the exception of the use of parentheses. In the case of multiple sets of parentheses, their contents are also evaluated from left-to-right
Within every kind of expression there's a number of bitwise, relational and logical operations that can be performed other than simple mathematical operations. Specifically for numbers these can be performed on floating point or integer numbers. While the operators are the same, there are subtle (and some not so subtle) differences in the way things work and we'll attempt to show these below.
In addition to the above there's a unary operator which follows:
ZX Spectrum Next User Manual, 3rd Edition (ISBN 978-1-5272-5496-1), written and illustrated by Phoebus R. Dokos. Copyright © 2020-2024 Phoebus Dokos / SpecNext Ltd. Licensed under CC BY-NC-SA 4.0. This is a transcription and can contain errors; check any doubt against the printed page.