Skip to main content

primitive_float_sec_with_period

Function primitive_float_sec_with_period 

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

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

  • If $x$ is not finite, $u=0$, or $x/u$ is an odd multiple of $1/4$, $\varepsilon$ may be ignored or assumed to be 0.
  • Otherwise, $|\varepsilon| < 2^{\lfloor\log_2 |\sec(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 an even multiple of $1/2$, the result is exactly $1$, and at an odd multiple exactly $-1$.
  • If $x/u$ is an odd multiple of $1/4$, the secant has a pole there, and the result is exactly $\infty$: the cosine is $+0.0$ at every such point, and the secant is its reciprocal.
  • If $x/u$ is an odd multiple of $1/8$, the result is $\pm\sqrt2$; if it is a multiple of $1/3$ or $1/6$ but not of $1/2$, the result is exactly $\pm2$; and if it is an odd multiple of $1/12$, the result is $\pm2\sqrt3/3$.

Overflow happens only at a pole, where the result is exactly $\infty$: an f32 or f64 whose fraction of a turn is not an odd multiple of $1/4$ is more than $2^{-66}$ of a turn away from one, so its secant stays below $2^{64}$. Underflow is not possible, since $|\sec(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::sec::primitive_float_sec_with_period;

assert!(primitive_float_sec_with_period(f32::NAN, 360).is_nan());
assert!(primitive_float_sec_with_period(f32::INFINITY, 360).is_nan());
assert!(primitive_float_sec_with_period(f32::NEGATIVE_INFINITY, 360).is_nan());
assert!(primitive_float_sec_with_period(1.0f32, 0).is_nan());
assert_eq!(
    NiceFloat(primitive_float_sec_with_period(-0.0f32, 360)),
    NiceFloat(1.0)
);
// a quarter turn is a pole
assert_eq!(
    NiceFloat(primitive_float_sec_with_period(90.0f32, 360)),
    NiceFloat(f32::INFINITY)
);
// a half turn is exactly -1
assert_eq!(
    NiceFloat(primitive_float_sec_with_period(180.0f32, 360)),
    NiceFloat(-1.0)
);
// an eighth of a turn: sqrt(2)
assert_eq!(
    NiceFloat(primitive_float_sec_with_period(45.0f32, 360)),
    NiceFloat(core::f32::consts::SQRT_2)
);
// a twelfth of a turn: 2 sqrt(3)/3
assert_eq!(
    NiceFloat(primitive_float_sec_with_period(30.0f64, 360)),
    NiceFloat(1.1547005383792515)
);
assert_eq!(
    NiceFloat(primitive_float_sec_with_period(1.0f32, 7)),
    NiceFloat(1.6038755)
);
assert_eq!(
    NiceFloat(primitive_float_sec_with_period(1.0f64, 7)),
    NiceFloat(1.6038754716096766)
);