跳转到内容
标准库Zig 0.17.0官方原文
源码std/math/isnan.zig

第 1 行

std.math.isnan.builtin

常量

第 2 行

std.math.isnan.expect

别名

第 4 行

别名目标 std.testing.expect

std.math.isnan.isNan

函数

第 6 行

pub fn isNan(x: anytype) bool

用法示例

test isNan {
    inline for ([_]type{ f16, f32, f64, f80, f128, c_longdouble }) |T| {
        try expect(isNan(math.nan(T)));
        try expect(isNan(-math.nan(T)));
        try expect(isNan(math.snan(T)));
        try expect(!isNan(@as(T, 1.0)));
        try expect(!isNan(@as(T, math.inf(T))));
    }
}

std.math.isnan.isSignalNan

函数

第 12 行

TODO: 已知 LLVM 在某些架构上会错误编译为静默 NaN, 跟踪议题:https://github.com/ziglang/zig/issues/14366

pub fn isSignalNan(x: anytype) bool

用法示例

test isSignalNan {
    inline for ([_]type{ f16, f32, f64, f80, f128, c_longdouble }) |T| {
        // TODO: 某些不应发生转换的情况下,信号 NaN 会被转换为静默 NaN,
        //       因而此操作可能失败。
        //       见 https://github.com/ziglang/zig/issues/14366
        if (!builtin.cpu.arch.isArm() and
            !builtin.cpu.arch.isAARCH64() and
            builtin.cpu.arch != .hexagon and
            !builtin.cpu.arch.isMIPS32() and
            !builtin.cpu.arch.isPowerPC() and
            !(builtin.cpu.arch.isX86() and builtin.os.tag == .windows and builtin.abi == .msvc) and // https://codeberg.org/ziglang/zig/issues/35519
            builtin.zig_backend != .stage2_c)
        {
            try expect(isSignalNan(math.snan(T)));
        }
        try expect(!isSignalNan(math.nan(T)));
        try expect(!isSignalNan(@as(T, 1.0)));
        try expect(!isSignalNan(math.inf(T)));
    }
}

std.math.isnan.math

别名

第 3 行

别名目标 std.math

std.math.isnan.std

别名

第 1 行

别名目标 std

源码

std/math/isnan.zig

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

pub fn isNan(x: anytype) bool {
    return x != x;
}

/// TODO: 已知 LLVM 在某些架构上会错误编译为静默 NaN,
///       跟踪议题:https://github.com/ziglang/zig/issues/14366
pub fn isSignalNan(x: anytype) bool {
    const T = @TypeOf(x);
    const U = @Int(.unsigned, @bitSizeOf(T));
    const quiet_signal_bit_mask = 1 << (math.floatFractionalBits(T) - 1);
    return isNan(x) and (@as(U, @bitCast(x)) & quiet_signal_bit_mask == 0);
}

test isNan {
    inline for ([_]type{ f16, f32, f64, f80, f128, c_longdouble }) |T| {
        try expect(isNan(math.nan(T)));
        try expect(isNan(-math.nan(T)));
        try expect(isNan(math.snan(T)));
        try expect(!isNan(@as(T, 1.0)));
        try expect(!isNan(@as(T, math.inf(T))));
    }
}

test isSignalNan {
    inline for ([_]type{ f16, f32, f64, f80, f128, c_longdouble }) |T| {
        // TODO: 某些不应发生转换的情况下,信号 NaN 会被转换为静默 NaN,
        //       因而此操作可能失败。
        //       见 https://github.com/ziglang/zig/issues/14366
        if (!builtin.cpu.arch.isArm() and
            !builtin.cpu.arch.isAARCH64() and
            builtin.cpu.arch != .hexagon and
            !builtin.cpu.arch.isMIPS32() and
            !builtin.cpu.arch.isPowerPC() and
            !(builtin.cpu.arch.isX86() and builtin.os.tag == .windows and builtin.abi == .msvc) and // https://codeberg.org/ziglang/zig/issues/35519
            builtin.zig_backend != .stage2_c)
        {
            try expect(isSignalNan(math.snan(T)));
        }
        try expect(!isSignalNan(math.nan(T)));
        try expect(!isSignalNan(@as(T, 1.0)));
        try expect(!isSignalNan(math.inf(T)));
    }
}