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.

intFromFloatExact

parse.intFromFloatExact
fn intFromFloatExact(T: type, value: anytype) ?T

File

lib/std/zon/parse.zig:1175

Code

fn intFromFloatExact(T: type, value: anytype) ?T {
    const max: @TypeOf(value) = @floatFromInt(std.math.maxInt(T));
    const min: @TypeOf(value) = @floatFromInt(std.math.minInt(T));
    if (value > max or value < min) {
        return null;
    }

    if (std.math.isNan(value) or std.math.trunc(value) != value) {
        return null;
    }

    return @intFromFloat(value);
}