How to Compare Float and Double While Accounting for Precision Loss?
Last Updated :
29 Jan, 2024
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.
- Comparison with Epsilon Value
- 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;
}
OutputGiven 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;
}
OutputGiven 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.
Similar Reads
How Do I Print a Double Value with Full Precision Using cout? The double value in C++ has a precision of up to 15 digits but while printing it using cout, it only prints six significant digits. In this article, we will learn how to print the double value with full precision. For Example, Input: double var = 12.3456789101112 Output: var = 12.3456789101112Print
2 min read
Abnormal behavior of floating point and double values Float is a 32 bit IEEE 754 single-precision Floating Point Number 1 bit for the sign, (8 bits for the exponent, and 23* for the value), i.e. float has 7 decimal digits of precision. Double is a 64 bit IEEE 754 double precision Floating Point Number (1 bit for the sign, 11 bits for the exponent, and
6 min read
How to Round a Double to an int in C++? In C++, the double data type is used to store floating-point numbers with double precision. In this article, we will learn how to round a double value to an int in C++. Example: Input: double val= 2.6 Output: Integer for double value 2.5 is: 3 Rounding a Double to an Int in C++To round a double valu
2 min read
How to Convert Float to int in C++? In C++, the float is a short term for floating point value that is used to define numeric values with decimal points. The floating point numbers can be converted to integers using the process called type casting. In this article, we will learn how to convert a float value to an int in C++. Example:
2 min read
How to Catch Floating Point Errors in C++? In C++, a part of the code that may throw exceptions is enclosed in try-and-catch blocks to handle them when they arise. We can also use try...catch to catch floating point errors but it involves a bit different approach in comparison to catching standard exceptions. In this article, we will look at
2 min read
Why floating-point values do not represent exact value The floating-point numbers serve as rough approximations of mathematical real numbers. They do not represent the exact value. For this reason, we compare the arithmetic results of float variables with a minimum tolerance value. Example: C++ // C++ program to illustrate the // floating point values #
4 min read