Skip to main content

primitive_float_cot_with_period_rational

Function primitive_float_cot_with_period_rational 

Source
pub fn primitive_float_cot_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 $\cot(2\pi x/u)$, the cotangent 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) = \cot(2\pi x/u)+\varepsilon. $$

  • If $u=0$ or $x/u$ is a multiple of $1/8$, $\varepsilon$ may be ignored or assumed to be 0.
  • Otherwise, $|\varepsilon| < 2^{\lfloor\log_2 |\cot(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)=\infty$
  • If $x/u$ is a multiple of $1/2$, the cotangent has a pole there, and the result is exactly $\pm\infty$: the sine is a zero carrying the sign of $x$ and the cosine is $\pm1$, so the sign is that of $x$ at an even multiple and the opposite at an odd one.
  • If $x/u$ is an odd multiple of $1/4$, the result is exactly $\pm0.0$, and if it is an odd multiple of $1/8$, exactly $\pm1$.
  • If $x/u$ in lowest terms has denominator 3 or 6, the result is $\pm\sqrt3/3$, and if it has denominator 12, $\pm\sqrt3$.

Overflow is possible away from a pole too: a fraction of a turn within about $2^{-130}$ of a multiple of $1/2$ has a cotangent beyond the largest f32, and one within about $2^{-1026}$ of one beyond the largest f64; so does a fraction of a turn small enough on its own, which a Rational can be however large its denominator is not. The result is then $\pm\infty$. A fraction of a turn as close to an odd multiple of $1/4$ underflows instead, to $\pm0.0$.

§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::cot::primitive_float_cot_with_period_rational;
use malachite_q::Rational;

assert!(primitive_float_cot_with_period_rational::<f64>(&Rational::ZERO, 0).is_nan());
assert_eq!(
    NiceFloat(primitive_float_cot_with_period_rational::<f64>(
        &Rational::ZERO,
        360
    )),
    NiceFloat(f64::INFINITY)
);
// a quarter turn is exactly 0
assert_eq!(
    NiceFloat(primitive_float_cot_with_period_rational::<f64>(
        &Rational::from_unsigneds(1u8, 4),
        1
    )),
    NiceFloat(0.0)
);
// an eighth of a turn is exactly 1
assert_eq!(
    NiceFloat(primitive_float_cot_with_period_rational::<f64>(
        &Rational::from_unsigneds(1u8, 8),
        1
    )),
    NiceFloat(1.0)
);
// a twelfth of a turn: sqrt(3)
assert_eq!(
    NiceFloat(primitive_float_cot_with_period_rational::<f64>(
        &Rational::from_unsigneds(1u8, 12),
        1
    )),
    NiceFloat(1.7320508075688772)
);
assert_eq!(
    NiceFloat(primitive_float_cot_with_period_rational::<f32>(
        &Rational::from_unsigneds(1u8, 7),
        1
    )),
    NiceFloat(0.7974734)
);
assert_eq!(
    NiceFloat(primitive_float_cot_with_period_rational::<f64>(
        &Rational::from_unsigneds(1u8, 7),
        1
    )),
    NiceFloat(0.7974733888824039)
);