trig_const/
atan2.rs

1/* origin: FreeBSD /usr/src/lib/msun/src/e_atan2.c */
2/*
3 * ====================================================
4 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
5 *
6 * Developed at SunSoft, a Sun Microsystems, Inc. business.
7 * Permission to use, copy, modify, and distribute this
8 * software is freely granted, provided that this notice
9 * is preserved.
10 * ====================================================
11 *
12 */
13/* atan2(y,x)
14 * Method :
15 *      1. Reduce y to positive by atan2(y,x)=-atan2(-y,x).
16 *      2. Reduce x to positive by (if x and y are unexceptional):
17 *              ARG (x+iy) = arctan(y/x)           ... if x > 0,
18 *              ARG (x+iy) = pi - arctan[y/(-x)]   ... if x < 0,
19 *
20 * Special cases:
21 *
22 *      ATAN2((anything), NaN ) is NaN;
23 *      ATAN2(NAN , (anything) ) is NaN;
24 *      ATAN2(+-0, +(anything but NaN)) is +-0  ;
25 *      ATAN2(+-0, -(anything but NaN)) is +-pi ;
26 *      ATAN2(+-(anything but 0 and NaN), 0) is +-pi/2;
27 *      ATAN2(+-(anything but INF and NaN), +INF) is +-0 ;
28 *      ATAN2(+-(anything but INF and NaN), -INF) is +-pi;
29 *      ATAN2(+-INF,+INF ) is +-pi/4 ;
30 *      ATAN2(+-INF,-INF ) is +-3pi/4;
31 *      ATAN2(+-INF, (anything but,0,NaN, and INF)) is +-pi/2;
32 *
33 * Constants:
34 * The hexadecimal values are the intended ones for the following
35 * constants. The decimal values may be used, provided that the
36 * compiler will convert from decimal to binary accurately enough
37 * to produce the hexadecimal values shown.
38 */
39use core::f64::consts::PI;
40
41use crate::{atan, fabs};
42
43/// Arctan2
44///
45/// ```
46/// # use trig_const::atan2;
47/// # use core::f64::consts::PI;
48/// # fn float_eq(lhs: f64, rhs: f64) { assert!((lhs - rhs).abs() < 0.0001, "lhs: {}, rhs: {}", lhs, rhs); }
49/// const ATAN2_0_1: f64 = atan2(0.0, 1.0);
50/// float_eq(ATAN2_0_1, 0.0);
51/// ```
52pub const fn atan2(y: f64, x: f64) -> f64 {
53    const PI_LO: f64 = 1.2246467991473531772E-16; /* 0x3CA1A626, 0x33145C07 */
54    if x.is_nan() || y.is_nan() {
55        return x + y;
56    }
57    let mut ix = (x.to_bits() >> 32) as u32;
58    let lx = x.to_bits() as u32;
59    let mut iy = (y.to_bits() >> 32) as u32;
60    let ly = y.to_bits() as u32;
61    if ((ix.wrapping_sub(0x3ff00000)) | lx) == 0 {
62        /* x = 1.0 */
63        return atan(y);
64    }
65    let m = ((iy >> 31) & 1) | ((ix >> 30) & 2); /* 2*sign(x)+sign(y) */
66    ix &= 0x7fffffff;
67    iy &= 0x7fffffff;
68
69    /* when y = 0 */
70    if (iy | ly) == 0 {
71        return match m {
72            0 | 1 => y, /* atan(+-0,+anything)=+-0 */
73            2 => PI,    /* atan(+0,-anything) = PI */
74            _ => -PI,   /* atan(-0,-anything) =-PI */
75        };
76    }
77    /* when x = 0 */
78    if (ix | lx) == 0 {
79        return if m & 1 != 0 { -PI / 2.0 } else { PI / 2.0 };
80    }
81    /* when x is INF */
82    if ix == 0x7ff00000 {
83        if iy == 0x7ff00000 {
84            return match m {
85                0 => PI / 4.0,        /* atan(+INF,+INF) */
86                1 => -PI / 4.0,       /* atan(-INF,+INF) */
87                2 => 3.0 * PI / 4.0,  /* atan(+INF,-INF) */
88                _ => -3.0 * PI / 4.0, /* atan(-INF,-INF) */
89            };
90        } else {
91            return match m {
92                0 => 0.0,  /* atan(+...,+INF) */
93                1 => -0.0, /* atan(-...,+INF) */
94                2 => PI,   /* atan(+...,-INF) */
95                _ => -PI,  /* atan(-...,-INF) */
96            };
97        }
98    }
99    /* |y/x| > 0x1p64 */
100    if ix.wrapping_add(64 << 20) < iy || iy == 0x7ff00000 {
101        return if m & 1 != 0 { -PI / 2.0 } else { PI / 2.0 };
102    }
103
104    /* z = atan(|y/x|) without spurious underflow */
105    let z = if (m & 2 != 0) && iy.wrapping_add(64 << 20) < ix {
106        /* |y/x| < 0x1p-64, x<0 */
107        0.0
108    } else {
109        atan(fabs(y / x))
110    };
111    match m {
112        0 => z,                /* atan(+,+) */
113        1 => -z,               /* atan(-,+) */
114        2 => PI - (z - PI_LO), /* atan(+,-) */
115        _ => (z - PI_LO) - PI, /* atan(-,-) */
116    }
117}