What you learn today
- Why seq fails on decimals
- Give a relative tolerance
- brap and brna
A mixing tank should hold exactly 21 % oxygen. You check seq ok o2 0.21, and it never returns 1, although the display shows 21 %. The reason: readings almost never land exactly on a number.
Why seq fails on decimals
The chip calculates with floating-point numbers. Many decimal numbers cannot be represented exactly in them, and every calculation drags small remainders along:
add r0 0.1 0.2
seq r1 r0 0.3 # r1 = 0, r0 is just above 0.3
A sensor value, an average, a division: all of them land just beside the round number. seq, beq and breq compare exactly and therefore almost never hit.
Approximately equal with a tolerance
The ap instructions (approximately) compare with some leeway:
sap ok o2 0.21 0.05 # 1 if o2 deviates from 0.21 by at most 5 %
The tolerance is relative: a distance of c × max(|a|, |b|) is allowed. With c = 0.05 that is 5 % of the larger magnitude, for 0.21 a little over 0.01.
| Instruction | does |
|---|---|
sap r? a b c | r = 1 if a ≈ b |
sna r? a b c | r = 1 if a clearly differs from b |
bap a b c target | jumps if a ≈ b |
bna a b c target | jumps if a clearly differs from b |
brap a b c n | relative, if a ≈ b |
brna a b c n | relative, if a clearly differs from b |
Giving the tolerance relatively
Relative is handy because one tolerance fits all orders of magnitude: 2 % is 2 kPa at 100 kPa and 100 kPa at 5000 kPa. For pressures, temperatures in Kelvin and mixing ratios that is usually exactly right.
sap equal inside outside 0.02 # pressure equalised to within 2 %
With NaN the distance is not a number, so neither small nor large. In the game code both sap and sna then return 0, and neither bap nor bna jumps. Whoever locks a door with sna does not lock it on sensor failure. Build safety conditions with sap/bap/brap and only let the releasing action run when “approximately equal” is really true.
Near zero a relative tolerance becomes tiny. 2 % of 0.01 kPa is 0.0002 kPa. In a vacuum 0.01 and 0 kPa are therefore not “approximately equal”. Values near zero need an absolute limit, which is tomorrow's topic.
Diagram
Exercise
Task
The chamber door may only be unlocked if the pressure in the chamber roughly matches the vestibule.
- gas sensors “Kammer” and “Vorraum”, door “Kammertuer”, wall light “Ausgleich”
- tolerance 5 % relative (
TOL) - lamp on and
Lock0 if both pressures are approximately equal, otherwise lamp off andLock1 - if a sensor is missing: lamp off, door locked
Use sap and brap (no seq, beq, breq).
Hints
Hint 1
Replace seq with sap equal inside outside TOL.
Hint 2
Door: move lock 0, then brap inside outside TOL 2 and move lock 1.
Hint 3
With NaN brap does not jump – the door then stays locked.
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