<p><code>sincos()</code> is just a way to compute <code>sin()</code> and <code>cos()</code> at once, possibly at improved performance compared to separate calls to <code>sin()</code> and <code>cos()</code>. 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 <code>sin()</code> and <code>cos()</code> if no facilities to compute them simultaneously is available.</p>
<p>Are there any disadvantages to adding <code>sincos()</code> to a standard library of a language, particularly one that already has <code>sin</code> and <code>cos</code>?</p>

<optimization><math>
Are there any disadvangates to requiring sincos() type functions?
<h1>Multiple return values</h1>
<p>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.</p>

CC BY-SA 4.0
2023-05-19T01:26:47.240Z
2023-05-19T01:26:47.240Z
Bbrk24
CC BY-SA 4.0
2023-05-19T01:27:14.707Z
Return a struct, the same way `div()` does.
CPlus
CC BY-SA 4.0
2023-05-19T01:54:46.937Z
You can also use out pointer parameters.
Isaiah
CC BY-SA 4.0
2023-05-19T03:29:59.653Z
@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.
kaya3
<h1>Floating point math, and speediness</h1>
<p>Yes, there are efficient ways to calculate <code>sin(x)</code> when you have <code>cos(x)</code>, for example, <code>cos(x)^2 = sqrt(1 - sin(x)^2)</code>, or the other methods discussed in  <a href="https://math.stackexchange.com/a/2265171/867286">this answer</a>. 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 <a href="https://math.stackexchange.com/a/2264113/867286">this answer</a>):</p>
<blockquote>
<p>Both <a href="https://github.com/lattera/glibc/blob/a2f34833b1042d5d8eeb263b4cf4caaea138c4ad/sysdeps/ieee754/dbl-64/s_sincos.c#L42" rel="nofollow noreferrer">glibc</a> and <a href="https://github.com/bminor/musl/blob/master/src/math/sincos.c" rel="nofollow noreferrer">musl libc</a> actually just use two separate calls to <code>sin()</code>
and <code>cos()</code></p>
</blockquote>
<p>Most modern CPUs nowadays have a separate instruction for calculating <code>sincos()</code>, so it can be faster, but <a href="https://randomascii.wordpress.com/2014/10/09/intel-underestimates-error-bounds-by-1-3-quintillion/" rel="nofollow noreferrer">Intel CPU is massively inaccurate</a> when it comes to this, so it's understandable some languages don't put this in the standard yet.</p>
<hr />
<p>As <a href="https://languagedesign.stackexchange.com/questions/611/are-there-any-disadvangates-to-requiring-sincos-type-functions#comment722_621">this comment</a> points out, there are some redundant parts in calculating <code>sin()</code> and <code>cos()</code> that can be optimized by using <code>sincos()</code>. And the <a href="https://github.com/jeremybarnes/cephes/blob/master/cmath/sincos.c" rel="nofollow noreferrer">cephes</a> library doesn't call <code>sin()</code> and <code>cos()</code> in <code>sincos()</code>. But speed-wise, <code>sincos()</code> doesn't perform too much faster than <code>sin()</code> and <code>cos()</code> separately (I think) to be include in the standard library.</p>

