2019-04-30 23:12:16 -07:00
|
|
|
// Ported from go, which is licensed under a BSD-3 license.
|
|
|
|
// https://golang.org/LICENSE
|
2017-06-20 04:01:04 -07:00
|
|
|
//
|
2019-04-30 23:12:16 -07:00
|
|
|
// https://golang.org/src/math/tan.go
|
2017-06-20 04:01:04 -07:00
|
|
|
|
2017-10-14 23:04:21 -07:00
|
|
|
const builtin = @import("builtin");
|
2019-03-02 13:46:04 -08:00
|
|
|
const std = @import("../std.zig");
|
2017-12-23 19:08:53 -08:00
|
|
|
const math = std.math;
|
2019-02-08 15:18:47 -08:00
|
|
|
const expect = std.testing.expect;
|
2017-06-16 01:26:10 -07:00
|
|
|
|
2019-04-30 23:12:16 -07:00
|
|
|
/// Returns the tangent of the radian value x.
|
|
|
|
///
|
|
|
|
/// Special Cases:
|
|
|
|
/// - tan(+-0) = +-0
|
|
|
|
/// - tan(+-inf) = nan
|
|
|
|
/// - tan(nan) = nan
|
2019-12-09 12:56:19 -08:00
|
|
|
pub fn tan(x: var) @TypeOf(x) {
|
|
|
|
const T = @TypeOf(x);
|
2017-12-21 21:50:30 -08:00
|
|
|
return switch (T) {
|
2019-04-30 23:12:16 -07:00
|
|
|
f32 => tan_(f32, x),
|
|
|
|
f64 => tan_(f64, x),
|
2017-06-16 01:26:10 -07:00
|
|
|
else => @compileError("tan not implemented for " ++ @typeName(T)),
|
2017-12-21 21:50:30 -08:00
|
|
|
};
|
2017-06-16 01:26:10 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
const Tp0 = -1.30936939181383777646E4;
|
2018-05-09 21:29:49 -07:00
|
|
|
const Tp1 = 1.15351664838587416140E6;
|
2017-06-16 01:26:10 -07:00
|
|
|
const Tp2 = -1.79565251976484877988E7;
|
|
|
|
|
2018-05-09 21:29:49 -07:00
|
|
|
const Tq1 = 1.36812963470692954678E4;
|
2017-06-16 01:26:10 -07:00
|
|
|
const Tq2 = -1.32089234440210967447E6;
|
2018-05-09 21:29:49 -07:00
|
|
|
const Tq3 = 2.50083801823357915839E7;
|
2017-06-16 01:26:10 -07:00
|
|
|
const Tq4 = -5.38695755929454629881E7;
|
|
|
|
|
2019-04-30 23:12:16 -07:00
|
|
|
const pi4a = 7.85398125648498535156e-1;
|
|
|
|
const pi4b = 3.77489470793079817668E-8;
|
|
|
|
const pi4c = 2.69515142907905952645E-15;
|
|
|
|
const m4pi = 1.273239544735162542821171882678754627704620361328125;
|
2017-06-16 01:26:10 -07:00
|
|
|
|
2019-04-30 23:12:16 -07:00
|
|
|
fn tan_(comptime T: type, x_: T) T {
|
2020-04-28 18:10:09 -07:00
|
|
|
const I = std.meta.Int(true, T.bit_count);
|
2017-06-16 01:26:10 -07:00
|
|
|
|
|
|
|
var x = x_;
|
|
|
|
if (x == 0 or math.isNan(x)) {
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
if (math.isInf(x)) {
|
2019-04-30 23:12:16 -07:00
|
|
|
return math.nan(T);
|
2017-06-16 01:26:10 -07:00
|
|
|
}
|
|
|
|
|
2019-04-30 23:12:16 -07:00
|
|
|
var sign = x < 0;
|
|
|
|
x = math.fabs(x);
|
2017-06-16 01:26:10 -07:00
|
|
|
|
|
|
|
var y = math.floor(x * m4pi);
|
2019-04-30 23:12:16 -07:00
|
|
|
var j = @floatToInt(I, y);
|
2017-06-16 01:26:10 -07:00
|
|
|
|
|
|
|
if (j & 1 == 1) {
|
|
|
|
j += 1;
|
|
|
|
y += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
const z = ((x - y * pi4a) - y * pi4b) - y * pi4c;
|
|
|
|
const w = z * z;
|
|
|
|
|
2019-04-30 23:12:16 -07:00
|
|
|
var r = if (w > 1e-14)
|
|
|
|
z + z * (w * ((Tp0 * w + Tp1) * w + Tp2) / ((((w + Tq1) * w + Tq2) * w + Tq3) * w + Tq4))
|
|
|
|
else
|
|
|
|
z;
|
2017-06-16 01:26:10 -07:00
|
|
|
|
|
|
|
if (j & 2 == 2) {
|
|
|
|
r = -1 / r;
|
|
|
|
}
|
|
|
|
|
2019-04-30 23:12:16 -07:00
|
|
|
return if (sign) -r else r;
|
2017-06-16 01:26:10 -07:00
|
|
|
}
|
|
|
|
|
2017-06-19 11:36:33 -07:00
|
|
|
test "math.tan" {
|
2019-11-06 20:25:57 -08:00
|
|
|
expect(tan(@as(f32, 0.0)) == tan_(f32, 0.0));
|
|
|
|
expect(tan(@as(f64, 0.0)) == tan_(f64, 0.0));
|
2017-06-16 01:26:10 -07:00
|
|
|
}
|
|
|
|
|
2017-06-19 11:36:33 -07:00
|
|
|
test "math.tan32" {
|
2017-06-16 01:26:10 -07:00
|
|
|
const epsilon = 0.000001;
|
|
|
|
|
2019-04-30 23:12:16 -07:00
|
|
|
expect(math.approxEq(f32, tan_(f32, 0.0), 0.0, epsilon));
|
|
|
|
expect(math.approxEq(f32, tan_(f32, 0.2), 0.202710, epsilon));
|
|
|
|
expect(math.approxEq(f32, tan_(f32, 0.8923), 1.240422, epsilon));
|
|
|
|
expect(math.approxEq(f32, tan_(f32, 1.5), 14.101420, epsilon));
|
|
|
|
expect(math.approxEq(f32, tan_(f32, 37.45), -0.254397, epsilon));
|
|
|
|
expect(math.approxEq(f32, tan_(f32, 89.123), 2.285852, epsilon));
|
2017-06-16 01:26:10 -07:00
|
|
|
}
|
|
|
|
|
2017-06-19 11:36:33 -07:00
|
|
|
test "math.tan64" {
|
2017-06-16 01:26:10 -07:00
|
|
|
const epsilon = 0.000001;
|
|
|
|
|
2019-04-30 23:12:16 -07:00
|
|
|
expect(math.approxEq(f64, tan_(f64, 0.0), 0.0, epsilon));
|
|
|
|
expect(math.approxEq(f64, tan_(f64, 0.2), 0.202710, epsilon));
|
|
|
|
expect(math.approxEq(f64, tan_(f64, 0.8923), 1.240422, epsilon));
|
|
|
|
expect(math.approxEq(f64, tan_(f64, 1.5), 14.101420, epsilon));
|
|
|
|
expect(math.approxEq(f64, tan_(f64, 37.45), -0.254397, epsilon));
|
|
|
|
expect(math.approxEq(f64, tan_(f64, 89.123), 2.2858376, epsilon));
|
2017-06-16 01:26:10 -07:00
|
|
|
}
|
2017-06-20 04:01:04 -07:00
|
|
|
|
|
|
|
test "math.tan32.special" {
|
2019-04-30 23:12:16 -07:00
|
|
|
expect(tan_(f32, 0.0) == 0.0);
|
|
|
|
expect(tan_(f32, -0.0) == -0.0);
|
|
|
|
expect(math.isNan(tan_(f32, math.inf(f32))));
|
|
|
|
expect(math.isNan(tan_(f32, -math.inf(f32))));
|
|
|
|
expect(math.isNan(tan_(f32, math.nan(f32))));
|
2017-06-20 04:01:04 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
test "math.tan64.special" {
|
2019-04-30 23:12:16 -07:00
|
|
|
expect(tan_(f64, 0.0) == 0.0);
|
|
|
|
expect(tan_(f64, -0.0) == -0.0);
|
|
|
|
expect(math.isNan(tan_(f64, math.inf(f64))));
|
|
|
|
expect(math.isNan(tan_(f64, -math.inf(f64))));
|
|
|
|
expect(math.isNan(tan_(f64, math.nan(f64))));
|
2017-06-20 04:01:04 -07:00
|
|
|
}
|