# Problem with natural logarithmic function in fixed point notation

I am facing a problem with natural logarithmic function in fixed-point notation.

Let's say

$$x = 0.54, \qquad\text{then}\qquad \ln(x) = -0.616186, \qquad\text{a negative number} \tag{1}$$

Then in fixed-point notation, the value of $$x$$ is calculated by (assuming $$x$$ is $$\rm Q8.24$$ format)

temp_var = 0.54 * 16777216 = 9059696 (fixed point value in Q8.24 format)


So,

$$x = \ln(\textrm{temp_var}) = 16.01\qquad\text{since}\qquad 2^{24} = 16777216$$

And the float (true / unscaled form) value corrosponds to

$$16.01 / 16777216 = 9.54e-7\qquad\text{a positive value}\tag{2}$$

As we can see, both $$(1)$$ and $$(2)$$ are different in both magnitude and sign.

So, I just want to know how to solve these kinds of problems at a fixed point?

• The log rule is ln(a*b)= ln(a) + ln(b). And x = ln(0.54 * 16777216) =16.01 not ln(16.01) – Irreducible Jul 10 '20 at 6:27
• @Irreducible..I have corrected the mistake – rkc Jul 10 '20 at 9:17
• @rkc um, why are you dividing your logarithm value... that's not how logarithms work. – Marcus Müller Jul 10 '20 at 9:32
• @MarcusMüller Hi, I am dividing by 16777216, just to get the true and original value of x (unscaled form). The fixed-point representation of x is 16 (8.24 format). So, the true value corresponds to (float)16 / 16777216, right? – rkc Jul 10 '20 at 10:07
• please read the first comment again, the logarithmus rule is: log(a*b) = log(a) + log(b), that is why your division makes no sense, that is not how logarithmus works. – Irreducible Jul 13 '20 at 8:53