CC BY-SA 4.0
2023-05-19T01:48:40.463Z
2023-05-19T02:23:42.940Z
2023-05-19T02:23:42.940Z
justANewbie
CC BY-SA 4.0
2023-10-24T16:19:25.623Z
If one knows `sin(q)` and `cos(q)`, for some value of `q` near `x`, then `sin(x)` can be approximated as `sin(q)+cos(q)*(x-q)` and `cos(x)` can be approximated as `cos(q)-sin(q)*(x-q)`, so computations of `q`, `sin(q)`, `cos(q)`, and `x-q` can be shared between the computations of `cos(x)` and `sin(x)`.
supercat
CC BY-SA 4.0
2023-05-19T01:56:08.797Z
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.
Pseudonym
CC BY-SA 4.0
2023-05-19T02:45:17.183Z
given comment about intel CPUs is from 2014. Is there any analysis more recent?
Razetime
CC BY-SA 4.0
2023-05-19T03:02:45.990Z
@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.
Pseudonym
CC BY-SA 4.0
2023-05-19T03:07:24.100Z
@Razetime not that I know of, but in the article Intel seems to keep it because of backwards compatibility?
justANewbie
CC BY-SA 4.0
2023-05-19T03:35:21.210Z
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.
kaya3
CC BY-SA 4.0
2023-05-19T06:57:28.683Z
@Pseudonym interesting, could you provide me with what CPUs and GPUs are having separate instructions for `sincos()` and what consider it to be deprecated?
justANewbie
CC BY-SA 4.0
2023-05-20T01:25:59.853Z
@justANewbie I'm not aware of any modern architecture that has a combined `sincos` instruction. The only extant one that I'm aware of is x87, which isn't a modern architecture and is deprecated. However, many *abstract* machines do support it, such as SPIR-V, and many compilers will optimise the case of `sin` and `cos` of the same arguments, because that *combination* can be compiled to more efficient code (e.g. see `__sincos_stret` in LLVM).
Pseudonym
# Are there any disadvangates to requiring sincos() type functions?
Posted by: username_0 on 2023-05-19
`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`?
**Tags:** optimization math
---
## 2 answers
### Answer by username_4 on 2023-05-19. Score: 5 | Accepted answer
# 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](https://math.stackexchange.com/a/2265171/867286). 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](https://math.stackexchange.com/a/2264113/867286)):

> Both [glibc](https://github.com/lattera/glibc/blob/a2f34833b1042d5d8eeb263b4cf4caaea138c4ad/sysdeps/ieee754/dbl-64/s_sincos.c#L42) and [musl libc](https://github.com/bminor/musl/blob/master/src/math/sincos.c) 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](https://randomascii.wordpress.com/2014/10/09/intel-underestimates-error-bounds-by-1-3-quintillion/) when it comes to this, so it's understandable some languages don't put this in the standard yet.

---

As [this comment](https://languagedesign.stackexchange.com/questions/611/are-there-any-disadvangates-to-requiring-sincos-type-functions#comment722_621) points out, there are some redundant parts in calculating `sin()` and `cos()` that can be optimized by using `sincos()`. And the [cephes](https://github.com/jeremybarnes/cephes/blob/master/cmath/sincos.c) 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.
> **Comments:**
> **username_6** (2023-05-19): 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.  
> **username_7** (2023-05-19): given comment about intel CPUs is from 2014. Is there any analysis more recent?  
> **username_6** (2023-05-19): @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.  
> **username_4** (2023-05-19): @Razetime not that I know of, but in the article Intel seems to keep it because of backwards compatibility?  
> **username_3** (2023-05-19): 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.  
> **username_4** (2023-05-19): @Pseudonym interesting, could you provide me with what CPUs and GPUs are having separate instructions for `sincos()` and what consider it to be deprecated?  
> **username_6** (2023-05-20): @justANewbie I'm not aware of any modern architecture that has a combined `sincos` instruction. The only extant one that I'm aware of is x87, which isn't a modern architecture and is deprecated. However, many *abstract* machines do support it, such as SPIR-V, and many compilers will optimise the case of `sin` and `cos` of the same arguments, because that *combination* can be compiled to more efficient code (e.g. see `__sincos_stret` in LLVM).  
> **username_5** (2023-10-24): If one knows `sin(q)` and `cos(q)`, for some value of `q` near `x`, then `sin(x)` can be approximated as `sin(q)+cos(q)*(x-q)` and `cos(x)` can be approximated as `cos(q)-sin(q)*(x-q)`, so computations of `q`, `sin(q)`, `cos(q)`, and `x-q` can be shared between the computations of `cos(x)` and `sin(x)`.  
---
### Answer by username_1 on 2023-05-19. Score: 6
# 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.
> **Comments:**
> **username_0** (2023-05-19): Return a struct, the same way `div()` does.  
> **username_2** (2023-05-19): You can also use out pointer parameters.  
> **username_3** (2023-05-19): @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.  
---
