For exploratory work related to special function implementations, I need to compute $\log \frac{\sin y}{\sin x} $, where $0 \le x \le y \le 2x < \frac{\pi}{2}$. Cases with $x \approx y$ in particular are critical to overall accuracy.
Given that the ratio of the sines is often close to unity, I want to use the log1p
function to compute the logarithm as accurately as possible, which means I need to find a way to compute $\frac{\sin y}{\sin x}-1$ accurately.
Given the pre-conditions, based on the Sterbenz lemma, $\delta = y - x$ can be computed exactly with binary floating-point arithmetic. With the help of the angle-sum and half-angle formulas, I then get
$$ \sin y = \sin(x+\delta) = \sin(x) + \left(\sin(\delta) \cos(x) - 2 \sin^{2}\left(\frac{\delta}{2}\right) \sin x \right) $$
from which follows immediately
$$\frac{\sin y}{\sin x} - 1 = \sin(\delta) \frac{\cos x}{\sin x} - 2 \sin^{2} \left(\frac{\delta}{2}\right) $$
Given the pre-conditions, there is no risk of cancellation in the subtraction, since the minuend is at least twice as large as the subtrahend, and usually much larger than that. This computation is performance sensitive, and since a function sincos
is available that computes $\sin$ and $\cos$ in one go, I have also considered rewriting the above as follows to reduce the cost of computing all transcendentals to just two sincos
calls (presumably trading-off with a small increase in round-off error)
$$2\sin\left(\frac{\delta}{2}\right) \cos\left(\frac{\delta}{2}\right)\frac{\cos x}{\sin x} - 2\sin^{2}\left(\frac{\delta}{2}\right)$$
This could be further transformed into the following but I have not checked yet whether this is actually advantageous
$$2\sin\left(\frac{\delta}{2}\right) \left(\cos\left(\frac{\delta}{2}\right)\frac{\cos x}{\sin x} - \sin\left(\frac{\delta}{2}\right)\right)$$
Is there an alternate arrangement of this computation that also maintains full accuracy and further minimizes computational cost? The availability of fused-multiply add (FMA) can be assumed. Abstract operational costs are as follows: add
, sub
, mul
, fma
= 1; div
, sqrt
, sin
, cos
= 10; log
, log1p
, sincos
= 15; tan
= 20.
sqrt
is about the same cost assin
,cos
, which have fairly low cost here due to the limited range of the inputs. I will update the question with that information. $\endgroup$cot
is not available right now. I can synthesize it fromdiv
andsincos
, or (given more time) create a dedicated implementation, since my current approach would need that anyhow. I would appreciate a more detailed answer along the lines of your comment. $\endgroup$