Skip to main content

primitive_float_sec_with_period_rational

Function primitive_float_sec_with_period_rational 

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

Computes $\sec(2\pi x/u)$, the secant of a Rational measured in $u$ths of a turn (so that u = 360 is degrees), returning the result as a primitive float.

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

  • If $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(x,0)=\text{NaN}$
  • $f(0,u)=1$
  • 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$; if it is an odd multiple of $1/12$, the result is $\pm2\sqrt3/3$; and fifths and tenths give $\pm2\varphi$ or $\pm2(\varphi-1)$, where $\varphi$ is the golden ratio.

Overflow is possible away from a pole too: a fraction of a turn within about $2^{-130}$ of an odd multiple of $1/4$ has a secant beyond the largest f32, and one within about $2^{-1026}$ of one beyond the largest f64, and the result is then $\pm\infty$. Underflow is not possible, since $|\sec(2\pi x/u)| \geq 1$.

§Worst-case complexity

$T(m) = O(m (\log m)^2 \log\log m)$

$M(m) = O(m \log m)$

where $T$ is time, $M$ is additional memory, and $m$ is x.significant_bits(): the fraction of a turn is reduced modulo 1 exactly, so the magnitude of $x$ does not drive the cost.

§Examples

use malachite_base::num::basic::traits::Zero;
use malachite_base::num::float::NiceFloat;
use malachite_float::float::arithmetic::sec::primitive_float_sec_with_period_rational;
use malachite_q::Rational;

assert!(primitive_float_sec_with_period_rational::<f64>(&Rational::ZERO, 0).is_nan());
assert_eq!(
    NiceFloat(primitive_float_sec_with_period_rational::<f64>(
        &Rational::ZERO,
        360
    )),
    NiceFloat(1.0)
);
// a quarter turn is a pole
assert_eq!(
    NiceFloat(primitive_float_sec_with_period_rational::<f64>(
        &Rational::from_unsigneds(1u8, 4),
        1
    )),
    NiceFloat(f64::INFINITY)
);
// an eighth of a turn: sqrt(2)
assert_eq!(
    NiceFloat(primitive_float_sec_with_period_rational::<f64>(
        &Rational::from_unsigneds(1u8, 8),
        1
    )),
    NiceFloat(core::f64::consts::SQRT_2)
);
// a twelfth of a turn: 2 sqrt(3)/3
assert_eq!(
    NiceFloat(primitive_float_sec_with_period_rational::<f64>(
        &Rational::from_unsigneds(1u8, 12),
        1
    )),
    NiceFloat(1.1547005383792515)
);
assert_eq!(
    NiceFloat(primitive_float_sec_with_period_rational::<f32>(
        &Rational::from_unsigneds(1u8, 7),
        1
    )),
    NiceFloat(1.6038755)
);
assert_eq!(
    NiceFloat(primitive_float_sec_with_period_rational::<f64>(
        &Rational::from_unsigneds(1u8, 7),
        1
    )),
    NiceFloat(1.6038754716096766)
);