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 standard library.
std › math › isfinite Public Namespace isfinite Doc Source Public Namespace Namespaces 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 isFinite (x : anytype ) bool Returns whether x is a finite value.
File math/isfinite.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 const std = @import ("../std.zig " );
const math = std .math ;
const expect = std .testing .expect ;
/// Returns whether x is a finite value.
pub fn isFinite (x : anytype ) bool {
const T = @TypeOf (x );
const TBits = @Int (.unsigned , @typeInfo (T ).float .bits );
const remove_sign = ~@as (TBits , 0 ) >> 1 ;
return @as (TBits , @bitCast (x )) & remove_sign < @as (TBits , @bitCast (math .inf (T )));
}
test isFinite {
inline for ([_ ]type { f16 , f32 , f64 , f80 , f128 }) |T | {
// normals
try expect (isFinite (@as (T , 1.0 )));
try expect (isFinite (-@as (T , 1.0 )));
// zero & subnormals
try expect (isFinite (@as (T , 0.0 )));
try expect (isFinite (@as (T , -0.0 )));
try expect (isFinite (math .floatTrueMin (T )));
// other float limits
try expect (isFinite (math .floatMin (T )));
try expect (isFinite (math .floatMax (T )));
// inf & nan
try expect (!isFinite (math .inf (T )));
try expect (!isFinite (-math .inf (T )));
try expect (!isFinite (math .nan (T )));
try expect (!isFinite (-math .nan (T )));
}
}