# Problem in comparing Floating point numbers and how to compare them correctly?

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

`// C++ program to compare ` `// floating point numbers ` ` ` `#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; ` ` ` `} ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `double` `a = (0.3 * 3) + 0.1; ` ` ` `double` `b = 1; ` ` ` `compareFloatNum(a, b); ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to compare ` `// floating point numbers ` `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"` `); ` ` ` `} ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `double` `a = (` `0.3` `* ` `3` `) + ` `0.1` `; ` ` ` `double` `b = ` `1` `; ` ` ` `compareFloatNum(a, b); ` ` ` `} ` `} ` ` ` `// This code is contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

## Python

`# Python program to compare ` `# floating point numbers ` `def` `compareFloatNum(a, b): ` ` ` `if` `(a ` `=` `=` `b): ` ` ` `print` `(` `"The numbers are equal"` `) ` ` ` ` ` `else` `: ` ` ` `print` `(` `"The numbers are not equal"` `) ` ` ` `# Driver code ` ` ` `a ` `=` `(` `0.3` `*` `3` `) ` `+` `0.1` `b ` `=` `1` `compareFloatNum(a, b) ` ` ` `# This code is contributed by mohit kumar 29 ` |

*chevron_right*

*filter_none*

## C#

`// C# program to compare ` `// floating point numbers ` `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"` `); ` ` ` `} ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main(String[] args) ` ` ` `{ ` ` ` `double` `a = (0.3 * 3) + 0.1; ` ` ` `double` `b = 1; ` ` ` `comparefloatNum(a, b); ` ` ` `} ` `} ` ` ` `// This code is contributed by PrinciRaj1992 ` |

*chevron_right*

*filter_none*

**Output:**

The numbers are not equal

**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.

`// C++ program to compare ` `// floating point numbers ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `void` `printFloatNum(` `double` `a, ` `double` `b) ` `{ ` ` ` `// To print decimal numbers up to 20 digits ` ` ` `cout << setprecision(20); ` ` ` ` ` `cout << ` `"a is : "` `<< a << endl; ` ` ` `cout << ` `"b is : "` `<< b << endl; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `double` `a = (0.3 * 3) + 0.1; ` ` ` `double` `b = 1; ` ` ` `printFloatNum(a, b); ` `} ` |

*chevron_right*

*filter_none*

**Output:**

a is : 0.99999999999999988898 b is : 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++

`// C++ program to compare ` `// floating point numbers correctly ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `void` `compareFloatNum(` `double` `a, ` `double` `b) ` `{ ` ` ` ` ` `// Correct method to compare ` ` ` `// floating-point numbers ` ` ` `if` `(` `abs` `(a - b) < 1e-9) { ` ` ` `cout << ` `"The numbers are equal "` ` ` `<< endl; ` ` ` `} ` ` ` `else` `{ ` ` ` `cout << ` `"The numbers are not equal "` ` ` `<< endl; ` ` ` `} ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `double` `a = (0.3 * 3) + 0.1; ` ` ` `double` `b = 1; ` ` ` `compareFloatNum(a, b); ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to compare ` `// floating point numbers correctly ` `class` `GFG ` `{ ` ` ` `static` `void` `compareFloatNum(` `double` `a, ` `double` `b) ` `{ ` ` ` ` ` `// Correct method to compare ` ` ` `// floating-point numbers ` ` ` `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"` `); ` ` ` `} ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `double` `a = (` `0.3` `* ` `3` `) + ` `0.1` `; ` ` ` `double` `b = ` `1` `; ` ` ` `compareFloatNum(a, b); ` `} ` `} ` ` ` `// This code is contributed by Rajput-Ji ` |

*chevron_right*

*filter_none*

## Python3

`# Python program to compare ` `# floating ponumbers correctly ` ` ` `def` `compareFloatNum(a, b): ` ` ` ` ` `# Correct method to compare ` ` ` `# floating-ponumbers ` ` ` `if` `(` `abs` `(a ` `-` `b) < ` `1e` `-` `9` `): ` ` ` `print` `(` `"The numbers are equal "` `); ` ` ` `else` `: ` ` ` `print` `(` `"The numbers are not equal "` `); ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` `a ` `=` `(` `0.3` `*` `3` `) ` `+` `0.1` `; ` ` ` `b ` `=` `1` `; ` ` ` `compareFloatNum(a, b); ` ` ` `# This code is contributed by PrinciRaj1992 ` |

*chevron_right*

*filter_none*

## C#

` ` `// C# program to compare ` `// floating point numbers correctly ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `static` `void` `comparefloatNum(` `double` `a, ` `double` `b) ` `{ ` ` ` ` ` `// Correct method to compare ` ` ` `// floating-point numbers ` ` ` `if` `(Math.Abs(a - b) < 1e-9) ` ` ` `{ ` ` ` `Console.Write(` `"The numbers are equal "` ` ` `+` `"\n"` `); ` ` ` `} ` ` ` `else` ` ` `{ ` ` ` `Console.Write(` `"The numbers are not equal "` ` ` `+` `"\n"` `); ` ` ` `} ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main(String[] args) ` `{ ` ` ` `double` `a = (0.3 * 3) + 0.1; ` ` ` `double` `b = 1; ` ` ` `comparefloatNum(a, b); ` `} ` `} ` ` ` `// This code is contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

**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.

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: **DSA Self Paced**. Become industry ready at a student-friendly price.

## Recommended Posts:

- Can we use % operator on floating point numbers?
- C Program to Multiply two Floating Point Numbers
- Write a one line C function to round floating point numbers
- Precision of floating point numbers in C++ (floor(), ceil(), trunc(), round() and setprecision())
- Floating Point Operations & Associativity in C, C++ and Java
- How to count set bits in a floating point number in C?
- Convert a floating point number to string in C
- Rounding Floating Point Number To two Decimal Places in C and C++
- C++ Floating Point Manipulation (fmod(), remainder(), remquo() ... in cmath)
- Check if a given string is a valid number (Integer or Floating Point) in Java
- Comparing Intel i3, i5 and i7 processors
- Comparing Ruby with other programming languages
- Comparing enum members in Java
- Comparing String objects using Relational Operators in C++
- sizeof() for Floating Constant in C
- Rotation of a point about another point in C++
- std::string::compare() in C++
- Program to compare m^n and n^m
- How to compare two arrays in Java?
- Compare two Strings in Java

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.