Ponto flutuante: f32 e f64
Trabalhe com números fracionários.
Ponto flutuante: f32 e f64 é uma aula grátis de Zig Academy no CoddyKit. Esta é a aula 2 de 4. Você pode ler a aula completa abaixo gratuitamente — depois pratica ao vivo no navegador com um editor de código integrado e um tutor de IA 24/7. Faz parte do caminho de aprendizado de Zig Academy, e seu progresso é sincronizado entre a web e o app CoddyKit. O curso de Zig Academy inclui 4 aulas no total.
Partes desta aula ainda não foram traduzidas e aparecem em inglês.
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. 🎯
Perguntas Frequentes
A aula “Ponto flutuante: f32 e f64” é grátis?
Sim — o texto completo de “Ponto flutuante: f32 e f64” é grátis para ler aqui na web. Para praticá-la interativamente (um editor de código integrado e um tutor de IA 24/7) e desbloquear o restante do curso de Zig Academy, atualize para CoddyKit PRO. O curso de Zig Academy inclui 4 aulas no total.
O que vou aprender em “Ponto flutuante: f32 e f64”?
Trabalhe com números fracionários. Você pratica Zig Academy com código prático que executa diretamente no navegador, e um tutor de IA 24/7 responde suas dúvidas enquanto trabalha na aula.
Preciso ter experiência prévia para começar Zig Academy?
Nenhuma experiência prévia é necessária. Zig Academy no CoddyKit é estruturado para alunos iniciantes até avançados, então você pode começar aqui ou desde o início e aprender no seu ritmo. Esta é a aula 2 de 4.
Quanto tempo leva a aula “Ponto flutuante: f32 e f64”?
A maioria das aulas CoddyKit leva cerca de 5–10 minutos. Cada uma é compacta e interativa, então você faz progresso constante e retoma exatamente de onde parou entre web e app.
Posso escrever e executar código nesta aula de Zig Academy?
Sim. Cada aula de Zig Academy inclui um editor de código integrado, então você escreve e executa código real direto no navegador e recebe feedback de IA instantaneamente — nenhuma configuração local necessária.
Todas as aulas deste curso
- Inteiros com tamanho definido: i32, u8, usize
- Ponto flutuante: f32 e f64
- bool, void e os tipos comptime
- Literais numéricos e sublinhados