What you learn today
- Root and power
- Natural logarithm
- There is no log10
An ore find lies 30 m east and 40 m north of your base. How far is that? One tank has 0.5 kPa, the next 50,000 kPa – how do you display both sensibly? For questions like these the chip has four arithmetic instructions that go beyond mul and div.
Root and power
sqrt r a takes the square root, pow r a b computes a to the power of b. With Pythagoras you get the distance from two legs:
pow tmp dx 2 # dx * dx
pow d dz 2 # dz * dz
add d d tmp
sqrt d d # 30 and 40 give 50
pow follows the floating point standard IEEE-754. A few results you should know:
| Calculation | Result |
|---|---|
pow r0 2 10 | 1024 |
pow r0 x 0.5 | same as sqrt r0 x |
pow r0 10 -1 | 0.1 |
pow r0 -8 0.5 | NaN |
sqrt r0 -4 | NaN |
There is no square root of a negative number. The chip reports no error, it returns NaN – and every calculation that works with the value afterwards becomes NaN as well. One value subtracted the wrong way round is enough.
exp and log
exp r a computes e to the power of a (e ≈ 2.718). log r a goes the way back: it is the natural logarithm, to base e. You need both wherever something grows or shrinks by a fixed share per tick. Example: a value loses 10 % of its distance to the target every tick. After how many ticks is only 1 % left?
log n REST # REST = 0.01, log(0.01)
log tmp FACTOR # FACTOR = 0.9, log(0.9)
div n n tmp # about 43.7 ticks
The other way round, exp works out how much is left after n ticks. exp and log cancel each other out:
log tmp FACTOR # log(0.9)
mul tmp tmp n # n * log(0.9)
exp rest tmp # 0.9 to the power of n, like pow rest FACTOR n
There is no log10
The chip does not know a base-10 logarithm, nor ln – log already is the natural one. You get log10 through a division:
log10(x) = log(x) / log(10)
log(10) is a fixed number. As a define it costs no register:
define LN10 2.302585092994046 # log(10)
log e x
div e e LN10 # e = log10(x)
floor e e # power of ten
With that you split a big value into power of ten and mantissa, the way you write 50,000 as 5 · 10⁴. By the way, 5e4 is not a valid number in the code – you have to split it yourself.
Floating point does not always come out even: log(1000) / log(10) gives 2.9999999999999996 instead of 3. A floor afterwards makes it 2 – at exactly 1000 kPa the power of ten is wrong. Add a tiny correction before the floor, for example 0.000001.
log of 0 gives minus infinity, log of a negative number NaN. Check the value first, for example with sgtz ok x, and treat vacuum as a case of its own.
Exercise
Task
A pipe analyzer “Lager” measures pressures between almost 0 and 50,000 kPa. Two LED displays show the value as a power of ten:
- “Exponent” = floor(log10(p)), so 2 at 101.3 kPa
- “Mantisse” = p / 10^exponent, so 1.013
- Vacuum (p ≤ 0): both displays 0
- Analyzer gone: both −1
Measuring and output are already there. Compute log10 via LN10 and add the correction CORR before the floor.
Hints
Hint 1
Exponent: log e p, div e e LN10, add e e CORR, floor e e.
Hint 2
Mantissa: pow m BASE e gives 10^e, then div m p m.
Hint 3
In vacuum log becomes minus infinity and the mantissa NaN – the select lines with ok already catch that.
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