1 /* origin: FreeBSD /usr/src/lib/msun/src/e_atan2f.c */
2 /*
3  * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com.
4  */
5 /*
6  * ====================================================
7  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
8  *
9  * Developed at SunPro, a Sun Microsystems, Inc. business.
10  * Permission to use, copy, modify, and distribute this
11  * software is freely granted, provided that this notice
12  * is preserved.
13  * ====================================================
14  */
15 
16 use super::atanf;
17 use super::fabsf;
18 
19 const PI: f32 = 3.1415927410e+00; /* 0x40490fdb */
20 const PI_LO: f32 = -8.7422776573e-08; /* 0xb3bbbd2e */
21 
22 /// Arctangent of y/x (f32)
23 ///
24 /// Computes the inverse tangent (arc tangent) of `y/x`.
25 /// Produces the correct result even for angles near pi/2 or -pi/2 (that is, when `x` is near 0).
26 /// Returns a value in radians, in the range of -pi to pi.
27 #[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)]
atan2f(y: f32, x: f32) -> f3228 pub fn atan2f(y: f32, x: f32) -> f32 {
29     if x.is_nan() || y.is_nan() {
30         return x + y;
31     }
32     let mut ix = x.to_bits();
33     let mut iy = y.to_bits();
34 
35     if ix == 0x3f800000 {
36         /* x=1.0 */
37         return atanf(y);
38     }
39     let m = ((iy >> 31) & 1) | ((ix >> 30) & 2); /* 2*sign(x)+sign(y) */
40     ix &= 0x7fffffff;
41     iy &= 0x7fffffff;
42 
43     /* when y = 0 */
44     if iy == 0 {
45         return match m {
46             0 | 1 => y,   /* atan(+-0,+anything)=+-0 */
47             2 => PI,      /* atan(+0,-anything) = pi */
48             3 | _ => -PI, /* atan(-0,-anything) =-pi */
49         };
50     }
51     /* when x = 0 */
52     if ix == 0 {
53         return if m & 1 != 0 { -PI / 2. } else { PI / 2. };
54     }
55     /* when x is INF */
56     if ix == 0x7f800000 {
57         return if iy == 0x7f800000 {
58             match m {
59                 0 => PI / 4.,           /* atan(+INF,+INF) */
60                 1 => -PI / 4.,          /* atan(-INF,+INF) */
61                 2 => 3. * PI / 4.,      /* atan(+INF,-INF)*/
62                 3 | _ => -3. * PI / 4., /* atan(-INF,-INF)*/
63             }
64         } else {
65             match m {
66                 0 => 0.,      /* atan(+...,+INF) */
67                 1 => -0.,     /* atan(-...,+INF) */
68                 2 => PI,      /* atan(+...,-INF) */
69                 3 | _ => -PI, /* atan(-...,-INF) */
70             }
71         };
72     }
73     /* |y/x| > 0x1p26 */
74     if (ix + (26 << 23) < iy) || (iy == 0x7f800000) {
75         return if m & 1 != 0 { -PI / 2. } else { PI / 2. };
76     }
77 
78     /* z = atan(|y/x|) with correct underflow */
79     let z = if (m & 2 != 0) && (iy + (26 << 23) < ix) {
80         /*|y/x| < 0x1p-26, x < 0 */
81         0.
82     } else {
83         atanf(fabsf(y / x))
84     };
85     match m {
86         0 => z,                /* atan(+,+) */
87         1 => -z,               /* atan(-,+) */
88         2 => PI - (z - PI_LO), /* atan(+,-) */
89         _ => (z - PI_LO) - PI, /* case 3 */ /* atan(-,-) */
90     }
91 }
92