aboutsummaryrefslogtreecommitdiff
path: root/lib/std/math/fabs.zig
blob: f1bb4be7e78124f8cfc9e13edd59794238837f63 (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
const std = @import("../std.zig");
const math = std.math;
const expect = std.testing.expect;

/// Returns the absolute value of x.
///
/// Special Cases:
///  - fabs(+-inf) = +inf
///  - fabs(nan)   = nan
pub fn fabs(x: anytype) @TypeOf(x) {
    const T = @TypeOf(x);
    const TBits = std.meta.Int(.unsigned, @bitSizeOf(T));
    if (@typeInfo(T) != .Float) {
        @compileError("fabs not implemented for " ++ @typeName(T));
    }

    const float_bits = @bitCast(TBits, x);
    const remove_sign = ~@as(TBits, 0) >> 1;

    return @bitCast(T, float_bits & remove_sign);
}

test "math.fabs" {
    // TODO add support for f80 & c_longdouble here
    inline for ([_]type{ f16, f32, f64, f128 }) |T| {
        // normals
        try expect(fabs(@as(T, 1.0)) == 1.0);
        try expect(fabs(@as(T, -1.0)) == 1.0);
        try expect(fabs(math.floatMin(T)) == math.floatMin(T));
        try expect(fabs(-math.floatMin(T)) == math.floatMin(T));
        try expect(fabs(math.floatMax(T)) == math.floatMax(T));
        try expect(fabs(-math.floatMax(T)) == math.floatMax(T));

        // subnormals
        try expect(fabs(@as(T, 0.0)) == 0.0);
        try expect(fabs(@as(T, -0.0)) == 0.0);
        try expect(fabs(math.floatTrueMin(T)) == math.floatTrueMin(T));
        try expect(fabs(-math.floatTrueMin(T)) == math.floatTrueMin(T));

        // non-finite numbers
        try expect(math.isPositiveInf(fabs(math.inf(T))));
        try expect(math.isPositiveInf(fabs(-math.inf(T))));
        try expect(math.isNan(fabs(math.nan(T))));
    }
}