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, andlong double. C++ does not define their actual sizes but guarantees minimum sizes.
- Typically,
floatoccupies 4 bytes,doubleoccupies 8 bytes, andlong doublevaries 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
doubleoverfloatdue to precision issues. While both can cause errors,floatis 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, andNAND, 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.