Zig 0.17.0-dev (Split by item)

Shortcut keys
?
Show these tips
S
Focus search field
Esc
Clear focus & close tips
U
Jump to source code
↑
Move up in search results
↓
Move down in search results
↵
Go to active search result
This is an example of documentation generated by ZigDoc, an alternative to Zig's built-in Auto Doc feature. See also examples in other modes/formats. The project being documented here (as the example) is the Zig standard library. Private Type Function

ModfTests

Private Type Function
std.math.modf.ModfTests
fn ModfTests(comptime T: type) type

Generate a namespace of tests for modf on values of the given type

File

math/modf.zig:54
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
fn ModfTests(comptime T: type) type {
    return struct {
        test "normal" {
            const epsilon: comptime_float = @max(1e-6, math.floatEps(T));
            var r: Modf(T) = undefined;

            r = modf(@as(T, 1.0));
            try expectEqual(1.0, r.ipart);
            try expectEqual(0.0, r.fpart);

            r = modf(@as(T, 0.34682));
            try expectEqual(0.0, r.ipart);
            try expectApproxEqAbs(0.34682, r.fpart, epsilon);

            r = modf(@as(T, 3.97812));
            try expectEqual(3.0, r.ipart);
            // account for precision error
            const expected_a: T = 3.97812 - @as(T, 3);
            try expectApproxEqAbs(expected_a, r.fpart, epsilon);

            r = modf(@as(T, 43874.3));
            try expectEqual(43874.0, r.ipart);
            // account for precision error
            const expected_b: T = 43874.3 - @as(T, 43874.0);
            try expectApproxEqAbs(expected_b, r.fpart, epsilon);

            r = modf(@as(T, 1234.340780));
            try expectEqual(1234.0, r.ipart);
            // account for precision error
            const expected_c: T = 1234.340780 - @as(T, 1234);
            try expectApproxEqAbs(expected_c, r.fpart, epsilon);
        }
        test "vector" {
            const widths = [_]comptime_int{ 1, 2, 3, 4, 8, 16 };

            inline for (widths) |len| {
                const V: type = @Vector(len, T);
                var r: Modf(V) = undefined;

                r = modf(@as(V, @splat(1.0)));
                try expectEqual(@as(V, @splat(1.0)), r.ipart);
                try expectEqual(@as(V, @splat(0.0)), r.fpart);

                r = modf(@as(V, @splat(2.75)));
                try expectEqual(@as(V, @splat(2.0)), r.ipart);
                try expectEqual(@as(V, @splat(0.75)), r.fpart);

                r = modf(@as(V, @splat(0.2)));
                try expectEqual(@as(V, @splat(0.0)), r.ipart);
                try expectEqual(@as(V, @splat(0.2)), r.fpart);

                r = modf(std.simd.iota(T, len) + @as(V, @splat(0.5)));
                try expectEqual(std.simd.iota(T, len), r.ipart);
                try expectEqual(@as(V, @splat(0.5)), r.fpart);
            }
        }
        test "inf" {
            var r: Modf(T) = undefined;

            r = modf(math.inf(T));
            try expect(math.isPositiveInf(r.ipart) and math.isNan(r.fpart));

            r = modf(-math.inf(T));
            try expect(math.isNegativeInf(r.ipart) and math.isNan(r.fpart));
        }
        test "nan" {
            const r: Modf(T) = modf(math.nan(T));
            try expect(math.isNan(r.ipart) and math.isNan(r.fpart));
        }
    };
}