Skip to main content

primitive_float_sin_cos

Function primitive_float_sin_cos 

Source
pub fn primitive_float_sin_cos<T>(x: T) -> (T, T)
where Float: From<T> + PartialOrd<T>, for<'a> T: ExactFrom<&'a Float> + RoundingFrom<&'a Float> + PrimitiveFloat,
Expand description

Computes $\sin x$ and $\cos x$, the sine and cosine of a primitive float, together. Using this function is more accurate than using the default sin_cos function or the ones provided by libm.

The results are those of primitive_float_sin and primitive_float_cos, but the argument reduction and most of the work are shared, so this is faster than the two calls when both values are needed.

$$ f(x) = (\sin x+\varepsilon_s, \cos x+\varepsilon_c). $$

  • If $x$ is not finite, $\varepsilon_s$ and $\varepsilon_c$ may be ignored or assumed to be 0.
  • If $x$ is finite, then $|\varepsilon_s| < 2^{\lfloor\log_2 |\sin x|\rfloor-p}$ and $|\varepsilon_c| < 2^{\lfloor\log_2 |\cos x|\rfloor-p}$, where $p$ is the precision of the output (24 if T is a f32 and 53 if T is a f64).

Special cases:

  • $f(\text{NaN})=(\text{NaN},\text{NaN})$
  • $f(\pm\infty)=(\text{NaN},\text{NaN})$
  • $f(\pm0.0)=(\pm0.0,1.0)$

Overflow is not possible, since the results lie in $[-1, 1]$. The sine is subnormal only when $x$ is, and then it is $x$ itself; the cosine is never subnormal. See primitive_float_sin and primitive_float_cos.

§Worst-case complexity

Constant time and additional memory.

§Examples

use malachite_base::num::float::NiceFloat;
use malachite_float::float::arithmetic::sin_cos::primitive_float_sin_cos;

let (s, c) = primitive_float_sin_cos(f32::NAN);
assert!(s.is_nan());
assert!(c.is_nan());

let (s, c) = primitive_float_sin_cos(0.0f32);
assert_eq!(NiceFloat(s), NiceFloat(0.0));
assert_eq!(NiceFloat(c), NiceFloat(1.0));

let (s, c) = primitive_float_sin_cos(1.0f32);
assert_eq!(NiceFloat(s), NiceFloat(0.84147096));
assert_eq!(NiceFloat(c), NiceFloat(0.5403023));

let (s, c) = primitive_float_sin_cos(1.0f64);
assert_eq!(NiceFloat(s), NiceFloat(0.8414709848078965));
assert_eq!(NiceFloat(c), NiceFloat(0.5403023058681398));