Floats
- - IEEE-754-2008 binary16
f32
- IEEE-754-2008 binary32f64
- IEEE-754-2008 binary64f128
- 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
const std = @import("std");
const inf = std.math.inf(f32);
const negative_inf = -std.math.inf(f64);
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
$ 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
$ ./float_mode
optimized = 1.0e-03
strict = 9.765625e-04