Skip to main content

primitive_float_tan_with_period

Function primitive_float_tan_with_period 

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

$$ f(x,u) = \tan(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 |\tan(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)=\pm0.0$
  • If $x/u$ is a multiple of $1/2$, the result is exactly $0.0$: with the sign of $x$ at an even multiple, and the opposite sign at an odd one, since the tangent reaches each of its zeros from below and the function is odd.
  • If $x/u$ is an odd multiple of $1/4$, the tangent has a pole there, and the result is exactly $\infty$ (at $1/4$ modulo $1$) or $-\infty$ (at $3/4$).
  • If $x/u$ is an odd multiple of $1/8$, the result is exactly $1$ or $-1$.

Overflow happens only at a pole, where the result is exactly $\pm\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 tangent stays below $2^{64}$. The result underflows, to a subnormal or to zero, only when $2\pi x/u$ does, which takes a subnormal $x$ or a large $u$; no f32 or f64 is close enough to a nonzero multiple of a half turn, without being one, for its tangent to be subnormal.

§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::tan::primitive_float_tan_with_period;

assert!(primitive_float_tan_with_period(f32::NAN, 360).is_nan());
assert!(primitive_float_tan_with_period(f32::INFINITY, 360).is_nan());
assert!(primitive_float_tan_with_period(f32::NEGATIVE_INFINITY, 360).is_nan());
assert!(primitive_float_tan_with_period(1.0f32, 0).is_nan());
assert_eq!(
    NiceFloat(primitive_float_tan_with_period(-0.0f32, 360)),
    NiceFloat(-0.0)
);
// a quarter turn is a pole
assert_eq!(
    NiceFloat(primitive_float_tan_with_period(90.0f32, 360)),
    NiceFloat(f32::INFINITY)
);
// a half turn is exactly zero, reached from below
assert_eq!(
    NiceFloat(primitive_float_tan_with_period(180.0f32, 360)),
    NiceFloat(-0.0)
);
// an eighth of a turn is exactly 1
assert_eq!(
    NiceFloat(primitive_float_tan_with_period(45.0f32, 360)),
    NiceFloat(1.0)
);
// a twelfth of a turn: sqrt(3)/3
assert_eq!(
    NiceFloat(primitive_float_tan_with_period(30.0f64, 360)),
    NiceFloat(0.5773502691896257)
);
assert_eq!(
    NiceFloat(primitive_float_tan_with_period(1.0f32, 7)),
    NiceFloat(1.2539604)
);
assert_eq!(
    NiceFloat(primitive_float_tan_with_period(1.0f64, 7)),
    NiceFloat(1.2539603376627038)
);