aboutsummaryrefslogtreecommitdiff
path: root/lib/std/math/tan.zig
blob: fd5950df7c25c917cad3691bdda557c084911843 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// Ported from musl, which is licensed under the MIT license:
// https://git.musl-libc.org/cgit/musl/tree/COPYRIGHT
//
// https://git.musl-libc.org/cgit/musl/tree/src/math/tanf.c
// https://git.musl-libc.org/cgit/musl/tree/src/math/tan.c
// https://golang.org/src/math/tan.go

const std = @import("../std.zig");
const math = std.math;
const expect = std.testing.expect;

const kernel = @import("__trig.zig");
const __rem_pio2 = @import("__rem_pio2.zig").__rem_pio2;
const __rem_pio2f = @import("__rem_pio2f.zig").__rem_pio2f;

/// Returns the tangent of the radian value x.
///
/// Special Cases:
///  - tan(+-0)   = +-0
///  - tan(+-inf) = nan
///  - tan(nan)   = nan
pub fn tan(x: anytype) @TypeOf(x) {
    const T = @TypeOf(x);
    return switch (T) {
        f32 => tan32(x),
        f64 => tan64(x),
        else => @compileError("tan not implemented for " ++ @typeName(T)),
    };
}

fn tan32(x: f32) f32 {
    // Small multiples of pi/2 rounded to double precision.
    const t1pio2: f64 = 1.0 * math.pi / 2.0; // 0x3FF921FB, 0x54442D18
    const t2pio2: f64 = 2.0 * math.pi / 2.0; // 0x400921FB, 0x54442D18
    const t3pio2: f64 = 3.0 * math.pi / 2.0; // 0x4012D97C, 0x7F3321D2
    const t4pio2: f64 = 4.0 * math.pi / 2.0; // 0x401921FB, 0x54442D18

    var ix = @bitCast(u32, x);
    const sign = ix >> 31 != 0;
    ix &= 0x7fffffff;

    if (ix <= 0x3f490fda) { // |x| ~<= pi/4
        if (ix < 0x39800000) { // |x| < 2**-12
            // raise inexact if x!=0 and underflow if subnormal
            math.doNotOptimizeAway(if (ix < 0x00800000) x / 0x1p120 else x + 0x1p120);
            return x;
        }
        return kernel.__tandf(x, false);
    }
    if (ix <= 0x407b53d1) { // |x| ~<= 5*pi/4
        if (ix <= 0x4016cbe3) { // |x| ~<= 3pi/4
            return kernel.__tandf((if (sign) x + t1pio2 else x - t1pio2), true);
        } else {
            return kernel.__tandf((if (sign) x + t2pio2 else x - t2pio2), false);
        }
    }
    if (ix <= 0x40e231d5) { // |x| ~<= 9*pi/4
        if (ix <= 0x40afeddf) { // |x| ~<= 7*pi/4
            return kernel.__tandf((if (sign) x + t3pio2 else x - t3pio2), true);
        } else {
            return kernel.__tandf((if (sign) x + t4pio2 else x - t4pio2), false);
        }
    }

    // tan(Inf or NaN) is NaN
    if (ix >= 0x7f800000) {
        return x - x;
    }

    var y: f64 = undefined;
    const n = __rem_pio2f(x, &y);
    return kernel.__tandf(y, n & 1 != 0);
}

fn tan64(x: f64) f64 {
    var ix = @bitCast(u64, x) >> 32;
    ix &= 0x7fffffff;

    // |x| ~< pi/4
    if (ix <= 0x3fe921fb) {
        if (ix < 0x3e400000) { // |x| < 2**-27
            // raise inexact if x!=0 and underflow if subnormal
            math.doNotOptimizeAway(if (ix < 0x00100000) x / 0x1p120 else x + 0x1p120);
            return x;
        }
        return kernel.__tan(x, 0.0, false);
    }

    // tan(Inf or NaN) is NaN
    if (ix >= 0x7ff00000) {
        return x - x;
    }

    var y: [2]f64 = undefined;
    const n = __rem_pio2(x, &y);
    return kernel.__tan(y[0], y[1], n & 1 != 0);
}

test "math.tan" {
    try expect(tan(@as(f32, 0.0)) == tan32(0.0));
    try expect(tan(@as(f64, 0.0)) == tan64(0.0));
}

test "math.tan32" {
    const epsilon = 0.00001;

    try expect(math.approxEqAbs(f32, tan32(0.0), 0.0, epsilon));
    try expect(math.approxEqAbs(f32, tan32(0.2), 0.202710, epsilon));
    try expect(math.approxEqAbs(f32, tan32(0.8923), 1.240422, epsilon));
    try expect(math.approxEqAbs(f32, tan32(1.5), 14.101420, epsilon));
    try expect(math.approxEqAbs(f32, tan32(37.45), -0.254397, epsilon));
    try expect(math.approxEqAbs(f32, tan32(89.123), 2.285852, epsilon));
}

test "math.tan64" {
    const epsilon = 0.000001;

    try expect(math.approxEqAbs(f64, tan64(0.0), 0.0, epsilon));
    try expect(math.approxEqAbs(f64, tan64(0.2), 0.202710, epsilon));
    try expect(math.approxEqAbs(f64, tan64(0.8923), 1.240422, epsilon));
    try expect(math.approxEqAbs(f64, tan64(1.5), 14.101420, epsilon));
    try expect(math.approxEqAbs(f64, tan64(37.45), -0.254397, epsilon));
    try expect(math.approxEqAbs(f64, tan64(89.123), 2.2858376, epsilon));
}

test "math.tan32.special" {
    try expect(tan32(0.0) == 0.0);
    try expect(tan32(-0.0) == -0.0);
    try expect(math.isNan(tan32(math.inf(f32))));
    try expect(math.isNan(tan32(-math.inf(f32))));
    try expect(math.isNan(tan32(math.nan(f32))));
}

test "math.tan64.special" {
    try expect(tan64(0.0) == 0.0);
    try expect(tan64(-0.0) == -0.0);
    try expect(math.isNan(tan64(math.inf(f64))));
    try expect(math.isNan(tan64(-math.inf(f64))));
    try expect(math.isNan(tan64(math.nan(f64))));
}