In this article, we will see what is the problem in comparing floating-point numbers and we will discuss the correct way to compare two floating-point numbers.
What is the problem in comparing Floating-Point Numbers usually?
Let us first compare two floating-point numbers with the help of relational operator (==).
Example: Using “==” for comparison
CPP
#include <bits/stdc++.h>
using namespace std;
void compareFloatNum( double a, double b)
{
if (a == b) {
cout << "The numbers are equal"
<< endl;
}
else {
cout << "The numbers are not equal"
<< endl;
}
}
int main()
{
double a = (0.3 * 3) + 0.1;
double b = 1;
compareFloatNum(a, b);
}
|
Java
class GFG
{
static void compareFloatNum( double a, double b)
{
if (a == b)
{
System.out.print( "The numbers are equal" + "\n" );
}
else
{
System.out.print( "The numbers are not equal" + "\n" );
}
}
public static void main(String[] args)
{
double a = ( 0.3 * 3 ) + 0.1 ;
double b = 1 ;
compareFloatNum(a, b);
}
}
|
Python
def compareFloatNum(a, b):
if (a = = b):
print ( "The numbers are equal" )
else :
print ( "The numbers are not equal" )
a = ( 0.3 * 3 ) + 0.1
b = 1
compareFloatNum(a, b)
|
C#
using System;
class GFG
{
static void comparefloatNum( double a, double b)
{
if (a == b)
{
Console.Write( "The numbers are equal" + "\n" );
}
else
{
Console.Write( "The numbers are not equal" + "\n" );
}
}
public static void Main(String[] args)
{
double a = (0.3 * 3) + 0.1;
double b = 1;
comparefloatNum(a, b);
}
}
|
Javascript
<script>
function compareFloatNum(a,b)
{
if (a == b)
{
document.write( "The numbers are equal" + "<br>" );
}
else
{
document.write( "The numbers are not equal" + "<br>" );
}
}
let a = (0.3 * 3) + 0.1;
let b = 1;
compareFloatNum(a, b);
</script>
|
Output:
The numbers are not equal
Time complexity of this program is O(1), as it only performs a comparison between two floating point numbers.
The space complexity is also O(1), as the program uses only a constant amount of memory for storing the two floating point numbers and a few local variables used for the comparison.
Why does this problem occur?
In the case of floating-point numbers, the relational operator (==) does not produce correct output, this is due to the internal precision errors in rounding up floating-point numbers.
In the above example, we can see the inaccuracy in comparing two floating-point numbers using “==” operator. The two numbers ‘a’ and ‘b’ are equal ( as (0.3 * 3) + 0.1 = 1 ) but the program results in an incorrect output.
Let’s take a closer look at the numbers in the next snippet.
CPP
#include <bits/stdc++.h>
using namespace std;
void printFloatNum( double a, double b)
{
cout << setprecision(20);
cout << "a is : " << a << endl;
cout << "b is : " << b << endl;
}
int main()
{
double a = (0.3 * 3) + 0.1;
double b = 1;
printFloatNum(a, b);
}
|
Python3
def printFloatNum(a, b):
print ( "a is : %.20f" % a)
print ( "b is : %.20f" % b)
if __name__ = = "__main__" :
a = ( 0.3 * 3 ) + 0.1
b = 1
printFloatNum(a, b)
|
Javascript
function printFloatNum(a, b) {
console.log(`a is : ${a.toFixed(20)}`);
console.log(`b is : ${b.toFixed(20)}`);
}
let a = (0.3 * 3) + 0.1;
let b = 1;
printFloatNum(a, b);
|
Java
import java.util.*;
public class CompareFloatingPointNumbers {
public static void printFloatNum( double a, double b) {
System.out.println(String.format( "a is : %.20f" , a));
System.out.println(String.format( "b is : %.20f" , b));
}
public static void main(String[] args) {
double a = ( 0.3 * 3 ) + 0.1 ;
double b = 1 ;
printFloatNum(a, b);
}
}
|
Output:
a is : 0.99999999999999988898
b is : 1
Time complexity:
The program has a constant time complexity, as it only performs a fixed set of operations and does not depend on the input size. Therefore, the time complexity is O(1).
Space complexity:
The program uses a fixed amount of memory for the double variables a and b, as well as for the output printed to the console. Therefore, the space complexity is also O(1).
Now we can see the internal rounding error in floating-point numbers. Number ‘a’ is not correctly rounded up to 1,
there is an internal error in rounding up, a very small error but makes a huge difference when we are comparing the numbers.
How to compare floating-point numbers correctly?
If we do have to compare two floating-point numbers then rather than using “==” operator we will find the absolute difference between the numbers (which if were correctly represented, the difference would have been 0) and compare it with a very small number 1e-9 (i.e 10^-9, this number is very small) and if the difference is less than this number, we can safely say that the two floating-point numbers are equal.
Example:
C++
#include <bits/stdc++.h>
using namespace std;
void compareFloatNum( double a, double b)
{
if ( abs (a - b) < 1e-9) {
cout << "The numbers are equal "
<< endl;
}
else {
cout << "The numbers are not equal "
<< endl;
}
}
int main()
{
double a = (0.3 * 3) + 0.1;
double b = 1;
compareFloatNum(a, b);
}
|
Java
class GFG
{
static void compareFloatNum( double a, double b)
{
if (Math.abs(a - b) < 1e- 9 )
{
System.out.print( "The numbers are equal "
+ "\n" );
}
else
{
System.out.print( "The numbers are not equal "
+ "\n" );
}
}
public static void main(String[] args)
{
double a = ( 0.3 * 3 ) + 0.1 ;
double b = 1 ;
compareFloatNum(a, b);
}
}
|
Python3
def compareFloatNum(a, b):
if ( abs (a - b) < 1e - 9 ):
print ( "The numbers are equal " );
else :
print ( "The numbers are not equal " );
if __name__ = = '__main__' :
a = ( 0.3 * 3 ) + 0.1 ;
b = 1 ;
compareFloatNum(a, b);
|
C#
using System;
class GFG
{
static void comparefloatNum( double a, double b)
{
if (Math.Abs(a - b) < 1e-9)
{
Console.Write( "The numbers are equal "
+ "\n" );
}
else
{
Console.Write( "The numbers are not equal "
+ "\n" );
}
}
public static void Main(String[] args)
{
double a = (0.3 * 3) + 0.1;
double b = 1;
comparefloatNum(a, b);
}
}
|
Javascript
<script>
function compareFloatNum(a,b)
{
if (Math.abs(a - b) < 1e-9)
{
document.write( "The numbers are equal "
+ "<br>" );
}
else
{
document.write( "The numbers are not equal "
+ "<br>" );
}
}
let a = (0.3 * 3) + 0.1;
let b = 1;
compareFloatNum(a, b);
</script>
|
Output:
The numbers are equal
This code results in the correct output, so whenever two floating point numbers are two be compared then rather than using “==” operator, we will use the above technique.