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

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

Rounding: round, trunc, floor, ceil

round, trunc, floor and ceil: four ways to get rid of decimals – and why they differ for negative numbers.

Lesson 10 min Week 4

What you learn today

  • Four ways to round
  • Mind negative numbers
  • round displays to digits

The sensor reports 294.2318 K. On the display you want to see “21.1”, not “21.0818”. And when you work out how many gas canisters you need, the answer must not be half a canister. There are four instructions for that.

The four rounding instructions

Instructiondoes2.7−2.7
round r? ato the nearest whole number3−3
trunc r? acut off the decimals2−2
floor r? around down, towards minus infinity2−3
ceil r? around up, towards plus infinity3−2

Negative numbers

For positive numbers trunc and floor are the same. For negative ones they are not: trunc only cuts and therefore moves towards zero, floor always moves down. For −2.7 that means: trunc gives −2, floor gives −3. On Mars at −40 °C that is not an edge case, it is everyday life.

Warning

For values exactly on .5, round does not always round the way you know from school. Do not rely on a particular direction for such values. With readings exactly .5 almost never happens anyway.

Rounding to digits

There is no instruction “round to one decimal”. You shift the decimal point, round and shift it back:

l r0 d0 Temperature
sub r0 r0 273.15     # e.g. 21.0818
mul r1 r0 10         # 210.818
round r1 r1          # 211
div r1 r1 10         # 21.1
s d1 Setting r1

For two digits use 100 instead of 10.

Whole pieces

When something only exists in whole pieces, the question is which direction is safe:

div r2 r3 50         # mol per canister: 50
ceil r2 r2           # rather one canister too many

ceil gives you enough canisters, floor tells you how many full canisters you can already fill.

Tip

A display in Percent mode rounds to whole percent on its own. You do not need round there.

Diagram

Data flow of an arithmetic instruction add r1 r0 273.15 r0 = 21 273.15 + r1 = 294.15 the target always comes first
Rounding instructions read one value and store the result in the target register. As always, the target comes first.

Exercise

Task

d0 is a gas sensor, d1 and d2 are LED displays.

  • d1 shows the temperature in °C, rounded to one decimal.
  • d2 shows the pressure in whole kPa, rounded down.

Hints

Hint 1

One decimal: times 10, round, divided by 10.

Hint 2

Rounding down means floor – for positive pressures trunc would do too.

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