Skip to main content

primitive_float_csc

Function primitive_float_csc 

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

Computes $\csc x$, the cosecant of a primitive float, correctly rounded. Neither the standard library nor libm provides a cosecant.

$$ f(x) = \csc x+\varepsilon. $$

  • If $x$ is not finite, $\varepsilon$ may be ignored or assumed to be 0.
  • If $x$ is finite, then $|\varepsilon| < 2^{\lfloor\log_2 |\csc 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}$
  • $f(\pm\infty)=\text{NaN}$
  • $f(\pm0.0)=\pm\infty$

Overflow is possible: the cosecant of a tiny $x$ is close to $1/x$, so an $x$ with $|x|$ below about $2^{-128}$ has a cosecant beyond the largest f32, and one below about $2^{-1024}$ beyond the largest f64; the result is then $\pm\infty$. No f32 or f64 is close enough to a nonzero multiple of $\pi$ for its cosecant to overflow, and the result is never subnormal, since $|\csc x| \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;

assert!(primitive_float_csc(f32::NAN).is_nan());
assert!(primitive_float_csc(f32::INFINITY).is_nan());
assert!(primitive_float_csc(f32::NEGATIVE_INFINITY).is_nan());
assert_eq!(
    NiceFloat(primitive_float_csc(0.0f32)),
    NiceFloat(f32::INFINITY)
);
assert_eq!(
    NiceFloat(primitive_float_csc(-0.0f32)),
    NiceFloat(f32::NEGATIVE_INFINITY)
);
assert_eq!(NiceFloat(primitive_float_csc(1.0f32)), NiceFloat(1.1883951));
assert_eq!(
    NiceFloat(primitive_float_csc(1.0f64)),
    NiceFloat(1.1883951057781212)
);