
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
What is the most effective way for float and double comparison in C/C++?
Here we will see how to compare two floating point data or two double data using C or C++. The floating point / double comparison is not similar to the integer comparison.
To compare two floating point or double values, we have to consider the precision in to the comparison. For example, if two numbers are 3.1428 and 3.1415, then they are same up to the precision 0.01, but after that, like 0.001 they are not same.
To compare using this criteria, we will find the absolute value after subtracting one floating point number from another, then check whether the result is lesser than the precision value or not. By this we can decide that they are equivalent or not.
Example
#include <iostream> #include <cmath> using namespace std; bool compare_float(float x, float y, float epsilon = 0.01f){ if(fabs(x - y) < epsilon) return true; //they are same return false; //they are not same } bool compare_float(double x, double y, double epsilon = 0.0000001f){ if(fabs(x - y) < epsilon) return true; //they are same return false; //they are not same } int main() { float x, y; x = 22.0f/7.0f; y = 3.1415f; if(compare_float(x, y)){ cout << "They are equivalent" << endl; } else { cout << "They are not equivalent" << endl; } if(compare_float(x, y, 0.001f)){ cout << "They are equivalent" << endl; } else { cout << "They are not equivalent" << endl; } double a, b; a = 2.03547415; b = 2.03547428; if(compare_float(a, b)){ cout << "They are equivalent" << endl; } else { cout << "They are not equivalent" << endl; } if(compare_float(a, b, 0.000001f)){ cout << "They are equivalent" << endl; } else { cout << "They are not equivalent" << endl; } }
Output
They are equivalent They are not equivalent They are not equivalent They are equivalent
- Related Articles
- Comparison of double and float primitive types in C#
- Comparison of double and float primitive types in Java\n
- Float and Double in C
- What is the difference between a float, double and a decimal in C#?
- Difference between float and double in C/C++
- Effective Way for Project Status Communication
- Assigning an integer to float and comparison in C/C++
- How to compare float and double in C++?
- What is the most effective approach to master Canva?
- Difference Between Float and Double
- Comparison of Float in Java
- Checking if a double (or float) is NaN in C++
- Round float and double numbers in Java
- Difference between float and double in Arduino
- What are the most effective ways of achieving concurrency in iOS?

Advertisements