float / bool
Zig provides clearly distinct types for floating-point numbers, booleans, and void. The floating-point types — f16, f32, f64, and f128 — conform to IEEE 754. The boolean type bool holds one of two values: true or false. void is a type that carries no value and is used as the return type of functions that return nothing.
Syntax
// -----------------------------------------------
// Floating-point types (f + bit width)
// -----------------------------------------------
var a: f16 = 3.14; // f16 : half-precision (16-bit)
var b: f32 = 3.14; // f32 : single-precision (32-bit)
var c: f64 = 3.14159; // f64 : double-precision (64-bit), the default choice
var d: f128 = 3.14; // f128 : quad-precision (128-bit)
// -----------------------------------------------
// bool type
// -----------------------------------------------
var flag: bool = true; // holds either true or false
var ng: bool = false;
// The result of a comparison expression is bool
const is_big: bool = (c > 3.0); // true
// -----------------------------------------------
// void type
// -----------------------------------------------
// Use void as the return type for functions that return nothing
fn do_nothing() void {
// returns nothing
}
// Commonly combined with error union as !void
fn greet() !void {
const stdout = std.io.getStdOut().writer();
try stdout.print("hello\n", .{});
}
// -----------------------------------------------
// Type conversion (@as / @floatCast / @floatFromInt)
// -----------------------------------------------
const int_val: i32 = 5;
const float_val: f64 = @floatFromInt(int_val); // integer → floating-point
const narrow: f32 = @floatCast(float_val); // cast f64 → f32
Syntax reference
| Type / Syntax | Description |
|---|---|
f16 | 16-bit half-precision floating-point. Used when you want to save memory, but precision is limited. |
f32 | 32-bit single-precision floating-point. Commonly used in graphics and embedded applications. |
f64 | 64-bit double-precision floating-point. The default choice for general numerical computation. |
f128 | 128-bit quad-precision floating-point. Used when extremely high precision is required. |
bool | Boolean type. Holds either true or false. |
true / false | Literals of type bool. They appear as the result of conditional expressions and comparison operations. |
void | A type that carries no value. Used as the return type of functions that return nothing. |
!void | An error union type meaning "success returns void, failure returns an error value." Commonly used in main() and I/O operations. |
@floatFromInt(val) | Converts an integer value to a floating-point number. |
@intFromFloat(val) | Converts a floating-point number to an integer. The fractional part is truncated. |
@floatCast(val) | Casts a floating-point value to a different floating-point type. |
std.math.isNan(val) | Returns whether the value is NaN (Not a Number). |
std.math.isInf(val) | Returns whether the value is infinity. |
Sample code
float_bool.zig
// float_bool.zig — sample code for Zig's floating-point, bool, and void types
// Manages synchronization rates (floating-point) and sortie flags (bool)
// for Evangelion pilots to demonstrate each type in practice
const std = @import("std");
// -----------------------------------------------
// Example of a function with a void return type
// Use void for utility functions that don't need to return a value
// -----------------------------------------------
fn print_separator(stdout: anytype) void {
// No error is returned, so void is used (caller does not need try)
stdout.print("------------------------------\n", .{}) catch {};
}
pub fn main() !void {
// Get a Writer for standard output
const stdout = std.io.getStdOut().writer();
try stdout.print("=== Evangelion Pilot Synchronization Rate Data ===\n\n", .{});
// -----------------------------------------------
// Define each pilot's sync rate as f64
// Sync rates range roughly from 0.0 to 400.0, so f64 provides sufficient precision
// -----------------------------------------------
const shinji_sync: f64 = 141.7; // Ikari Shinji's sync rate
const rei_sync: f64 = 98.4; // Ayanami Rei's sync rate
const asuka_sync: f64 = 157.3; // Soryu Asuka Langley's sync rate
const kaworu_sync: f64 = 400.0; // Nagisa Kaworu's sync rate (theoretical maximum)
const misato_sync: f64 = 0.0; // Katsuragi Misato's sync rate (not a pilot, so 0.0)
// Define sortie eligibility flags as bool
// Only pilots with a sync rate of 30.0 or above are cleared for sortie
const min_sync: f64 = 30.0;
const shinji_can_sortie: bool = shinji_sync >= min_sync;
const rei_can_sortie: bool = rei_sync >= min_sync;
const asuka_can_sortie: bool = asuka_sync >= min_sync;
const kaworu_can_sortie: bool = kaworu_sync >= min_sync;
const misato_can_sortie: bool = misato_sync >= min_sync;
// -----------------------------------------------
// Display sync rates and sortie eligibility
// {d:.1} formats a floating-point value to one decimal place
// -----------------------------------------------
try stdout.print("Pilot Sync Rate Sortie\n", .{});
print_separator(stdout);
try stdout.print("Ikari Shinji {d:.1}% {}\n", .{ shinji_sync, shinji_can_sortie });
try stdout.print("Ayanami Rei {d:.1}% {}\n", .{ rei_sync, rei_can_sortie });
try stdout.print("Soryu Asuka Langley {d:.1}% {}\n", .{ asuka_sync, asuka_can_sortie });
try stdout.print("Nagisa Kaworu {d:.1}% {}\n", .{ kaworu_sync, kaworu_can_sortie });
try stdout.print("Katsuragi Misato {d:.1}% {}\n", .{ misato_sync, misato_can_sortie });
// -----------------------------------------------
// Calculate the average sync rate as f64
// Use @floatFromInt() to cast an integer before dividing
// -----------------------------------------------
try stdout.print("\n--- Average Sync Rate Calculation ---\n", .{});
const total: f64 = shinji_sync + rei_sync + asuka_sync + kaworu_sync + misato_sync;
const count: f64 = @floatFromInt(@as(i32, 5)); // convert integer 5 to f64
const average: f64 = total / count;
try stdout.print("Total: {d:.1}%\n", .{total});
try stdout.print("Average: {d:.2}%\n", .{average});
// -----------------------------------------------
// Cast to f32 and observe the difference in precision
// Use @floatCast() to convert f64 → f32
// -----------------------------------------------
try stdout.print("\n--- f64 vs f32 Precision Comparison ---\n", .{});
const asuka_f64: f64 = asuka_sync; // sync rate as f64
const asuka_f32: f32 = @floatCast(asuka_f64); // cast f64 → f32
try stdout.print("Asuka sync rate f64: {d:.6}\n", .{asuka_f64});
try stdout.print("Asuka sync rate f32: {d:.6}\n", .{asuka_f32});
// -----------------------------------------------
// Convert a floating-point number to an integer
// @intFromFloat() truncates the fractional part
// -----------------------------------------------
try stdout.print("\n--- Extracting the Integer Part of Sync Rates ---\n", .{});
const shinji_int: i32 = @intFromFloat(shinji_sync); // 141.7 → 141
const rei_int: i32 = @intFromFloat(rei_sync); // 98.4 → 98
const asuka_int: i32 = @intFromFloat(asuka_sync); // 157.3 → 157
try stdout.print("Shinji integer part: {d}\n", .{shinji_int});
try stdout.print("Rei integer part: {d}\n", .{rei_int});
try stdout.print("Asuka integer part: {d}\n", .{asuka_int});
// -----------------------------------------------
// Use bool in conditional branching
// -----------------------------------------------
try stdout.print("\n--- Sortie Orders ---\n", .{});
if (shinji_can_sortie) {
try stdout.print("Ikari Shinji: Sortie approved. Launch Unit 01.\n", .{});
} else {
try stdout.print("Ikari Shinji: Sync rate too low. Sortie cancelled.\n", .{});
}
if (!misato_can_sortie) {
try stdout.print("Katsuragi Misato: Not a pilot. Cannot sortie.\n", .{});
}
}
zig run float_bool.zig === Evangelion Pilot Synchronization Rate Data === Pilot Sync Rate Sortie ------------------------------ Ikari Shinji 141.7% true Ayanami Rei 98.4% true Soryu Asuka Langley 157.3% true Nagisa Kaworu 400.0% true Katsuragi Misato 0.0% false --- Average Sync Rate Calculation --- Total: 797.4% Average: 159.48% --- f64 vs f32 Precision Comparison --- Asuka sync rate f64: 157.300000 Asuka sync rate f32: 157.300003 --- Extracting the Integer Part of Sync Rates --- Shinji integer part: 141 Rei integer part: 98 Asuka integer part: 157 --- Sortie Orders --- Ikari Shinji: Sortie approved. Launch Unit 01. Katsuragi Misato: Not a pilot. Cannot sortie.
Overview
Zig provides four floating-point types — f16, f32, f64, and f128 — all conforming to IEEE 754. f64 is the common default for general numerical computation, while f32 is preferred when memory efficiency matters, such as in graphics or embedded contexts. To convert an integer to a floating-point number, use @floatFromInt(). To convert a floating-point number to an integer (truncating the fractional part), use @intFromFloat(). To cast between floating-point types of different precision, use @floatCast(). The bool type holds either true or false and can be used directly in if statements and while conditions. Comparison operators (>=, ==, !=, etc.) automatically produce a bool result. Use void as the return type of functions that return nothing. If the function may return an error, specify !void (an error union type) and use try at the call site to propagate errors. For integer types, see Integer types. For variable and constant declarations, see Variables and constants.
If you find any errors or copyright issues, please contact us.