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

Day 285 of 365 · Expert · Month 10: Bits and data

Floating-point precision

Registers are decimals with a 53-bit mantissa: why 0.1 + 0.2 is not 0.3, where whole numbers lose their precision and why hashes are safe anyway.

Lesson 16 min Week 41

What you learn today

  • 53-bit mantissa
  • Rounding errors
  • Large hashes in a register

A counter adds 0.1 every tick. After ten ticks the chip asks beq tenths 1 done – and never jumps. The code is right, the number is not quite. Today is about what a register really stores.

53-bit mantissa

Every register is a double precision decimal: 53 bits for the digits, plus an exponent that moves the point. Two rules follow from that:

  1. Whole numbers up to 2^53 are exact. Above that, bits for the last digits are missing.
  2. Most decimal fractions are not exact. 0.1 is an endless fraction in binary, like 1/3 in decimal. The nearest number is stored.

Rounding errors

add r0 0.1 0.2 gives 0.30000000000000004. That is not a bug of the game, every computer working with decimals does this. seq r1 r0 0.3 therefore returns 0.

add sum 0.1 0.2              # 0.30000000000000004
seq exact sum 0.3            # 0, not equal
sap approx sum 0.3 TOLERANCE   # 1, close enough

You have known the solution since day 109: compare with a tolerance. sap checks whether two values are relatively close to each other. For controls a hysteresis with sgt and slt is better than an equality test anyway.

Numbers like 0.5, 0.25 or 0.125, on the other hand, are exact – they are binary fractions. A counter in half seconds therefore stays exact.

Large numbers

Above 2^53 = 9,007,199,254,740,992 units get lost:

add loss BIG 1           # 2^53 + 1 becomes 2^53
sub loss loss BIG     # 0 instead of 1

In the game you hit the limit mostly when packing: values with more than 53 bits together lose their lowest bits.

Large hashes in a register

Hashes look big, for example −1252983604 for the gas sensor. But they are 32-bit numbers and therefore fit into a register easily and exactly. seq on two hashes is therefore safe, even though care is needed with decimals otherwise.

Warning

Do not calculate with hashes. A hash times 1000 or two hashes in one packed value need more than 53 bits – then the lower digits are no longer right and a later comparison fails.

Diagram

The register bank of the chip r0 42 r1 31 r2 10 r3 -2.5 r4 0 r5 0 r6 0 r7 0 r8 0 r9 0 r10 0 r11 0 r12 0 r13 0 r14 0 r15 0 sp = r16 ra = r17 16 free registers, one number each sp: stack pointer · ra: return address
A decimal stores 53 bits of digits and an exponent. Whatever does not fit into the 53 bits is rounded.

Exercise

Task

See the limits for yourself:

  • sum = 0.1 + 0.2
  • exact = 1 if sum is exactly 0.3 (seq)
  • approx = 1 if sum is close to 0.3 within TOLERANCE (sap)
  • loss = (BIG + 1) − BIG
  • tenths = 0.1 added ten times in a loop
  • one = 1 if tenths is close to 1 within TOLERANCE

Hints

Hint 1

seq exact sum 0.3 and sap approx sum 0.3 TOLERANCE.

Hint 2

For the loss first add, then sub with BIG.

Hint 3

Loop with a counter up to 10, then sap one tenths 1 TOLERANCE.

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