Open In App

How to Compare Float and Double While Accounting for Precision Loss?

Last Updated : 29 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In C++ programming, real numbers are represented in a limited amount of memory so accuracy loss is a frequent worry when dealing with floating-point and double numbers in C++ making direct comparison of two such values unreliable. In this article, we will discuss how to compare two float or double values while accounting for the precision loss.

Problem with Direct Comparison

Using direct equality tests (==) to compare two floating point or double numbers may provide surprising results. Even if two calculations are mathematically equivalent, the results might differ slightly in their least significant digits due to rounding errors.

Methods to Compare Two Float or Double

There are two methods to compare two float or double values while accounting for precision loss.

  1. Comparison with Epsilon Value
  2. Relative Comparision

1. Epsilon Value

It is advised to use an epsilon, or tiny number, to provide a tolerance threshold for comparisons when comparing float and double values. This tolerance makes up for the imprecision that floating-point encoding introduces to use this check if the absolute difference between them is less than a small number, known as epsilon (ε)or not. This is generally useful when the magnitude of the two values is not very large.

Example

The below example demonstrates the method to compare float and double numbers using epsilon value for threshold.

C++




// C++ program to demonstrate the method to compare float
// and double numbers using epsilon value for threshold.
  
#include <cmath>
#include <iostream>
using namespace std;
  
// function to check if the given numbers are equal or not
// using epsilon value
bool isEqual(double a, double b, double epsilon)
{
    return fabs(a - b) < epsilon;
}
  
int main()
{
  
    // initializing two numbers
    double num1 = 0.3 * 3 + 0.1;
    double num2 = 1.0;
  
    // checking if numbers are equal or not
    if (isEqual(num1, num2, 1e-6)) {
        cout << "Given numbers are equal." << endl;
    }
    else {
        cout << "Given numbers are not equal." << endl;
    }
    return 0;
}


Output

Given numbers are equal.

2. Relative Comparison

Relative comparison is used when working with values of varying magnitudes, take into consideration using relative comparison. This method works particularly well when comparing numbers whose scales could differ greatly.

Example

The below example demonstrates the method to compare float and double numbers by the relative comparison method.

C++




// C++ program to demonstrate the method to compare float
// and double numbers by relative comparison method.
  
#include <cmath>
#include <iostream>
using namespace std;
  
// function to check if the given two numbers are relatively
// equal
bool checkRelativelyEqual(double a, double b,
                          double relativeEpsilon)
{
    double diff = fabs(a - b);
    a = fabs(a);
    b = fabs(b);
    double largest = (b > a) ? b : a;
  
    return diff <= largest * relativeEpsilon;
}
  
int main()
{
    // initializing two double numbers to be compared
    double num1 = 1000000.01;
    double num2 = 1000000.02;
    // check if both numbers are relatively equal or not
    if (checkRelativelyEqual(num1, num2, 1e-6)) {
        cout << "Given Numbers are relatively equal."
             << endl;
    }
  
    // if the numbers are not equal then print this
    else {
        cout << "Given Numbers are not relatively equal."
             << endl;
    }
  
    return 0;
}


Output

Given Numbers are relatively equal.

Conclusion

It is important to take into account the accuracy loss that comes with floating-point representations when comparing float and double numbers in fields such as game development, weather prediction and many more where the minor inaccuracy may result in different results.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads