Floating Point: f32 and f64
Work with fractional numbers.
Floating Point: f32 and f64 is a free Zig Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Zig Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Numbers with a Fraction
When you need decimals, Zig offers floating-point types. They store fractional values like 3.14 instead of whole numbers only.
f64 Is the Common Choice
The f64 type holds a double-precision float using 64 bits. It is the usual default when you want accuracy and plenty of range.
const pi: f64 = 3.14159;f32 Trades Precision for Size
An f32 uses 32 bits. It saves memory and can be faster, but it stores fewer significant digits than an f64.
const ratio: f32 = 0.5;Zig Has More Float Widths
Beyond f32 and f64, Zig also supports f16 and f128. You choose the width that balances precision against memory and speed.
Float Literals Need a Dot
A float literal includes a decimal point, like 2.0. Writing just 2 gives an integer, which is a different kind of value.
const half = 0.5;
const one = 1.0;Scientific Notation Works Too
You can write very large or small floats with an exponent. The form 1.5e3 means 1.5 times ten to the third power, or 1500.
const big: f64 = 1.5e3;Doing Float Math
The usual operators apply to floats. Division of two floats keeps the fractional part instead of throwing it away.
const result: f64 = 7.0 / 2.0; // 3.5Floats Are Approximate
Floats cannot store every decimal exactly. Tiny rounding errors mean 0.1 plus 0.2 may not equal exactly 0.3.
Compare with a Tolerance
Because of rounding, avoid testing floats for exact equality. Check that the difference is smaller than a tiny threshold instead.
const close = @abs(a - b) < 0.0001;Convert Int to Float
To turn an integer into a float you ask explicitly with @floatFromInt. Zig never mixes the two number kinds silently.
const n: i32 = 10;
const f: f64 = @floatFromInt(n);Special Float Values
Floats can also represent infinity and not-a-number. These appear from operations like dividing by zero, and the std library names them.
Quick Check
You divide one float by another and want to keep the fractional result. What is true about floating-point math in Zig?
Recap
Use f64 for general decimals and f32 to save space. Remember floats are approximate, so compare with a tolerance and cast on purpose. 🎯
Frequently asked questions
Is the “Floating Point: f32 and f64” lesson free?
Yes — the full text of “Floating Point: f32 and f64” is free to read here on the web, and the Zig Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Zig Academy course, upgrade to CoddyKit PRO.
What will I learn in “Floating Point: f32 and f64”?
Work with fractional numbers. You practise Zig Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Zig Academy?
No prior experience is required. Zig Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Floating Point: f32 and f64” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Zig Academy lesson?
Yes. Every Zig Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Sized Integers: i32, u8, usize
- Floating Point: f32 and f64
- bool, void, and the Comptime Types
- Number Literals and Underscores