| 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 |
*/ |
| 39 |
|
| 40 |
use super::{atan, fabs}; |
| 41 |
|
| 42 |
const PI: f64 = 3.1415926535897931160E+00; /* 0x400921FB, 0x54442D18 */ |
| 43 |
const PI_LO: f64 = 1.2246467991473531772E-16; /* 0x3CA1A626, 0x33145C07 */ |
| 44 |
|
| 45 |
/// Arctangent of y/x (f64) |
| 46 |
/// |
| 47 |
/// Computes the inverse tangent (arc tangent) of `y/x`. |
| 48 |
/// Produces the correct result even for angles near pi/2 or -pi/2 (that is, when `x` is near 0). |
| 49 |
/// Returns a value in radians, in the range of -pi to pi. |
| 50 |
#[cfg_attr(assert_no_panic, no_panic::no_panic)] |
| 51 |
pub fn atan2(y: f64, x: f64) -> f64 { |
| 52 |
if x.is_nan() || y.is_nan() { |
| 53 |
return x + y; |
| 54 |
} |
| 55 |
let mut ix = (x.to_bits() >> 32) as u32; |
| 56 |
let lx = x.to_bits() as u32; |
| 57 |
let mut iy = (y.to_bits() >> 32) as u32; |
| 58 |
let ly = y.to_bits() as u32; |
| 59 |
if ((ix.wrapping_sub(0x3ff00000)) | lx) == 0 { |
| 60 |
/* x = 1.0 */ |
| 61 |
return atan(y); |
| 62 |
} |
| 63 |
let m = ((iy >> 31) & 1) | ((ix >> 30) & 2); /* 2*sign(x)+sign(y) */ |
| 64 |
ix &= 0x7fffffff; |
| 65 |
iy &= 0x7fffffff; |
| 66 |
|
| 67 |
/* when y = 0 */ |
| 68 |
if (iy | ly) == 0 { |
| 69 |
return match m { |
| 70 |
0 | 1 => y, /* atan(+-0,+anything)=+-0 */ |
| 71 |
2 => PI, /* atan(+0,-anything) = PI */ |
| 72 |
_ => -PI, /* atan(-0,-anything) =-PI */ |
| 73 |
}; |
| 74 |
} |
| 75 |
/* when x = 0 */ |
| 76 |
if (ix | lx) == 0 { |
| 77 |
return if m & 1 != 0 { -PI / 2.0 } else { PI / 2.0 }; |
| 78 |
} |
| 79 |
/* when x is INF */ |
| 80 |
if ix == 0x7ff00000 { |
| 81 |
if iy == 0x7ff00000 { |
| 82 |
return match m { |
| 83 |
0 => PI / 4.0, /* atan(+INF,+INF) */ |
| 84 |
1 => -PI / 4.0, /* atan(-INF,+INF) */ |
| 85 |
2 => 3.0 * PI / 4.0, /* atan(+INF,-INF) */ |
| 86 |
_ => -3.0 * PI / 4.0, /* atan(-INF,-INF) */ |
| 87 |
}; |
| 88 |
} else { |
| 89 |
return match m { |
| 90 |
0 => 0.0, /* atan(+...,+INF) */ |
| 91 |
1 => -0.0, /* atan(-...,+INF) */ |
| 92 |
2 => PI, /* atan(+...,-INF) */ |
| 93 |
_ => -PI, /* atan(-...,-INF) */ |
| 94 |
}; |
| 95 |
} |
| 96 |
} |
| 97 |
/* |y/x| > 0x1p64 */ |
| 98 |
if ix.wrapping_add(64 << 20) < iy || iy == 0x7ff00000 { |
| 99 |
return if m & 1 != 0 { -PI / 2.0 } else { PI / 2.0 }; |
| 100 |
} |
| 101 |
|
| 102 |
/* z = atan(|y/x|) without spurious underflow */ |
| 103 |
let z = if (m & 2 != 0) && iy.wrapping_add(64 << 20) < ix { |
| 104 |
/* |y/x| < 0x1p-64, x<0 */ |
| 105 |
0.0 |
| 106 |
} else { |
| 107 |
atan(fabs(y / x)) |
| 108 |
}; |
| 109 |
match m { |
| 110 |
0 => z, /* atan(+,+) */ |
| 111 |
1 => -z, /* atan(-,+) */ |
| 112 |
2 => PI - (z - PI_LO), /* atan(+,-) */ |
| 113 |
_ => (z - PI_LO) - PI, /* atan(-,-) */ |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
#[cfg(test)] |
| 118 |
mod tests { |
| 119 |
use super::*; |
| 120 |
|
| 121 |
#[test] |
| 122 |
#[cfg_attr(x86_no_sse, ignore = "FIXME(i586): possible incorrect rounding")] |
| 123 |
fn sanity_check() { |
| 124 |
assert_eq!(atan2(0.0, 1.0), 0.0); |
| 125 |
assert_eq!(atan2(0.0, -1.0), PI); |
| 126 |
assert_eq!(atan2(-0.0, -1.0), -PI); |
| 127 |
assert_eq!(atan2(3.0, 2.0), atan(3.0 / 2.0)); |
| 128 |
assert_eq!(atan2(2.0, -1.0), atan(2.0 / -1.0) + PI); |
| 129 |
assert_eq!(atan2(-2.0, -1.0), atan(-2.0 / -1.0) - PI); |
| 130 |
} |
| 131 |
} |