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.
fnnextAfterFloat(comptimeT: type, x: T, y: T) T {
comptimeassert(@typeInfo(T) == .float);
if (x == y) {
// Returning `y` ensures that (0.0, -0.0) returns -0.0 and that (-0.0, 0.0) returns 0.0.returny;
}
if (math.isNan(x) ormath.isNan(y)) {
returnmath.nan(T);
}
if (x == 0.0) {
returnif (y > 0.0)
math.floatTrueMin(T)
else
-math.floatTrueMin(T);
}
if (@bitSizeOf(T) == 80) {
// Unlike other floats, `f80` has an explicitly stored integer bit between the fractional
// part and the exponent and thus requires special handling. This integer bit *must* be set
// when the value is normal, an infinity or a NaN and *should* be cleared otherwise.
constfractional_bits_mask = (1 << math.floatFractionalBits(f80)) - 1;
constinteger_bit_mask = 1 << math.floatFractionalBits(f80);
constexponent_bits_mask = (1 << math.floatExponentBits(f80)) - 1;
varx_parts = math.F80.fromFloat(x);
// Bitwise increment/decrement the fractional part while also taking care to update the
// exponent if we overflow the fractional part. This might flip the integer bit; this is
// intentional.
if ((x > 0.0) == (y > x)) {
x_parts.fraction +%= 1;
if (x_parts.fraction & fractional_bits_mask == 0) {
x_parts.exp += 1;
}
} else {
if (x_parts.fraction & fractional_bits_mask == 0) {
x_parts.exp -= 1;
}
x_parts.fraction -%= 1;
}
// If the new value is normal or an infinity (indicated by at least one bit in the exponent
// being set), the integer bit might have been cleared from an overflow, so we must ensure
// that it remains set.
if (x_parts.exp & exponent_bits_mask != 0) {
x_parts.fraction |= integer_bit_mask;
}
// Otherwise, the new value is subnormal and the integer bit will have either flipped from
// set to cleared (if the old value was normal) or remained cleared (if the old value was
// subnormal), both of which are the outcomes we want.
returnx_parts.toFloat();
} else {
constBits = @Int(.unsigned, @bitSizeOf(T));
varx_bits: Bits = @bitCast(x);
if ((x > 0.0) == (y > x)) {
x_bits += 1;
} else {
x_bits -= 1;
}
return@bitCast(x_bits);
}
}