How to convert $\ln x - \ln y$ into a more accurate floating point representation?

607 Views Asked by At

I have an equation $\ln(x) - \ln(y)$ where x and y are very close to eachother. For example if $x = 5.1234$ then something like $fl(\ln(5.1234)) = 1.6338$ (with 4 significant digits). If $y = 5.1233$ then this creates a loss of significant digits which makes the floating point approximation very inaccurate with the actual value.

I need to re-write this to make it more accurate so I re-wrote it like this: $\ln x - \ln y = \ln(\frac{x}{y})$. Unfortunately, if I do something like $\ln(\frac{5.1234}{5.1233})$ then $(\frac{5.1234}{5.1233})$ = 1.000019519 which is 1.0000 in floating point so it's still very inaccurate. Is there some other way to re-write this to make the floating point value more accurate?

2

There are 2 best solutions below

0
On BEST ANSWER

Use the function $\mathrm{log1p}$ if available. Otherwise the first term of the Taylor expansion of $\ln(1+z)= z + O(z^2)$ gives

$$\ln\frac{5.1234}{5.1233} = \ln\frac{5.1234+5.1233-5.1233}{5.1233}= \ln\left(1+\frac{0.0001}{5.1233}\right) \approx \frac{0.0001}{5.1233} $$ $$\approx 0.1951866961 \cdot 10^{-4} \approx 0.19519\cdot 10^{-4}$$

3
On

EDIT: This answer assumes your inputs are approximation (i.e., have a finite number of significant figures, with $5.1234$ approximating some unknowable "true" quantity $5.1234\pm 10^{-4}$. If for some reason $5.1234$ is an exact number (has infinite significant figures) then this answer is moot and you should use log1p.

You're right to use the identity $$\log x - \log y = \log(x/y)$$ as this avoids catastrophic cancellation of the results of the $\log$ functions. If you get numerical zero, that's still more accurate than whatever noise comes out of catastrophic cancellation.

Other than that, it's not clear what you're hoping for. Math isn't magic; you can't recover information that's been lost. $$\log\left(5.1234+\epsilon\cdot 10^{-4}\right) - \log 5.1233 \approx 0.0000195185 + 0.0000195183 \epsilon$$ for representation error $|\epsilon|<1$, so you cannot hope for any kind of accurate result.

As gammatester says in his answer, you could use log1p, but it's not actually all that useful in this case unless you somehow know $x-y$ to higher precision than $x$ and $y$.