Skip to main content

primitive_float_sin_with_period

Function primitive_float_sin_with_period 

Source
pub fn primitive_float_sin_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 $\sin(2\pi x/u)$, the sine of a primitive float measured in $u$ths of a turn (so that u = 360 is degrees).

$$ f(x,u) = \sin(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 |\sin(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)=\pm0.0$
  • If $x/u$ is a multiple of $1/2$, the result is exactly $0.0$ with the sign of $x$ (following IEEE 754-2019’s sinPi, so that the function is odd); if it is an odd multiple of $1/4$, the result is exactly $1$ or $-1$; and if it is $\pm1/12$ or $\pm5/12$ modulo $1$, the result is exactly $1/2$ or $-1/2$.

Overflow is not possible, since the result lies in $[-1, 1]$. The result underflows, to a subnormal or to zero, only when $2\pi x/u$ does, which takes a subnormal $x$ or a large $u$; no f32 or f64 is close enough to a half turn, without being one, for its sine to be subnormal.

§Worst-case complexity

Constant time and additional memory.

§Examples

use malachite_base::num::float::NiceFloat;
use malachite_float::float::arithmetic::sin::primitive_float_sin_with_period;

assert!(primitive_float_sin_with_period(f32::NAN, 360).is_nan());
assert!(primitive_float_sin_with_period(f32::INFINITY, 360).is_nan());
assert!(primitive_float_sin_with_period(1.0f32, 0).is_nan());
assert_eq!(
    NiceFloat(primitive_float_sin_with_period(-0.0f32, 360)),
    NiceFloat(-0.0)
);
assert_eq!(
    NiceFloat(primitive_float_sin_with_period(90.0f32, 360)),
    NiceFloat(1.0)
);
assert_eq!(
    NiceFloat(primitive_float_sin_with_period(30.0f64, 360)),
    NiceFloat(0.5)
);
assert_eq!(
    NiceFloat(primitive_float_sin_with_period(1.0f32, 7)),
    NiceFloat(0.7818315)
);
assert_eq!(
    NiceFloat(primitive_float_sin_with_period(1.0f64, 7)),
    NiceFloat(0.7818314824680298)
);