2017-06-20 04:01:04 -07:00
|
|
|
// Special Cases:
|
|
|
|
//
|
|
|
|
// atan2(y, nan) = nan
|
|
|
|
// atan2(nan, x) = nan
|
|
|
|
// atan2(+0, x>=0) = +0
|
|
|
|
// atan2(-0, x>=0) = -0
|
|
|
|
// atan2(+0, x<=-0) = +pi
|
|
|
|
// atan2(-0, x<=-0) = -pi
|
|
|
|
// atan2(y>0, 0) = +pi/2
|
|
|
|
// atan2(y<0, 0) = -pi/2
|
|
|
|
// atan2(+inf, +inf) = +pi/4
|
|
|
|
// atan2(-inf, +inf) = -pi/4
|
|
|
|
// atan2(+inf, -inf) = 3pi/4
|
|
|
|
// atan2(-inf, -inf) = -3pi/4
|
|
|
|
// atan2(y, +inf) = 0
|
|
|
|
// atan2(y>0, -inf) = +pi
|
|
|
|
// atan2(y<0, -inf) = -pi
|
|
|
|
// atan2(+inf, x) = +pi/2
|
|
|
|
// atan2(-inf, x) = -pi/2
|
|
|
|
|
2017-12-23 19:08:53 -08:00
|
|
|
const std = @import("../index.zig");
|
|
|
|
const math = std.math;
|
|
|
|
const assert = std.debug.assert;
|
2017-06-16 01:26:10 -07:00
|
|
|
|
2018-03-21 23:18:51 -07:00
|
|
|
pub fn atan2(comptime T: type, x: T, y: T) T {
|
2017-12-21 21:50:30 -08:00
|
|
|
return switch (T) {
|
2017-12-22 10:14:07 -08:00
|
|
|
f32 => atan2_32(x, y),
|
|
|
|
f64 => atan2_64(x, y),
|
2017-06-16 01:26:10 -07:00
|
|
|
else => @compileError("atan2 not implemented for " ++ @typeName(T)),
|
2017-12-21 21:50:30 -08:00
|
|
|
};
|
2017-06-16 01:26:10 -07:00
|
|
|
}
|
|
|
|
|
2018-01-25 01:10:11 -08:00
|
|
|
fn atan2_32(y: f32, x: f32) f32 {
|
2018-05-09 21:29:49 -07:00
|
|
|
const pi: f32 = 3.1415927410e+00;
|
2017-06-16 01:26:10 -07:00
|
|
|
const pi_lo: f32 = -8.7422776573e-08;
|
|
|
|
|
|
|
|
if (math.isNan(x) or math.isNan(y)) {
|
|
|
|
return x + y;
|
|
|
|
}
|
|
|
|
|
|
|
|
var ix = @bitCast(u32, x);
|
|
|
|
var iy = @bitCast(u32, y);
|
|
|
|
|
|
|
|
// x = 1.0
|
|
|
|
if (ix == 0x3F800000) {
|
|
|
|
return math.atan(y);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 2 * sign(x) + sign(y)
|
|
|
|
const m = ((iy >> 31) & 1) | ((ix >> 30) & 2);
|
|
|
|
ix &= 0x7FFFFFFF;
|
|
|
|
iy &= 0x7FFFFFFF;
|
|
|
|
|
|
|
|
if (iy == 0) {
|
|
|
|
switch (m) {
|
2018-05-28 17:23:55 -07:00
|
|
|
0, 1 => return y, // atan(+-0, +...)
|
2018-05-09 21:29:49 -07:00
|
|
|
2 => return pi, // atan(+0, -...)
|
|
|
|
3 => return -pi, // atan(-0, -...)
|
2017-06-16 01:26:10 -07:00
|
|
|
else => unreachable,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ix == 0) {
|
|
|
|
if (m & 1 != 0) {
|
|
|
|
return -pi / 2;
|
|
|
|
} else {
|
|
|
|
return pi / 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ix == 0x7F800000) {
|
|
|
|
if (iy == 0x7F800000) {
|
|
|
|
switch (m) {
|
2018-05-09 21:29:49 -07:00
|
|
|
0 => return pi / 4, // atan(+inf, +inf)
|
|
|
|
1 => return -pi / 4, // atan(-inf, +inf)
|
|
|
|
2 => return 3 * pi / 4, // atan(+inf, -inf)
|
|
|
|
3 => return -3 * pi / 4, // atan(-inf, -inf)
|
2017-06-16 01:26:10 -07:00
|
|
|
else => unreachable,
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
switch (m) {
|
2018-05-09 21:29:49 -07:00
|
|
|
0 => return 0.0, // atan(+..., +inf)
|
|
|
|
1 => return -0.0, // atan(-..., +inf)
|
|
|
|
2 => return pi, // atan(+..., -inf)
|
|
|
|
3 => return -pi, // atan(-...f, -inf)
|
2017-06-16 01:26:10 -07:00
|
|
|
else => unreachable,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// |y / x| > 0x1p26
|
|
|
|
if (ix + (26 << 23) < iy or iy == 0x7F800000) {
|
|
|
|
if (m & 1 != 0) {
|
|
|
|
return -pi / 2;
|
|
|
|
} else {
|
|
|
|
return pi / 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// z = atan(|y / x|) with correct underflow
|
2017-12-21 21:50:30 -08:00
|
|
|
var z = z: {
|
2017-06-16 01:26:10 -07:00
|
|
|
if ((m & 2) != 0 and iy + (26 << 23) < ix) {
|
2017-12-21 21:50:30 -08:00
|
|
|
break :z 0.0;
|
2017-06-16 01:26:10 -07:00
|
|
|
} else {
|
2017-12-21 21:50:30 -08:00
|
|
|
break :z math.atan(math.fabs(y / x));
|
2017-06-16 01:26:10 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
switch (m) {
|
2018-05-09 21:29:49 -07:00
|
|
|
0 => return z, // atan(+, +)
|
|
|
|
1 => return -z, // atan(-, +)
|
|
|
|
2 => return pi - (z - pi_lo), // atan(+, -)
|
|
|
|
3 => return (z - pi_lo) - pi, // atan(-, -)
|
2017-06-16 01:26:10 -07:00
|
|
|
else => unreachable,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-25 01:10:11 -08:00
|
|
|
fn atan2_64(y: f64, x: f64) f64 {
|
2018-05-09 21:29:49 -07:00
|
|
|
const pi: f64 = 3.1415926535897931160E+00;
|
2017-06-16 01:26:10 -07:00
|
|
|
const pi_lo: f64 = 1.2246467991473531772E-16;
|
|
|
|
|
|
|
|
if (math.isNan(x) or math.isNan(y)) {
|
|
|
|
return x + y;
|
|
|
|
}
|
|
|
|
|
|
|
|
var ux = @bitCast(u64, x);
|
2018-06-16 23:57:07 -07:00
|
|
|
var ix = @intCast(u32, ux >> 32);
|
|
|
|
var lx = @intCast(u32, ux & 0xFFFFFFFF);
|
2017-06-16 01:26:10 -07:00
|
|
|
|
|
|
|
var uy = @bitCast(u64, y);
|
2018-06-16 23:57:07 -07:00
|
|
|
var iy = @intCast(u32, uy >> 32);
|
|
|
|
var ly = @intCast(u32, uy & 0xFFFFFFFF);
|
2017-06-16 01:26:10 -07:00
|
|
|
|
|
|
|
// x = 1.0
|
|
|
|
if ((ix -% 0x3FF00000) | lx == 0) {
|
|
|
|
return math.atan(y);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 2 * sign(x) + sign(y)
|
|
|
|
const m = ((iy >> 31) & 1) | ((ix >> 30) & 2);
|
|
|
|
ix &= 0x7FFFFFFF;
|
|
|
|
iy &= 0x7FFFFFFF;
|
|
|
|
|
|
|
|
if (iy | ly == 0) {
|
|
|
|
switch (m) {
|
2018-05-28 17:23:55 -07:00
|
|
|
0, 1 => return y, // atan(+-0, +...)
|
2018-05-09 21:29:49 -07:00
|
|
|
2 => return pi, // atan(+0, -...)
|
|
|
|
3 => return -pi, // atan(-0, -...)
|
2017-06-16 01:26:10 -07:00
|
|
|
else => unreachable,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ix | lx == 0) {
|
|
|
|
if (m & 1 != 0) {
|
|
|
|
return -pi / 2;
|
|
|
|
} else {
|
|
|
|
return pi / 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ix == 0x7FF00000) {
|
|
|
|
if (iy == 0x7FF00000) {
|
|
|
|
switch (m) {
|
2018-05-09 21:29:49 -07:00
|
|
|
0 => return pi / 4, // atan(+inf, +inf)
|
|
|
|
1 => return -pi / 4, // atan(-inf, +inf)
|
|
|
|
2 => return 3 * pi / 4, // atan(+inf, -inf)
|
|
|
|
3 => return -3 * pi / 4, // atan(-inf, -inf)
|
2017-06-16 01:26:10 -07:00
|
|
|
else => unreachable,
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
switch (m) {
|
2018-05-09 21:29:49 -07:00
|
|
|
0 => return 0.0, // atan(+..., +inf)
|
|
|
|
1 => return -0.0, // atan(-..., +inf)
|
|
|
|
2 => return pi, // atan(+..., -inf)
|
|
|
|
3 => return -pi, // atan(-...f, -inf)
|
2017-06-16 01:26:10 -07:00
|
|
|
else => unreachable,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// |y / x| > 0x1p64
|
|
|
|
if (ix +% (64 << 20) < iy or iy == 0x7FF00000) {
|
|
|
|
if (m & 1 != 0) {
|
|
|
|
return -pi / 2;
|
|
|
|
} else {
|
|
|
|
return pi / 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// z = atan(|y / x|) with correct underflow
|
2017-12-21 21:50:30 -08:00
|
|
|
var z = z: {
|
2017-06-16 01:26:10 -07:00
|
|
|
if ((m & 2) != 0 and iy +% (64 << 20) < ix) {
|
2017-12-21 21:50:30 -08:00
|
|
|
break :z 0.0;
|
2017-06-16 01:26:10 -07:00
|
|
|
} else {
|
2017-12-21 21:50:30 -08:00
|
|
|
break :z math.atan(math.fabs(y / x));
|
2017-06-16 01:26:10 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
switch (m) {
|
2018-05-09 21:29:49 -07:00
|
|
|
0 => return z, // atan(+, +)
|
|
|
|
1 => return -z, // atan(-, +)
|
|
|
|
2 => return pi - (z - pi_lo), // atan(+, -)
|
|
|
|
3 => return (z - pi_lo) - pi, // atan(-, -)
|
2017-06-16 01:26:10 -07:00
|
|
|
else => unreachable,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-19 11:36:33 -07:00
|
|
|
test "math.atan2" {
|
2017-08-28 01:28:42 -07:00
|
|
|
assert(atan2(f32, 0.2, 0.21) == atan2_32(0.2, 0.21));
|
|
|
|
assert(atan2(f64, 0.2, 0.21) == atan2_64(0.2, 0.21));
|
2017-06-16 01:26:10 -07:00
|
|
|
}
|
|
|
|
|
2017-06-19 11:36:33 -07:00
|
|
|
test "math.atan2_32" {
|
2017-06-16 01:26:10 -07:00
|
|
|
const epsilon = 0.000001;
|
|
|
|
|
2017-06-19 11:36:33 -07:00
|
|
|
assert(math.approxEq(f32, atan2_32(0.0, 0.0), 0.0, epsilon));
|
|
|
|
assert(math.approxEq(f32, atan2_32(0.2, 0.2), 0.785398, epsilon));
|
|
|
|
assert(math.approxEq(f32, atan2_32(-0.2, 0.2), -0.785398, epsilon));
|
|
|
|
assert(math.approxEq(f32, atan2_32(0.2, -0.2), 2.356194, epsilon));
|
|
|
|
assert(math.approxEq(f32, atan2_32(-0.2, -0.2), -2.356194, epsilon));
|
|
|
|
assert(math.approxEq(f32, atan2_32(0.34, -0.4), 2.437099, epsilon));
|
|
|
|
assert(math.approxEq(f32, atan2_32(0.34, 1.243), 0.267001, epsilon));
|
2017-06-16 01:26:10 -07:00
|
|
|
}
|
|
|
|
|
2017-06-19 11:36:33 -07:00
|
|
|
test "math.atan2_64" {
|
2017-06-16 01:26:10 -07:00
|
|
|
const epsilon = 0.000001;
|
|
|
|
|
2017-06-19 11:36:33 -07:00
|
|
|
assert(math.approxEq(f64, atan2_64(0.0, 0.0), 0.0, epsilon));
|
|
|
|
assert(math.approxEq(f64, atan2_64(0.2, 0.2), 0.785398, epsilon));
|
|
|
|
assert(math.approxEq(f64, atan2_64(-0.2, 0.2), -0.785398, epsilon));
|
|
|
|
assert(math.approxEq(f64, atan2_64(0.2, -0.2), 2.356194, epsilon));
|
|
|
|
assert(math.approxEq(f64, atan2_64(-0.2, -0.2), -2.356194, epsilon));
|
|
|
|
assert(math.approxEq(f64, atan2_64(0.34, -0.4), 2.437099, epsilon));
|
|
|
|
assert(math.approxEq(f64, atan2_64(0.34, 1.243), 0.267001, epsilon));
|
2017-06-16 01:26:10 -07:00
|
|
|
}
|
2017-06-20 04:01:04 -07:00
|
|
|
|
|
|
|
test "math.atan2_32.special" {
|
|
|
|
const epsilon = 0.000001;
|
|
|
|
|
|
|
|
assert(math.isNan(atan2_32(1.0, math.nan(f32))));
|
|
|
|
assert(math.isNan(atan2_32(math.nan(f32), 1.0)));
|
|
|
|
assert(atan2_32(0.0, 5.0) == 0.0);
|
|
|
|
assert(atan2_32(-0.0, 5.0) == -0.0);
|
|
|
|
assert(math.approxEq(f32, atan2_32(0.0, -5.0), math.pi, epsilon));
|
2017-09-13 22:44:22 -07:00
|
|
|
//assert(math.approxEq(f32, atan2_32(-0.0, -5.0), -math.pi, epsilon)); TODO support negative zero?
|
2017-08-20 02:43:17 -07:00
|
|
|
assert(math.approxEq(f32, atan2_32(1.0, 0.0), math.pi / 2.0, epsilon));
|
|
|
|
assert(math.approxEq(f32, atan2_32(1.0, -0.0), math.pi / 2.0, epsilon));
|
|
|
|
assert(math.approxEq(f32, atan2_32(-1.0, 0.0), -math.pi / 2.0, epsilon));
|
|
|
|
assert(math.approxEq(f32, atan2_32(-1.0, -0.0), -math.pi / 2.0, epsilon));
|
|
|
|
assert(math.approxEq(f32, atan2_32(math.inf(f32), math.inf(f32)), math.pi / 4.0, epsilon));
|
|
|
|
assert(math.approxEq(f32, atan2_32(-math.inf(f32), math.inf(f32)), -math.pi / 4.0, epsilon));
|
|
|
|
assert(math.approxEq(f32, atan2_32(math.inf(f32), -math.inf(f32)), 3.0 * math.pi / 4.0, epsilon));
|
|
|
|
assert(math.approxEq(f32, atan2_32(-math.inf(f32), -math.inf(f32)), -3.0 * math.pi / 4.0, epsilon));
|
2017-06-20 04:01:04 -07:00
|
|
|
assert(atan2_32(1.0, math.inf(f32)) == 0.0);
|
|
|
|
assert(math.approxEq(f32, atan2_32(1.0, -math.inf(f32)), math.pi, epsilon));
|
|
|
|
assert(math.approxEq(f32, atan2_32(-1.0, -math.inf(f32)), -math.pi, epsilon));
|
2017-08-20 02:43:17 -07:00
|
|
|
assert(math.approxEq(f32, atan2_32(math.inf(f32), 1.0), math.pi / 2.0, epsilon));
|
|
|
|
assert(math.approxEq(f32, atan2_32(-math.inf(f32), 1.0), -math.pi / 2.0, epsilon));
|
2017-06-20 04:01:04 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
test "math.atan2_64.special" {
|
|
|
|
const epsilon = 0.000001;
|
|
|
|
|
|
|
|
assert(math.isNan(atan2_64(1.0, math.nan(f64))));
|
|
|
|
assert(math.isNan(atan2_64(math.nan(f64), 1.0)));
|
|
|
|
assert(atan2_64(0.0, 5.0) == 0.0);
|
|
|
|
assert(atan2_64(-0.0, 5.0) == -0.0);
|
|
|
|
assert(math.approxEq(f64, atan2_64(0.0, -5.0), math.pi, epsilon));
|
2017-09-13 22:44:22 -07:00
|
|
|
//assert(math.approxEq(f64, atan2_64(-0.0, -5.0), -math.pi, epsilon)); TODO support negative zero?
|
2017-08-20 02:43:17 -07:00
|
|
|
assert(math.approxEq(f64, atan2_64(1.0, 0.0), math.pi / 2.0, epsilon));
|
|
|
|
assert(math.approxEq(f64, atan2_64(1.0, -0.0), math.pi / 2.0, epsilon));
|
|
|
|
assert(math.approxEq(f64, atan2_64(-1.0, 0.0), -math.pi / 2.0, epsilon));
|
|
|
|
assert(math.approxEq(f64, atan2_64(-1.0, -0.0), -math.pi / 2.0, epsilon));
|
|
|
|
assert(math.approxEq(f64, atan2_64(math.inf(f64), math.inf(f64)), math.pi / 4.0, epsilon));
|
|
|
|
assert(math.approxEq(f64, atan2_64(-math.inf(f64), math.inf(f64)), -math.pi / 4.0, epsilon));
|
|
|
|
assert(math.approxEq(f64, atan2_64(math.inf(f64), -math.inf(f64)), 3.0 * math.pi / 4.0, epsilon));
|
|
|
|
assert(math.approxEq(f64, atan2_64(-math.inf(f64), -math.inf(f64)), -3.0 * math.pi / 4.0, epsilon));
|
2017-06-20 04:01:04 -07:00
|
|
|
assert(atan2_64(1.0, math.inf(f64)) == 0.0);
|
|
|
|
assert(math.approxEq(f64, atan2_64(1.0, -math.inf(f64)), math.pi, epsilon));
|
|
|
|
assert(math.approxEq(f64, atan2_64(-1.0, -math.inf(f64)), -math.pi, epsilon));
|
2017-08-20 02:43:17 -07:00
|
|
|
assert(math.approxEq(f64, atan2_64(math.inf(f64), 1.0), math.pi / 2.0, epsilon));
|
|
|
|
assert(math.approxEq(f64, atan2_64(-math.inf(f64), 1.0), -math.pi / 2.0, epsilon));
|
2017-06-20 04:01:04 -07:00
|
|
|
}
|