3

Many computational libraries contain a routine called sincos that allows one to compute sin and cos simultaneously. The existence of this routine suggests that calling this routine is more efficient than making a call to sin followed by a call to cos.

I am interested in knowing how sincos is (or can be) implemented to make it more efficient than making two calls.

2 Answers2

3

If you compute the cosine-sine pair as point on the unit circle using complex multiplication, which is equivalent to trigonometric theorems, then you automatically get both values at once.

  1. ) To compute $z=e^{ix}$ compute first $z_n=e^{ix/2^n}$ using the Taylor series or polynomials with a more globally reduced error, then perform $n$ squarings $z_{k-1}=z_k^2$.

  2. ) The CORDIC algorithm matches a set of angles and directions in the complex plane. These directions can, step-by-step or in the end, translated into points on the unit circle. In total, $\tan(x)$ is computed first via standard CORDIC and then the pair $(\tan(x),1)$ is scaled to unit length to give the point $(\sin(x),\cos(x))$. This obviously requires a frame procedure around it to account for the cases where $\cos x$ is negative.

However, most modern implementation work via reduction by $\pi$ or smaller fractions of $\pi$ and error-minimized polynomials on the resulting interval to get the required precision. And this procedure in the end has less complexity than the more "geometric" algorithms.

Lutz Lehmann
  • 126,666
0

There's always $\sin(x) = \sqrt{1 -\cos(x)^2}$ and vice versa that allows you to compute one and compute the other with a square root.

However, both glibc and musl libc actually just use two separate calls to $\sin$ and $\cos$ because the above square root has rounding errors around $x = 0$.

Modern CPUs often have a dedicated sincos instruction that computes both values simultaneously. So a sincos function could be used as an interface for that. However, for example on Intel the CPU implementation of trigonometry functions can be quite inaccurate and can not be used by C libraries as the default implementation.

orlp
  • 10,508
  • Uhm . . . that's not what I've heard. Could you give a reference to the "CPU implementations of trig functions are not accurate . . " – user14717 May 03 '17 at 16:25
  • @user14717 https://randomascii.wordpress.com/2014/10/09/intel-underestimates-error-bounds-by-1-3-quintillion/ – orlp May 03 '17 at 16:32
  • Your statement should read "Intel's CPU implementation is inaccurate", not "CPU implementations are inaccurate". – user14717 May 03 '17 at 16:36