Home > Backend Development > C++ > Why does C floating-point arithmetic produce unexpected results, and how can we mitigate these precision anomalies?

Why does C floating-point arithmetic produce unexpected results, and how can we mitigate these precision anomalies?

Barbara Streisand
Release: 2024-11-04 21:38:02
Original
827 people have browsed it

Why does C   floating-point arithmetic produce unexpected results, and how can we mitigate these precision anomalies?

C Floating-Point Precision Anomaly and Resolution

Despite their widespread use, floating-point numbers in C exhibit limitations in precision. Consider the following code snippet:

<code class="cpp">double a = 0.3;
std::cout.precision(20);
std::cout << a << std::endl;
Copy after login

This code outputs 0.2999999999999999889 instead of the expected 0.3. This discrepancy occurs because the variable a is not stored exactly as a 0.3 double-precision floating-point number but rather an approximation of it due to the finite representation of floating-point values.

A more surprising behavior arises when a is repeatedly added 50 times:

<code class="cpp">double a, b;
a = 0.3;
b = 0;
for (char i = 1; i <= 50; i++) {
  b = b + a;
};
std::cout.precision(20);
std::cout << b << std::endl;
Copy after login

This code surprisingly outputs 15.000000000000014211 instead of 15.0. This is because each addition accumulates the approximation error, leading to an accumulated error greater than the original precision.

Resolving the Precision Anomaly

To obtain precise results, it is crucial to avoid setting the output precision greater than the available digits for the numeric type. This can be achieved using the std::numeric_limits class:

<code class="cpp">#include <iostream>
#include <limits>
int main()
{
        double a = 0.3;
        std::cout.precision(std::numeric_limits<double>::digits10);
        std::cout << a << std::endl;
        double b = 0;
        for (char i = 1; i <= 50; i++) {
                  b = b + a;
        };
        std::cout.precision(std::numeric_limits<double>::digits10);
        std::cout << b << std::endl;
}</code>
Copy after login

This code uses the digits10 member function to set the precision to the maximum available digits for a double-precision floating-point number, which is 15 digits for double.

Limitations of Floating-Point Arithmetic

While the provided solution minimizes the precision error, it is important to recognize that floating-point arithmetic has inherent limitations. If a loop involves thousands of iterations, even with the precision set appropriately, the accumulated error may become significant. In such scenarios, alternative data types, such as fixed-point numbers or rational numbers, may be more suitable for maintaining exact values.

The above is the detailed content of Why does C floating-point arithmetic produce unexpected results, and how can we mitigate these precision anomalies?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template