Skip to main content

primitive_float_atan2

Function primitive_float_atan2 

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

Computes $\operatorname{atan2}(y,x)$, the angle of the point $(x,y)$ measured from the positive $x$-axis, for primitive floats.

$$ f(y,x) = \operatorname{atan2}(y,x)+\varepsilon, $$ where $|\varepsilon| < 2^{\lfloor\log_2 |\operatorname{atan2}(y,x)|\rfloor-p}$ and $p$ is the precision of the output (24 if T is a f32 and 53 if T is a f64); the special cases below are exact.

Special cases, in which the sign of a zero argument selects the quadrant:

  • $f(\text{NaN},x)=f(y,\text{NaN})=\text{NaN}$
  • $f(\pm0.0,x)=\pm0.0$ if $x$ is positive or $+0.0$, and $\pm\pi$ if $x$ is negative or $-0.0$
  • $f(y,\pm0.0)=\pm\pi/2$, with the sign of $y$, for nonzero $y$
  • $f(\pm\infty,x)=\pm\pi/2$ for finite $x$, $\pm\pi/4$ for $+\infty$, and $\pm3\pi/4$ for $-\infty$
  • $f(y,+\infty)=\pm0.0$ and $f(y,-\infty)=\pm\pi$, with the sign of $y$, for finite nonzero $y$

Overflow is not possible, since $|\operatorname{atan2}(y,x)| \leq \pi$. The result is subnormal, or zero, only for a positive $x$ with $|y/x|$ subnormal or smaller.

§Worst-case complexity

Constant time and additional memory.

§Examples

use malachite_base::num::float::NiceFloat;
use malachite_float::float::arithmetic::atan2::primitive_float_atan2;

assert!(primitive_float_atan2(f32::NAN, 1.0).is_nan());
assert_eq!(
    NiceFloat(primitive_float_atan2(1.0f32, 1.0)),
    NiceFloat(0.7853982)
);
assert_eq!(
    NiceFloat(primitive_float_atan2(1.0f64, 1.0)),
    NiceFloat(0.7853981633974483)
);
// a negative x with a zero y is half a turn
assert_eq!(
    NiceFloat(primitive_float_atan2(0.0f64, -1.0)),
    NiceFloat(3.141592653589793)
);
assert_eq!(
    NiceFloat(primitive_float_atan2(-0.0f64, -1.0)),
    NiceFloat(-3.141592653589793)
);