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. Public Namespace

signbit

Public Namespace

Namespaces

Values

Functions

pub fn expect(ok: bool) !void

This function is intended to be used only in tests. When ok is false, returns a test failure error.

pub fn signbit(x: anytype) bool

Returns whether x is negative or negative 0.

fn testInts(comptime Type: type) !void
fn testFloats(comptime Type: type) !void

File

math/signbit.zig
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
const builtin = @import("builtin");
const std = @import("../std.zig");
const math = std.math;
const expect = std.testing.expect;

/// Returns whether x is negative or negative 0.
pub fn signbit(x: anytype) bool {
    return switch (@typeInfo(@TypeOf(x))) {
        .int, .comptime_int => x,
        .float => |float| @as(@Int(.signed, float.bits), @bitCast(x)),
        .comptime_float => @as(i128, @bitCast(@as(f128, x))), // any float type will do
        else => @compileError("std.math.signbit does not support " ++ @typeName(@TypeOf(x))),
    } < 0;
}

test signbit {
    try testInts(u0);
    try testInts(i1);
    try testInts(u1);
    try testInts(i2);
    try testInts(u2);

    try testFloats(f16);
    try testFloats(f32);
    try testFloats(f64);
    try testFloats(f80);
    try testFloats(f128);
    try testFloats(c_longdouble);
    try testFloats(comptime_float);
}

fn testInts(comptime Type: type) !void {
    try expect((std.math.minInt(Type) < 0) == signbit(@as(Type, std.math.minInt(Type))));
    try expect(!signbit(@as(Type, 0)));
    try expect(!signbit(@as(Type, std.math.maxInt(Type))));
}

fn testFloats(comptime Type: type) !void {
    try expect(!signbit(@as(Type, 0.0)));
    try expect(!signbit(@as(Type, 1.0)));
    try expect(signbit(@as(Type, -2.0)));
    try expect(signbit(@as(Type, -0.0)));
    try expect(!signbit(math.inf(Type)));
    try expect(signbit(-math.inf(Type)));
    try expect(!signbit(math.nan(Type)));
    try expect(signbit(-math.nan(Type)));
}