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.
conststd = @import("../std.zig");
constmath = std.math;
constexpect = std.testing.expect;
/// Returns whether x is neither zero, subnormal, infinity, or NaN.pubfnisNormal(x: anytype) bool {
constT = @TypeOf(x);
constTBits = @Int(.unsigned, @typeInfo(T).float.bits);
constincrement_exp = 1 << math.floatMantissaBits(T);
constremove_sign = ~@as(TBits, 0) >> 1;
// We add 1 to the exponent, and if it overflows to 0 or becomes 1,
// then it was all zeroes (subnormal) or all ones (special, inf/nan).
// The sign bit is removed because all ones would overflow into it.
// For f80, even though it has an explicit integer part stored,
// the exponent effectively takes priority if mismatching.
constvalue = @as(TBits, @bitCast(x)) +% increment_exp;
returnvalue & remove_sign >= (increment_exp << 1);
}
testisNormal {
// TODO add `c_longdouble' when math.inf(T) supports itinlinefor ([_]type{ f16, f32, f64, f80, f128 }) |T| {
constTBits = @Int(.unsigned, @bitSizeOf(T));
// normalstryexpect(isNormal(@as(T, 1.0)));
tryexpect(isNormal(math.floatMin(T)));
tryexpect(isNormal(math.floatMax(T)));
// subnormalstryexpect(!isNormal(@as(T, -0.0)));
tryexpect(!isNormal(@as(T, 0.0)));
tryexpect(!isNormal(@as(T, math.floatTrueMin(T))));
// largest subnormaltryexpect(!isNormal(@as(T, @bitCast(~(~@as(TBits, 0) << math.floatFractionalBits(T))))));
// non-finite numberstryexpect(!isNormal(-math.inf(T)));
tryexpect(!isNormal(math.inf(T)));
tryexpect(!isNormal(math.nan(T)));
// overflow edge-case (described in implementation, also see #10133)tryexpect(!isNormal(@as(T, @bitCast(~@as(TBits, 0)))));
}
}