Floats

    • - IEEE-754-2008 binary16
    • f32 - IEEE-754-2008 binary32
    • f64 - IEEE-754-2008 binary64
    • f128 - IEEE-754-2008 binary128

    Float literals have type comptime_float which is guaranteed to have the same precision and operations of the largest other floating point type, which is .

    Float literals to any floating point type, and to any integer type when there is no fractional component.

    float_literals.zig

    float_special_values.zig

    1. const std = @import("std");
    2. const inf = std.math.inf(f32);
    3. const negative_inf = -std.math.inf(f64);
    4. const nan = std.math.nan(f128);

    Floating Point Operations

    By default floating point operations use Strict mode, but you can switch to mode on a per-block basis:

    foo.zig

    1. $ zig build-obj foo.zig -O ReleaseFast

    For this test we have to separate code into two object files - otherwise the optimizer figures out all the values at compile-time, which operates in strict mode.

    float_mode.zig

    Shell

    1. $ ./float_mode
    2. optimized = 1.0e-03
    3. strict = 9.765625e-04