What you learn today
- Smallest tolerance at zero
- brapz and brnaz
- Epsilon is not the tolerance
Yesterday you saw that a relative tolerance becomes tiny near zero. The z variants of the ap instructions compare against zero. Whoever expects them to check “approximately zero” with some leeway is in for a surprise.
What sapz really checks
In the game code sapz r? a c is nothing but sap r? a 0 c. So the condition reads: the magnitude of a is at most max(c × magnitude of a, float.epsilon × 8).
The first part, c times the magnitude of a, is smaller than the magnitude itself for any tolerance below 1. It never helps. What remains is the fixed floor float.epsilon × 8, about 1.1 · 10⁻⁴⁴. That is the smallest tolerance the ap instructions have, and at zero it is the only one that takes effect.
| Instruction | gives 1 or jumps if |
|---|---|
sapz r? a c | a is practically exactly 0 |
snaz r? a c | a is not practically exactly 0 |
bapz a c target / brapz a c n | like sapz, with a jump |
bnaz a c target / brnaz a c n | like snaz, with a jump |
So sapz checks for exactly zero, like seqz. A remainder from 0.1 + 0.2 − 0.3 does not count as zero:
add r0 0.1 0.2
sub r0 r0 0.3 # r0 is a tiny remainder, not 0
sapz r1 r0 0.5 # r1 = 0
Epsilon is not the tolerance
There is also the constant epsilon. It is about 5 · 10⁻³²⁴, the smallest number above zero the chip can represent. It has nothing to do with the float.epsilon × 8 limit of the ap instructions. Whoever writes sap r0 a b epsilon compares practically exactly.
Approximately zero with a limit of your own
For “almost zero” you need an absolute limit in the unit of the value:
sub diff inside outside
abs diff diff
slt equal diff 0.5 # less than 0.5 kPa difference
That also works in a vacuum, where the relative tolerance of sap fails. The z instructions remain for values that are exactly 0 by nature: counters, counts, 0/1 flags.
The z instructions treat NaN as neither zero nor non-zero as well: sapz and snaz return 0, bapz and bnaz do not jump. Catch NaN beforehand with snan or brnan if it matters.
You must not leave out the tolerance c with the z instructions, otherwise the number of operands is wrong. Write 0. A tolerance of 1 or more makes the result always true, and that helps nobody.
Diagram
Exercise
Task
In front of the airlock:
- Gas sensors “Kammer” and “Vorraum”: wall light “Ausgleich” on if the pressures are less than 0.5 kPa apart – in a vacuum too. If a sensor is missing: off.
- Two doors “Schleusentuer”: flashing light “Tueroffen” on as soon as the number of open doors is not zero. Use
snazfor that (the sum overOpenis a whole number and never NaN).
Hints
Hint 1
Equalisation: sub, abs, then slt equal diff LIMIT. NaN makes slt 0 by itself.
Hint 2
Open doors: lbn … Open Sum counts them, because every open door reports 1.
Hint 3
snaz warn open 0 is 1 as soon as at least one door is open.
Quiz
4 questions. You pass from 60 % – the server does the grading.
Complete the day
- Passed the simulator exercise
- Passed the quiz with at least 60 %
Without an account progress is saved in this browser only.
Report a mistake in this lesson
Please sign in to report. Sign in