1 | /* |
2 | * origin: FreeBSD /usr/src/lib/msun/src/s_atanf.c |
3 | * |
4 | * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com. |
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 | /// The arctangent function. |
16 | pub fn atan2f(y: f32, x: f32) -> f32 { |
17 | const PI: f32 = 3.1415927410e+00; /* 0x40490fdb */ |
18 | const PI_LO: f32 = -8.7422776573e-08; /* 0xb3bbbd2e */ |
19 | |
20 | if x.is_nan() || y.is_nan() { |
21 | return x + y; |
22 | } |
23 | let mut ix = x.to_bits(); |
24 | let mut iy = y.to_bits(); |
25 | |
26 | if ix == 0x3f800000 { |
27 | /* x=1.0 */ |
28 | return super::atan(y); |
29 | } |
30 | let m = ((iy >> 31) & 1) | ((ix >> 30) & 2); /* 2*sign(x)+sign(y) */ |
31 | ix &= 0x7fffffff; |
32 | iy &= 0x7fffffff; |
33 | |
34 | /* when y = 0 */ |
35 | if iy == 0 { |
36 | return match m { |
37 | 0 | 1 => y, /* atan(+-0,+anything)=+-0 */ |
38 | 2 => PI, /* atan(+0,-anything) = pi */ |
39 | 3 | _ => -PI, /* atan(-0,-anything) =-pi */ |
40 | }; |
41 | } |
42 | /* when x = 0 */ |
43 | if ix == 0 { |
44 | return if m & 1 != 0 { |
45 | -PI / 2. |
46 | } else { |
47 | PI / 2. |
48 | }; |
49 | } |
50 | /* when x is INF */ |
51 | if ix == 0x7f800000 { |
52 | return if iy == 0x7f800000 { |
53 | match m { |
54 | 0 => PI / 4., /* atan(+INF,+INF) */ |
55 | 1 => -PI / 4., /* atan(-INF,+INF) */ |
56 | 2 => 3. * PI / 4., /* atan(+INF,-INF)*/ |
57 | 3 | _ => -3. * PI / 4., /* atan(-INF,-INF)*/ |
58 | } |
59 | } else { |
60 | match m { |
61 | 0 => 0., /* atan(+...,+INF) */ |
62 | 1 => -0., /* atan(-...,+INF) */ |
63 | 2 => PI, /* atan(+...,-INF) */ |
64 | 3 | _ => -PI, /* atan(-...,-INF) */ |
65 | } |
66 | }; |
67 | } |
68 | /* |y/x| > 0x1p26 */ |
69 | if (ix + (26 << 23) < iy) || (iy == 0x7f800000) { |
70 | return if m & 1 != 0 { |
71 | -PI / 2. |
72 | } else { |
73 | PI / 2. |
74 | }; |
75 | } |
76 | |
77 | /* z = atan(|y/x|) with correct underflow */ |
78 | let z = if (m & 2 != 0) && (iy + (26 << 23) < ix) { |
79 | /*|y/x| < 0x1p-26, x < 0 */ |
80 | 0. |
81 | } else { |
82 | super::atan(super::abs(y / x)) |
83 | }; |
84 | match m { |
85 | 0 => z, /* atan(+,+) */ |
86 | 1 => -z, /* atan(-,+) */ |
87 | 2 => PI - (z - PI_LO), /* atan(+,-) */ |
88 | _ => (z - PI_LO) - PI, /* case 3 */ /* atan(-,-) */ |
89 | } |
90 | } |
91 | |
92 | pub fn atan2(x: f32, y: f32) -> f32 { |
93 | use core::f32::consts::PI; |
94 | const PI_2: f32 = PI / 2.0; |
95 | const M_PI_2: f32 = -PI / 2.0; |
96 | const PI_4: f32 = PI / 4.0; |
97 | const PI_3_4: f32 = PI_4 * 3.0; |
98 | let mut r; |
99 | let c; |
100 | let abs_y = super::abs(y); |
101 | if x == 0.0 { |
102 | if y > 0.0 { |
103 | return PI_2; |
104 | } |
105 | if y == 0.0 { |
106 | return 0.0; |
107 | } |
108 | return M_PI_2; |
109 | } else if x > 0.0 { |
110 | r = (x - abs_y) / (x + abs_y); |
111 | c = PI_4; |
112 | } else { |
113 | r = (x + abs_y) / (abs_y - x); |
114 | c = PI_3_4; |
115 | } |
116 | r = 0.1963 * r * r * r - 0.9817 * r + c; |
117 | return super::copysign(r, y); |
118 | } |
119 | |