0Pricing
Zig Academy · Lección

Coma flotante: f32 y f64

Trabaje con números fraccionarios.

Coma flotante: f32 y f64 es una lección gratuita de Zig Academy en CoddyKit. Esta es la lección 2 de 4. Puedes leer la lección completa abajo gratuitamente — luego la practicas en el navegador con un editor de código integrado y un tutor de IA 24/7. Forma parte de la ruta de aprendizaje de Zig Academy, y tu progreso se sincroniza en la web y la app de CoddyKit. El curso de Zig Academy incluye 4 lecciones en total.

Partes de esta lección aún no han sido traducidas y se muestran en 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.5

Floats 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. 🎯

Preguntas frecuentes

¿La lección «Coma flotante: f32 y f64» es gratis?

Sí — el texto completo de «Coma flotante: f32 y f64» es gratis para leer aquí en la web. Para practicarla de forma interactiva (editor de código integrado y tutor de IA 24/7) y desbloquear el resto del curso de Zig Academy, actualiza a CoddyKit PRO. El curso de Zig Academy incluye 4 lecciones en total.

¿Qué aprenderé en «Coma flotante: f32 y f64»?

Trabaje con números fraccionarios. Practicas Zig Academy con código real que ejecutas directamente en el navegador, y un tutor de IA 24/7 responde tus preguntas mientras trabajas en la lección.

¿Necesito experiencia previa para empezar Zig Academy?

No se requiere experiencia previa. Zig Academy en CoddyKit está estructurado para principiantes hasta estudiantes avanzados, así que puedes empezar aquí o desde el inicio y avanzar a tu ritmo. Esta es la lección 2 de 4.

¿Cuánto tiempo toma la lección «Coma flotante: f32 y f64»?

La mayoría de las lecciones de CoddyKit toman alrededor de 5–10 minutos. Cada una es compacta e interactiva, así que avanzas constantemente y retomas exactamente por donde dejaste en la web y la app.

¿Puedo escribir y ejecutar código en esta lección de Zig Academy?

Sí. Cada lección de Zig Academy incluye un editor de código integrado, así que escribes y ejecutas código real directamente en tu navegador y obtienes retroalimentación instantánea de IA — sin configuración local necesaria.

Todas las lecciones de este curso

  1. Enteros con tamaño: i32, u8, usize
  2. Coma flotante: f32 y f64
  3. bool, void y los tipos comptime
  4. Literales numéricos y guiones bajos
← Volver a Zig Academy