Zig 0.17.0-dev (Split by item)
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 library itself.
Zig › std/ › math/ › isnan.zig
isnan.zig
Index
File
Code
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 ;
}
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 {
if (builtin .zig_backend == .stage2_x86_64 and builtin .object_format == .coff and builtin .abi != .gnu ) return error .SkipZigTest ;
if (builtin .os .tag == .windows ) {
return error .SkipZigTest ;
}
inline for ([_ ]type { f16 , f32 , f64 , f80 , f128 , c_longdouble }) |T | {
// some cases where they shouldn't such that this can fail.
// See 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 .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 )));
}
}