025.- Curso C++ Básico. Los Float point o números de punto flotante.

025.- Curso C++ Básico. Los Float point o números de punto flotante.

Understanding Floating Point Numbers in Programming

Introduction to Floating Point Numbers

  • Floating point numbers are used in programming to store numbers with fractional components and very large values using scientific notation.
  • In C++, floating point variables can hold real numbers, where the decimal point can "float," allowing for a variable number of digits before and after it.

Types of Floating Point Data

  • There are three types of floating point data: float, double, and long double. C++ does not define their actual sizes but guarantees minimum sizes.
  • Typically, float occupies 4 bytes, double occupies 8 bytes, and long double varies between 8 to 16 bytes. All floating points are signed.

Best Practices for Using Literals

  • It is essential to ensure that the type of literals matches the type of variables they are assigned to; otherwise, unnecessary conversions may lead to information loss.
  • Avoid using integer literals when floating point literals are required during initialization or function parameters expecting floating points.

Output Behavior in Floating Points

  • When printing a float like 5.0, if the fractional part is zero, it may not display (e.g., output shows just '5').
  • Scientific notation is introduced when dealing with large numbers; calculators can help convert these into scientific format.

Precision Limitations

  • Computers cannot represent infinite decimal fractions accurately due to memory limitations; thus, floating point numbers have a certain amount of significant digits known as precision.
  • The default precision for standard output (std::cout) is typically set at 6 significant digits.

Understanding Precision Variability

  • The number of significant digits depends on both the size (with floats having lower precision than doubles) and the specific value stored.
  • You can override default precision settings using output manipulators from <iomanip>, but this does not guarantee accuracy beyond inherent limits.

Issues with Precision in Calculations

  • Precision issues affect all floating-point calculations; even small discrepancies can arise from rounding errors when too many significant digits are involved.

Understanding Floating Point Precision Issues

The Importance of Using Double Over Float

  • It is recommended to use double over float due to precision issues. While both can cause errors, float is particularly prone to rounding errors.
  • Assigning a decimal like 0.1 to a floating-point number leads to precision problems because its binary representation is an infinite sequence, causing truncation in memory.

Rounding Errors and Their Consequences

  • Rounding errors can result in unexpected outcomes; for instance, summing 10 instances of 0.1 may not yield exactly 1 due to these inaccuracies.
  • Comparing floating-point numbers can be problematic as small rounding errors accumulate during mathematical operations like addition or multiplication.

Key Takeaways on Floating Point Numbers

  • Rounding errors are common with floating-point types and should not be assumed as exceptions; they are the rule. Caution is advised when using them, especially for financial data.

Special Categories of Floating Point Numbers

  • There are two special categories: INF (infinity), which can be positive or negative, and NAND, indicating "not a number." Their behavior may vary across different compilers.

Best Practices and Summary

  • Avoid division by zero as it leads to undefined behavior. Floating point numbers are useful for representing very large or small values but come with inherent rounding issues that often go unnoticed.
Video description

Acceso al AsistenteCPP: https://tinyurl.com/AsistenteCPP. NOTA: En el minuto 7:54 hay apenas un segundo que no se oye, lo que se dice en ese momento es: "vemos que "d" no nos da 0.1 exacto". Videotutorial que dedicamos a los Float point o números de punto flotante; Los tres tipos de datos float point: float, double, long double; Ejemplos de definiciones con números de punto flotante; Uso de prefijos para determinar el tipo; Uso de notaciones científicas; Número de dígitos de precisión y truncamientos; Anular precisión predeterminada con el uso de std::setprecision( ); Errores de redondeo; Categorías especiales de números flotantes: NaN e Inf. Código completo del curso: https://github.com/0utKast/CursoCPPGitHub/archive/refs/heads/master.zip