Skip to main content

primitive_float_cos_with_period

Function primitive_float_cos_with_period 

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

Computes $\cos(2\pi x/u)$, the cosine of a primitive float measured in $u$ths of a turn (so that u = 360 is degrees).

$$ f(x,u) = \cos(2\pi x/u)+\varepsilon. $$

  • If $x$ is not finite or $u=0$, $\varepsilon$ may be ignored or assumed to be 0.
  • If $x$ is finite and $u\neq 0$, then $|\varepsilon| < 2^{\lfloor\log_2 |\cos(2\pi x/u)|\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},u)=\text{NaN}$
  • $f(\pm\infty,u)=\text{NaN}$
  • $f(x,0)=\text{NaN}$
  • $f(\pm0.0,u)=1.0$
  • If $x/u$ is a multiple of $1/2$, the result is exactly $1$ or $-1$; if it is an odd multiple of $1/4$, the result is exactly $0.0$ (always positive, following IEEE 754-2019’s cosPi); and if it is an odd multiple of $1/6$ or $1/3$, the result is exactly $1/2$ or $-1/2$.

Overflow and underflow are not possible: the result lies in $[-1, 1]$, and no f32 or f64 is close enough to an odd quarter turn, without being one, for its cosine to be subnormal.

§Worst-case complexity

Constant time and additional memory.

§Examples

use malachite_base::num::float::NiceFloat;
use malachite_float::float::arithmetic::cos::primitive_float_cos_with_period;

assert!(primitive_float_cos_with_period(f32::NAN, 360).is_nan());
assert!(primitive_float_cos_with_period(f32::INFINITY, 360).is_nan());
assert!(primitive_float_cos_with_period(1.0f32, 0).is_nan());
assert_eq!(
    NiceFloat(primitive_float_cos_with_period(0.0f32, 360)),
    NiceFloat(1.0)
);
assert_eq!(
    NiceFloat(primitive_float_cos_with_period(90.0f32, 360)),
    NiceFloat(0.0)
);
assert_eq!(
    NiceFloat(primitive_float_cos_with_period(60.0f64, 360)),
    NiceFloat(0.5)
);
assert_eq!(
    NiceFloat(primitive_float_cos_with_period(1.0f32, 7)),
    NiceFloat(0.6234898)
);
assert_eq!(
    NiceFloat(primitive_float_cos_with_period(1.0f64, 7)),
    NiceFloat(0.6234898018587335)
);