2017-06-20 11:01:04 +00:00
|
|
|
// Special Cases:
|
|
|
|
//
|
|
|
|
// - atan(+-0) = +-0
|
|
|
|
// - atan(+-inf) = +-pi/2
|
|
|
|
|
2017-12-24 03:08:53 +00:00
|
|
|
const std = @import("../index.zig");
|
|
|
|
const math = std.math;
|
|
|
|
const assert = std.debug.assert;
|
2017-06-16 08:26:10 +00:00
|
|
|
|
2018-01-25 09:10:11 +00:00
|
|
|
pub fn atan(x: var) @typeOf(x) {
|
2017-06-16 08:26:10 +00:00
|
|
|
const T = @typeOf(x);
|
2017-12-22 05:50:30 +00:00
|
|
|
return switch (T) {
|
2017-12-22 18:14:07 +00:00
|
|
|
f32 => atan32(x),
|
|
|
|
f64 => atan64(x),
|
2017-06-16 08:26:10 +00:00
|
|
|
else => @compileError("atan not implemented for " ++ @typeName(T)),
|
2017-12-22 05:50:30 +00:00
|
|
|
};
|
2017-06-16 08:26:10 +00:00
|
|
|
}
|
|
|
|
|
2018-01-25 09:10:11 +00:00
|
|
|
fn atan32(x_: f32) f32 {
|
2018-11-13 13:08:37 +00:00
|
|
|
const atanhi = []const f32{
|
2017-06-16 08:26:10 +00:00
|
|
|
4.6364760399e-01, // atan(0.5)hi
|
|
|
|
7.8539812565e-01, // atan(1.0)hi
|
|
|
|
9.8279368877e-01, // atan(1.5)hi
|
|
|
|
1.5707962513e+00, // atan(inf)hi
|
|
|
|
};
|
|
|
|
|
2018-11-13 13:08:37 +00:00
|
|
|
const atanlo = []const f32{
|
2017-06-16 08:26:10 +00:00
|
|
|
5.0121582440e-09, // atan(0.5)lo
|
|
|
|
3.7748947079e-08, // atan(1.0)lo
|
|
|
|
3.4473217170e-08, // atan(1.5)lo
|
|
|
|
7.5497894159e-08, // atan(inf)lo
|
|
|
|
};
|
|
|
|
|
2018-11-13 13:08:37 +00:00
|
|
|
const aT = []const f32{
|
2017-06-16 08:26:10 +00:00
|
|
|
3.3333328366e-01,
|
2018-05-29 00:23:55 +00:00
|
|
|
-1.9999158382e-01,
|
2017-06-16 08:26:10 +00:00
|
|
|
1.4253635705e-01,
|
2018-05-29 00:23:55 +00:00
|
|
|
-1.0648017377e-01,
|
2017-06-16 08:26:10 +00:00
|
|
|
6.1687607318e-02,
|
|
|
|
};
|
|
|
|
|
|
|
|
var x = x_;
|
|
|
|
var ix: u32 = @bitCast(u32, x);
|
|
|
|
const sign = ix >> 31;
|
|
|
|
ix &= 0x7FFFFFFF;
|
|
|
|
|
|
|
|
// |x| >= 2^26
|
|
|
|
if (ix >= 0x4C800000) {
|
|
|
|
if (math.isNan(x)) {
|
|
|
|
return x;
|
|
|
|
} else {
|
|
|
|
const z = atanhi[3] + 0x1.0p-120;
|
|
|
|
return if (sign != 0) -z else z;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var id: ?usize = undefined;
|
|
|
|
|
|
|
|
// |x| < 0.4375
|
|
|
|
if (ix < 0x3EE00000) {
|
|
|
|
// |x| < 2^(-12)
|
|
|
|
if (ix < 0x39800000) {
|
|
|
|
if (ix < 0x00800000) {
|
|
|
|
math.forceEval(x * x);
|
|
|
|
}
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
id = null;
|
|
|
|
} else {
|
|
|
|
x = math.fabs(x);
|
|
|
|
// |x| < 1.1875
|
|
|
|
if (ix < 0x3F980000) {
|
|
|
|
// 7/16 <= |x| < 11/16
|
|
|
|
if (ix < 0x3F300000) {
|
|
|
|
id = 0;
|
|
|
|
x = (2.0 * x - 1.0) / (2.0 + x);
|
|
|
|
}
|
|
|
|
// 11/16 <= |x| < 19/16
|
|
|
|
else {
|
|
|
|
id = 1;
|
|
|
|
x = (x - 1.0) / (x + 1.0);
|
|
|
|
}
|
2018-05-29 00:23:55 +00:00
|
|
|
} else {
|
2017-06-16 08:26:10 +00:00
|
|
|
// |x| < 2.4375
|
|
|
|
if (ix < 0x401C0000) {
|
|
|
|
id = 2;
|
|
|
|
x = (x - 1.5) / (1.0 + 1.5 * x);
|
|
|
|
}
|
|
|
|
// 2.4375 <= |x| < 2^26
|
|
|
|
else {
|
|
|
|
id = 3;
|
|
|
|
x = -1.0 / x;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const z = x * x;
|
|
|
|
const w = z * z;
|
|
|
|
const s1 = z * (aT[0] + w * (aT[2] + w * aT[4]));
|
|
|
|
const s2 = w * (aT[1] + w * aT[3]);
|
|
|
|
|
2017-12-22 18:14:07 +00:00
|
|
|
if (id) |id_value| {
|
|
|
|
const zz = atanhi[id_value] - ((x * (s1 + s2) - atanlo[id_value]) - x);
|
2017-12-22 05:50:30 +00:00
|
|
|
return if (sign != 0) -zz else zz;
|
2017-12-22 18:14:07 +00:00
|
|
|
} else {
|
|
|
|
return x - x * (s1 + s2);
|
2017-06-16 08:26:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-25 09:10:11 +00:00
|
|
|
fn atan64(x_: f64) f64 {
|
2018-11-13 13:08:37 +00:00
|
|
|
const atanhi = []const f64{
|
2017-06-16 08:26:10 +00:00
|
|
|
4.63647609000806093515e-01, // atan(0.5)hi
|
|
|
|
7.85398163397448278999e-01, // atan(1.0)hi
|
|
|
|
9.82793723247329054082e-01, // atan(1.5)hi
|
|
|
|
1.57079632679489655800e+00, // atan(inf)hi
|
|
|
|
};
|
|
|
|
|
2018-11-13 13:08:37 +00:00
|
|
|
const atanlo = []const f64{
|
2017-06-16 08:26:10 +00:00
|
|
|
2.26987774529616870924e-17, // atan(0.5)lo
|
|
|
|
3.06161699786838301793e-17, // atan(1.0)lo
|
|
|
|
1.39033110312309984516e-17, // atan(1.5)lo
|
|
|
|
6.12323399573676603587e-17, // atan(inf)lo
|
|
|
|
};
|
|
|
|
|
2018-11-13 13:08:37 +00:00
|
|
|
const aT = []const f64{
|
2017-06-16 08:26:10 +00:00
|
|
|
3.33333333333329318027e-01,
|
2018-05-29 00:23:55 +00:00
|
|
|
-1.99999999998764832476e-01,
|
2017-06-16 08:26:10 +00:00
|
|
|
1.42857142725034663711e-01,
|
2018-05-29 00:23:55 +00:00
|
|
|
-1.11111104054623557880e-01,
|
2017-06-16 08:26:10 +00:00
|
|
|
9.09088713343650656196e-02,
|
2018-05-29 00:23:55 +00:00
|
|
|
-7.69187620504482999495e-02,
|
2017-06-16 08:26:10 +00:00
|
|
|
6.66107313738753120669e-02,
|
2018-05-29 00:23:55 +00:00
|
|
|
-5.83357013379057348645e-02,
|
2017-06-16 08:26:10 +00:00
|
|
|
4.97687799461593236017e-02,
|
2018-05-29 00:23:55 +00:00
|
|
|
-3.65315727442169155270e-02,
|
2017-06-16 08:26:10 +00:00
|
|
|
1.62858201153657823623e-02,
|
|
|
|
};
|
|
|
|
|
|
|
|
var x = x_;
|
|
|
|
var ux = @bitCast(u64, x);
|
2018-06-17 06:57:07 +00:00
|
|
|
var ix = @intCast(u32, ux >> 32);
|
2017-06-16 08:26:10 +00:00
|
|
|
const sign = ix >> 31;
|
|
|
|
ix &= 0x7FFFFFFF;
|
|
|
|
|
|
|
|
// |x| >= 2^66
|
|
|
|
if (ix >= 0x44100000) {
|
|
|
|
if (math.isNan(x)) {
|
|
|
|
return x;
|
|
|
|
} else {
|
|
|
|
const z = atanhi[3] + 0x1.0p-120;
|
|
|
|
return if (sign != 0) -z else z;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var id: ?usize = undefined;
|
|
|
|
|
|
|
|
// |x| < 0.4375
|
|
|
|
if (ix < 0x3DFC0000) {
|
|
|
|
// |x| < 2^(-27)
|
|
|
|
if (ix < 0x3E400000) {
|
|
|
|
if (ix < 0x00100000) {
|
2018-06-17 06:57:07 +00:00
|
|
|
math.forceEval(@floatCast(f32, x));
|
2017-06-16 08:26:10 +00:00
|
|
|
}
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
id = null;
|
|
|
|
} else {
|
|
|
|
x = math.fabs(x);
|
|
|
|
// |x| < 1.1875
|
|
|
|
if (ix < 0x3FF30000) {
|
|
|
|
// 7/16 <= |x| < 11/16
|
|
|
|
if (ix < 0x3FE60000) {
|
|
|
|
id = 0;
|
|
|
|
x = (2.0 * x - 1.0) / (2.0 + x);
|
|
|
|
}
|
|
|
|
// 11/16 <= |x| < 19/16
|
|
|
|
else {
|
|
|
|
id = 1;
|
|
|
|
x = (x - 1.0) / (x + 1.0);
|
|
|
|
}
|
2018-05-29 00:23:55 +00:00
|
|
|
} else {
|
2017-06-16 08:26:10 +00:00
|
|
|
// |x| < 2.4375
|
|
|
|
if (ix < 0x40038000) {
|
|
|
|
id = 2;
|
|
|
|
x = (x - 1.5) / (1.0 + 1.5 * x);
|
|
|
|
}
|
|
|
|
// 2.4375 <= |x| < 2^66
|
|
|
|
else {
|
|
|
|
id = 3;
|
|
|
|
x = -1.0 / x;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const z = x * x;
|
|
|
|
const w = z * z;
|
|
|
|
const s1 = z * (aT[0] + w * (aT[2] + w * (aT[4] + w * (aT[6] + w * (aT[8] + w * aT[10])))));
|
|
|
|
const s2 = w * (aT[1] + w * (aT[3] + w * (aT[5] + w * (aT[7] + w * aT[9]))));
|
|
|
|
|
2017-12-22 18:14:07 +00:00
|
|
|
if (id) |id_value| {
|
|
|
|
const zz = atanhi[id_value] - ((x * (s1 + s2) - atanlo[id_value]) - x);
|
2017-12-22 05:50:30 +00:00
|
|
|
return if (sign != 0) -zz else zz;
|
2017-12-22 18:14:07 +00:00
|
|
|
} else {
|
|
|
|
return x - x * (s1 + s2);
|
2017-06-16 08:26:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-19 18:36:33 +00:00
|
|
|
test "math.atan" {
|
2017-12-22 18:14:07 +00:00
|
|
|
assert(@bitCast(u32, atan(f32(0.2))) == @bitCast(u32, atan32(0.2)));
|
2017-08-28 08:28:42 +00:00
|
|
|
assert(atan(f64(0.2)) == atan64(0.2));
|
2017-06-16 08:26:10 +00:00
|
|
|
}
|
|
|
|
|
2017-06-19 18:36:33 +00:00
|
|
|
test "math.atan32" {
|
2017-06-16 08:26:10 +00:00
|
|
|
const epsilon = 0.000001;
|
|
|
|
|
|
|
|
assert(math.approxEq(f32, atan32(0.2), 0.197396, epsilon));
|
|
|
|
assert(math.approxEq(f32, atan32(-0.2), -0.197396, epsilon));
|
|
|
|
assert(math.approxEq(f32, atan32(0.3434), 0.330783, epsilon));
|
|
|
|
assert(math.approxEq(f32, atan32(0.8923), 0.728545, epsilon));
|
|
|
|
assert(math.approxEq(f32, atan32(1.5), 0.982794, epsilon));
|
|
|
|
}
|
|
|
|
|
2017-06-19 18:36:33 +00:00
|
|
|
test "math.atan64" {
|
2017-06-16 08:26:10 +00:00
|
|
|
const epsilon = 0.000001;
|
|
|
|
|
|
|
|
assert(math.approxEq(f64, atan64(0.2), 0.197396, epsilon));
|
|
|
|
assert(math.approxEq(f64, atan64(-0.2), -0.197396, epsilon));
|
|
|
|
assert(math.approxEq(f64, atan64(0.3434), 0.330783, epsilon));
|
|
|
|
assert(math.approxEq(f64, atan64(0.8923), 0.728545, epsilon));
|
|
|
|
assert(math.approxEq(f64, atan64(1.5), 0.982794, epsilon));
|
|
|
|
}
|
2017-06-20 11:01:04 +00:00
|
|
|
|
|
|
|
test "math.atan32.special" {
|
|
|
|
const epsilon = 0.000001;
|
|
|
|
|
|
|
|
assert(atan32(0.0) == 0.0);
|
|
|
|
assert(atan32(-0.0) == -0.0);
|
2017-08-20 09:43:17 +00:00
|
|
|
assert(math.approxEq(f32, atan32(math.inf(f32)), math.pi / 2.0, epsilon));
|
|
|
|
assert(math.approxEq(f32, atan32(-math.inf(f32)), -math.pi / 2.0, epsilon));
|
2017-06-20 11:01:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
test "math.atan64.special" {
|
|
|
|
const epsilon = 0.000001;
|
|
|
|
|
|
|
|
assert(atan64(0.0) == 0.0);
|
|
|
|
assert(atan64(-0.0) == -0.0);
|
2017-08-20 09:43:17 +00:00
|
|
|
assert(math.approxEq(f64, atan64(math.inf(f64)), math.pi / 2.0, epsilon));
|
|
|
|
assert(math.approxEq(f64, atan64(-math.inf(f64)), -math.pi / 2.0, epsilon));
|
2017-06-20 11:01:04 +00:00
|
|
|
}
|