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
| Instruction | does | 2.7 | −2.7 |
|---|---|---|---|
round r? a | to the nearest whole number | 3 | −3 |
trunc r? a | cut off the decimals | 2 | −2 |
floor r? a | round down, towards minus infinity | 2 | −3 |
ceil r? a | round up, towards plus infinity | 3 | −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.
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.
A display in Percent mode rounds to whole percent on its own. You do not need round there.
Diagram
Exercise
Task
d0 is a gas sensor, d1 and d2 are LED displays.
d1shows the temperature in °C, rounded to one decimal.d2shows 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