6
$\begingroup$

sincos() is just a way to compute sin() and cos() at once, possibly at improved performance compared to separate calls to sin() and cos(). However, many languages, notably C, lack this feature in the standard library. I do not see how this could be hard to implement, as the compiler could just emit separate calls to sin() and cos() if no facilities to compute them simultaneously is available.

Are there any disadvantages to adding sincos() to a standard library of a language, particularly one that already has sin and cos?

$\endgroup$

2 Answers 2

5
$\begingroup$

Floating point math, and speediness

Yes, there are efficient ways to calculate sin(x) when you have cos(x), for example, cos(x)^2 = sqrt(1 - sin(x)^2), or the other methods discussed in this answer. But calculating using square root is prone to rounding errors, and the other methods are just slow compare to what is currently used. That is why (quoted this answer):

Both glibc and musl libc actually just use two separate calls to sin() and cos()

Most modern CPUs nowadays have a separate instruction for calculating sincos(), so it can be faster, but Intel CPU is massively inaccurate when it comes to this, so it's understandable some languages don't put this in the standard yet.


As this comment points out, there are some redundant parts in calculating sin() and cos() that can be optimized by using sincos(). And the cephes library doesn't call sin() and cos() in sincos(). But speed-wise, sincos() doesn't perform too much faster than sin() and cos() separately (I think) to be include in the standard library.

$\endgroup$
8
  • 2
    $\begingroup$ It is usually more efficient to calculate sin and cos together rather than separately; range reduction and quadrant analysis only needs to be performed once, for example. $\endgroup$
    – Pseudonym
    May 19, 2023 at 1:56
  • $\begingroup$ given comment about intel CPUs is from 2014. Is there any analysis more recent? $\endgroup$
    – Razetime
    May 19, 2023 at 2:45
  • $\begingroup$ @Razetime has a point. The statement that "most modern CPUs nowadays have a separate instruction for calculating sincos()" is arguably untrue. ARM certainly doesn't, and it seems rare on GPUs, too. The modern CPUs that have one consider it to be deprecated. $\endgroup$
    – Pseudonym
    May 19, 2023 at 3:02
  • $\begingroup$ @Razetime not that I know of, but in the article Intel seems to keep it because of backwards compatibility? $\endgroup$ May 19, 2023 at 3:07
  • $\begingroup$ There is a good chance that a single CPU instruction which is rarely used, and only kept for backwards compatibility, may actually be slower than two instructions that are each frequently used. CPUs have a budget for transistors and the space they take up, so it may not be worth Intel adding more transistors just to make a rarely-used instruction more efficient, particularly since those extra transistors would make other paths in the chip longer to go around them. So I wouldn't take it for granted that computing both together is really more efficient ─ this needs empirical evidence. $\endgroup$
    – kaya3
    May 19, 2023 at 3:35
6
$\begingroup$

Multiple return values

Since you listed C as an example of a language that doesn’t have this, it’s worth pointing out that the language itself doesn’t facilitate returning multiple values from one function.

$\endgroup$
3
  • 3
    $\begingroup$ Return a struct, the same way div() does. $\endgroup$
    – CPlus
    May 19, 2023 at 1:27
  • 1
    $\begingroup$ You can also use out pointer parameters. $\endgroup$
    – Isaiah
    May 19, 2023 at 1:54
  • $\begingroup$ @user16217248 You can return a data structure which holds both results, but in many languages the performance cost of creating and accessing that data structure could be greater than the possible benefit of not having to call sin and cos separately. It may require allocating memory for the object, two writes and two reads from that object through its reference, and then deallocating the object. $\endgroup$
    – kaya3
    May 19, 2023 at 3:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .