You have 3 free guides left 😟
Unlock your guides
You have 3 free guides left 😟
Unlock your guides

2.4 Floating-point errors

2 min readjune 24, 2024

errors can sneak into your code, causing unexpected results. These errors stem from how computers represent numbers in binary, leading to limitations and rounding issues in calculations.

To manage these errors, you can use techniques like the function and be mindful of binary representation limitations. Understanding these concepts helps you write more accurate and reliable numerical code.

Floating-Point Errors and Precision Management

Sources of floating-point errors

Top images from around the web for Sources of floating-point errors
Top images from around the web for Sources of floating-point errors
  • Floating-point numbers represented with fixed number of bits leads to precision limitations
    • Binary fractions cannot precisely represent some decimal fractions (0.1 cannot be exactly represented in binary resulting in rounding errors)
  • Arithmetic operations on floating-point numbers can introduce and accumulate errors
    • Addition and subtraction of numbers with significantly different magnitudes can cause loss of precision
    • Repeated operations such as in loops can compound rounding errors
  • Comparing floating-point numbers directly for equality can lead to unexpected results due to precision limitations (two seemingly equal floating-point numbers may have slight differences)
  • can be affected by the accumulation of floating-point errors in complex calculations

Use of round() for precision

  • The round() function allows you to round a number to a specified number of decimal places
    • Syntax:
      round(number, [ndigits](https://www.fiveableKeyTerm:ndigits))
      where number is the value to be rounded and ndigits is the number of decimal places to round to (default is 0)
  • Rounding can help mitigate the impact of floating-point errors by limiting the precision of results (round(3.14159, 2) returns 3.14)
  • When comparing floating-point numbers, consider rounding them to a reasonable precision before comparison to avoid issues caused by slight differences in representation
  • The number of should be considered when rounding to maintain meaningful precision

Limitations of binary representation

  • Floating-point numbers stored in binary format have inherent limitations as not all decimal numbers can be exactly represented in binary leading to approximations and potential rounding errors
  • The standard defines the format for floating-point numbers
    • (32 bits) and (64 bits) are commonly used
    • The number of bits allocated for the determines the precision
  • Some decimal numbers such as 0.1 have repeating binary representations and cannot be exactly represented with a finite number of bits resulting in rounding errors when these numbers are stored or operated upon
  • Be aware of these limitations when working with floating-point numbers
    • Use appropriate rounding techniques and comparisons to mitigate the impact of precision errors
    • Consider using decimal or fraction modules for high-precision calculations when necessary

Representation and Precision Concepts

  • representation uses a fixed number of digits after the decimal point, offering an alternative to floating-point for some applications
  • (e.g., 1.23e5) is used to represent very large or small numbers in floating-point format
  • refers to the smallest representable positive number in a given floating-point system, crucial for understanding precision limits
© 2024 Fiveable Inc. All rights reserved.
AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.


© 2024 Fiveable Inc. All rights reserved.
AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.

© 2024 Fiveable Inc. All rights reserved.
AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.
Glossary
Glossary