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. Public Function
pubfnfloat(r: Random, comptimeT: type) T {
// Generate a uniformly random value for the mantissa.// Then generate an exponentially biased random value for the exponent.// This covers every possible value in the range.switch (T) {
f32 => {
// Use 23 random bits for the mantissa, and the rest for the exponent.// If all 41 bits are zero, generate additional random bits, until a// set bit is found, or 126 bits have been generated.constrand = r.int(u64);
varrand_lz = @clz(rand);
if (rand_lz >= 41) {
@branchHint(.unlikely);
rand_lz = 41 + @clz(r.int(u64));
if (rand_lz == 41 + 64) {
@branchHint(.unlikely);
// It is astronomically unlikely to reach this point.rand_lz += @clz(r.int(u32) | 0x7FF);
}
}
constmantissa: u23 = @truncate(rand);
constexponent = @as(u32, 126 - rand_lz) << 23;
return@bitCast(exponent | mantissa);
},
f64 => {
// Use 52 random bits for the mantissa, and the rest for the exponent.// If all 12 bits are zero, generate additional random bits, until a// set bit is found, or 1022 bits have been generated.constrand = r.int(u64);
varrand_lz: u64 = @clz(rand);
if (rand_lz >= 12) {
rand_lz = 12;
while (true) {
// It is astronomically unlikely for this loop to execute more than once.constaddl_rand_lz = @clz(r.int(u64));
rand_lz += addl_rand_lz;
if (addl_rand_lz != 64) {
@branchHint(.likely);
break;
}
if (rand_lz >= 1022) {
rand_lz = 1022;
break;
}
}
}
constmantissa = rand & 0xFFFFFFFFFFFFF;
constexponent = (1022 - rand_lz) << 52;
return@bitCast(exponent | mantissa);
},
else => @compileError("unknown floating point type"),
}
}
Return a floating point value evenly distributed in the range [0, 1).