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

Day 25 of 365 · Beginner · Month 1: First steps

min, max, abs and sgn

min and max set limits, abs measures the distance, sgn tells the direction. Four small instructions that save many jumps.

Lesson 10 min Week 4

What you learn today

  • Set limits with min and max
  • Distance with abs
  • Direction with sgn

A pump accepts Setting from 0 to 10. If your program computes 14, that is pointless at best, at −3 maybe worse. Instead of catching every case with a jump, you simply limit the value.

min and max

InstructionResult
min r? a bthe smaller of the two values
max r? a bthe larger of the two values

Sounds backwards but is logical: with min you set an upper limit, with max a lower limit.

min r0 r0 10         # at most 10
max r0 r0 0          # at least 0

After these two lines r0 is safely between 0 and 10. You find the smallest of three values in two steps: min r3 r0 r1, then min r3 r3 r2.

abs: the distance

abs drops the sign. −3 becomes 3, 3 stays 3. That way you ask for the distance to the target, whatever the direction:

sub r1 r0 294.15     # deviation from 21 C
abs r2 r1            # how far off, without direction
slt r3 r2 2          # 1 = less than 2 degrees off

sgn: the direction

sgn returns only the sign: −1 for negative, 1 for positive, 0 for exactly zero. On a display that becomes an arrow: −1 means “too cold”, 1 means “too warm”.

Note

sgn returns 0 for NaN, while abs, min and max pass NaN on. To sgn a failed sensor therefore looks like “exactly right”. You learn how to catch NaN in week 6.

Together: a limited output

sub r4 294.15 r0     # how much is missing to 21 C?
mul r4 r4 2          # 2 litres per degree
max r4 r4 0          # never negative
min r4 r4 10         # never above 10
s d3 Setting r4
Tip

Compute first, then limit, then write. In that order a device never receives a value outside its range.

Diagram

Data flow of an arithmetic instruction add r1 r0 273.15 r0 = 21 273.15 + r1 = 294.15 the target always comes first
Each of these instructions reads one or two values and stores the result in the register at the front.

Exercise

Task

d0 measures the room temperature, the target is 21 °C (294.15 K). d1 and d2 are LED displays, d3 a volume pump for warm gas.

  • d1 shows the distance to the target in degrees, without sign.
  • d2 shows the direction: −1 too cold, 1 too warm, 0 exactly right.
  • d3 Setting = (target − temperature) · 2, limited to 0 to 10.

No conditional jumps.

Hints

Hint 1

r1 already is the deviation actual − target. abs and sgn work on it directly.

Hint 2

target − actual is the same as −(actual − target): mul r4 r1 -2.

Hint 3

Limit with max r4 r4 0 and min r4 r4 10.

Quiz

5 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