Skip to main content

primitive_float_csc_with_period

Function primitive_float_csc_with_period 

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

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

  • If $x$ is not finite, $u=0$, or $x/u$ is a multiple of $1/4$ or has denominator 12 in lowest terms, $\varepsilon$ may be ignored or assumed to be 0.
  • Otherwise, $|\varepsilon| < 2^{\lfloor\log_2 |\csc(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)=\pm\infty$
  • If $x/u$ is a multiple of $1/2$, the cosecant has a pole there, and the result is exactly $\pm\infty$ with the sign of $x$: the sine is a zero carrying that sign, and the cosecant is its reciprocal, which keeps the function odd.
  • If $x/u$ in lowest terms has denominator 4, the result is exactly $\pm1$, and if it has denominator 12, exactly $\pm2$.
  • If $x/u$ in lowest terms has denominator 3 or 6, the result is $\pm2\sqrt3/3$; if 8, $\pm\sqrt2$; and if 20, $\pm2\varphi$ or $\pm2(\varphi-1)$, where $\varphi$ is the golden ratio.

Overflow happens at a pole, where the result is exactly $\pm\infty$, and for a tiny $x/u$, whose cosecant is close to $u/(2\pi x)$: an f32 or f64 whose fraction of a turn is not a multiple of $1/2$ is more than $2^{-66}$ of a turn away from one, so a cosecant that is not a pole stays below $2^{64}$ unless the angle itself is tiny. Underflow is not possible, since $|\csc(2\pi x/u)| \geq 1$.

§Worst-case complexity

Constant time and additional memory.

§Examples

use malachite_base::num::basic::traits::NegativeInfinity;
use malachite_base::num::float::NiceFloat;
use malachite_float::float::arithmetic::csc::primitive_float_csc_with_period;

assert!(primitive_float_csc_with_period(f32::NAN, 360).is_nan());
assert!(primitive_float_csc_with_period(f32::INFINITY, 360).is_nan());
assert!(primitive_float_csc_with_period(f32::NEGATIVE_INFINITY, 360).is_nan());
assert!(primitive_float_csc_with_period(1.0f32, 0).is_nan());
assert_eq!(
    NiceFloat(primitive_float_csc_with_period(-0.0f32, 360)),
    NiceFloat(f32::NEGATIVE_INFINITY)
);
// a quarter turn is exactly 1
assert_eq!(
    NiceFloat(primitive_float_csc_with_period(90.0f32, 360)),
    NiceFloat(1.0)
);
// a half turn is a pole
assert_eq!(
    NiceFloat(primitive_float_csc_with_period(180.0f32, 360)),
    NiceFloat(f32::INFINITY)
);
// a sixth of a turn: 2 sqrt(3)/3
assert_eq!(
    NiceFloat(primitive_float_csc_with_period(60.0f32, 360)),
    NiceFloat(1.1547005)
);
// a twelfth of a turn is exactly 2
assert_eq!(
    NiceFloat(primitive_float_csc_with_period(30.0f64, 360)),
    NiceFloat(2.0)
);
assert_eq!(
    NiceFloat(primitive_float_csc_with_period(1.0f32, 7)),
    NiceFloat(1.279048)
);
assert_eq!(
    NiceFloat(primitive_float_csc_with_period(1.0f64, 7)),
    NiceFloat(1.2790480076899327)
);