aboutsummaryrefslogtreecommitdiff
path: root/lib/std/math/sin.zig
blob: 5b92fd454ba298f85a1a8ade8d6315eb8e03ab9b (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
141
142
143
144
145
146
147
148
149
150
// Ported from go, which is licensed under a BSD-3 license.
// https://golang.org/LICENSE
//
// https://golang.org/src/math/sin.go

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

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

// sin polynomial coefficients
const S0 = 1.58962301576546568060E-10;
const S1 = -2.50507477628578072866E-8;
const S2 = 2.75573136213857245213E-6;
const S3 = -1.98412698295895385996E-4;
const S4 = 8.33333333332211858878E-3;
const S5 = -1.66666666666666307295E-1;

// cos polynomial coeffiecients
const C0 = -1.13585365213876817300E-11;
const C1 = 2.08757008419747316778E-9;
const C2 = -2.75573141792967388112E-7;
const C3 = 2.48015872888517045348E-5;
const C4 = -1.38888888888730564116E-3;
const C5 = 4.16666666666665929218E-2;

const pi4a = 7.85398125648498535156e-1;
const pi4b = 3.77489470793079817668E-8;
const pi4c = 2.69515142907905952645E-15;
const m4pi = 1.273239544735162542821171882678754627704620361328125;

fn sin_(comptime T: type, x_: T) T {
    const I = @IntType(true, T.bit_count);

    var x = x_;
    if (x == 0 or math.isNan(x)) {
        return x;
    }
    if (math.isInf(x)) {
        return math.nan(T);
    }

    var sign = x < 0;
    x = math.fabs(x);

    var y = math.floor(x * m4pi);
    var j = @floatToInt(I, y);

    if (j & 1 == 1) {
        j += 1;
        y += 1;
    }

    j &= 7;
    if (j > 3) {
        j -= 4;
        sign = !sign;
    }

    const z = ((x - y * pi4a) - y * pi4b) - y * pi4c;
    const w = z * z;

    const r = if (j == 1 or j == 2)
        1.0 - 0.5 * w + w * w * (C5 + w * (C4 + w * (C3 + w * (C2 + w * (C1 + w * C0)))))
    else
        z + z * w * (S5 + w * (S4 + w * (S3 + w * (S2 + w * (S1 + w * S0)))));

    return if (sign) -r else r;
}

test "math.sin" {
    if (builtin.os == .linux and builtin.arch == .arm and builtin.abi == .musleabihf) {
        // TODO https://github.com/ziglang/zig/issues/3289
        return error.SkipZigTest;
    }
    expect(sin(@as(f32, 0.0)) == sin_(f32, 0.0));
    expect(sin(@as(f64, 0.0)) == sin_(f64, 0.0));
    expect(comptime (math.sin(@as(f64, 2))) == math.sin(@as(f64, 2)));
}

test "math.sin32" {
    if (builtin.os == .linux and builtin.arch == .arm and builtin.abi == .musleabihf) {
        // TODO https://github.com/ziglang/zig/issues/3289
        return error.SkipZigTest;
    }
    const epsilon = 0.000001;

    expect(math.approxEq(f32, sin_(f32, 0.0), 0.0, epsilon));
    expect(math.approxEq(f32, sin_(f32, 0.2), 0.198669, epsilon));
    expect(math.approxEq(f32, sin_(f32, 0.8923), 0.778517, epsilon));
    expect(math.approxEq(f32, sin_(f32, 1.5), 0.997495, epsilon));
    expect(math.approxEq(f32, sin_(f32, -1.5), -0.997495, epsilon));
    expect(math.approxEq(f32, sin_(f32, 37.45), -0.246544, epsilon));
    expect(math.approxEq(f32, sin_(f32, 89.123), 0.916166, epsilon));
}

test "math.sin64" {
    if (builtin.os == .linux and builtin.arch == .arm and builtin.abi == .musleabihf) {
        // TODO https://github.com/ziglang/zig/issues/3289
        return error.SkipZigTest;
    }
    const epsilon = 0.000001;

    expect(math.approxEq(f64, sin_(f64, 0.0), 0.0, epsilon));
    expect(math.approxEq(f64, sin_(f64, 0.2), 0.198669, epsilon));
    expect(math.approxEq(f64, sin_(f64, 0.8923), 0.778517, epsilon));
    expect(math.approxEq(f64, sin_(f64, 1.5), 0.997495, epsilon));
    expect(math.approxEq(f64, sin_(f64, -1.5), -0.997495, epsilon));
    expect(math.approxEq(f64, sin_(f64, 37.45), -0.246543, epsilon));
    expect(math.approxEq(f64, sin_(f64, 89.123), 0.916166, epsilon));
}

test "math.sin32.special" {
    if (builtin.os == .linux and builtin.arch == .arm and builtin.abi == .musleabihf) {
        // TODO https://github.com/ziglang/zig/issues/3289
        return error.SkipZigTest;
    }
    expect(sin_(f32, 0.0) == 0.0);
    expect(sin_(f32, -0.0) == -0.0);
    expect(math.isNan(sin_(f32, math.inf(f32))));
    expect(math.isNan(sin_(f32, -math.inf(f32))));
    expect(math.isNan(sin_(f32, math.nan(f32))));
}

test "math.sin64.special" {
    if (builtin.os == .linux and builtin.arch == .arm and builtin.abi == .musleabihf) {
        // TODO https://github.com/ziglang/zig/issues/3289
        return error.SkipZigTest;
    }
    expect(sin_(f64, 0.0) == 0.0);
    expect(sin_(f64, -0.0) == -0.0);
    expect(math.isNan(sin_(f64, math.inf(f64))));
    expect(math.isNan(sin_(f64, -math.inf(f64))));
    expect(math.isNan(sin_(f64, math.nan(f64))));
}