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?
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}$$