stationeers.de Mod workshop · Dominik1199
DE Sign in
  1. Home
  2. IC10 academy
  3. Day 109

Day 109 of 365 · Advanced · Month 4: Network in detail

Approximately equal: sap, sna, bap, bna

Readings never hit exactly. sap, sna, bap, bna, brap and brna compare with a relative tolerance – and treat NaN as neither equal nor unequal.

Lesson 14 min Week 16

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.

Instructiondoes
sap r? a b cr = 1 if a ≈ b
sna r? a b cr = 1 if a clearly differs from b
bap a b c targetjumps if a ≈ b
bna a b c targetjumps if a clearly differs from b
brap a b c nrelative, if a ≈ b
brna a b c nrelative, 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 %
Warning

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.

Tip

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

Comparison and select as a switch temp = 313 sgt r1 temp 303 r1 = 1 select Color.Red Color.Green 4 if r1 ≠ 0 if r1 = 0
sap returns 1 if the distance is at most c times the larger magnitude. The tolerance grows with the values.

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 Lock 0 if both pressures are approximately equal, otherwise lamp off and Lock 1
  • 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

Esc
↑ ↓ selectEnter open/ opens search anywhere