
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to compare float and double in C++?
In C++, floating-point numbers are used to represent decimal values. The most commonly used floating-point types are float
and double
. These data types differ in their size, precision, and use cases. Understanding how to use and compare them correctly is important for accurate computations.
Understanding Float and Double Precision
The float
is a 32-bit single-precision floating-point type that can store approximately 7 decimal digits, while double
is a 64-bit double-precision type that offers about 15 to 16 digits of precision. Due to these differences, double is generally preferred for higher accuracy in scientific and financial calculations.
Example: Printing Float and Double Values
This example demonstrates the difference in precision when storing the same decimal number using float and double:
#include<iostream> using namespace std; int main() { float a = 3.1415926535f; // 'f' suffix denotes float double b = 3.1415926535; cout << "Float: " << a << endl; cout << "Double: " << b << endl; return 0; }
When you run the above code, the output will be:
Float: 3.14159 Double: 3.1415926535
Comparing Float and Double Using EPSILON
Floating-point numbers cannot always be compared reliably using the ==
operator due to rounding errors and precision limitations. A better approach is to use a small threshold value known as EPSILON
to check if the absolute difference between two numbers is very small - indicating they are "close enough."
Example: Using EPSILON for Safe Comparison
The following example demonstrates how to compare float and double values using EPSILON:
#include<iostream> #include<cmath> using namespace std; #define EPSILON 0.000001f bool areSameFloat(float a, float b) { return fabs(a - b) < EPSILON; } bool areSameDouble(double a, double b) { return fabs(a - b) < EPSILON; } int main() { float fa = 1.005f; float fb = 1.006f; cout << "Float comparison:" << endl; cout << "fa vs fa: " << areSameFloat(fa, fa) << endl; cout << "fa vs fb: " << areSameFloat(fa, fb) << endl; double da = 1.005; double db = 1.006; cout << "\nDouble comparison:" << endl; cout << "da vs da: " << areSameDouble(da, da) << endl; cout << "da vs db: " << areSameDouble(da, db) << endl; return 0; }
When you run the above code, the output will be:
Float comparison: fa vs fa: 1 fa vs fb: 0 Double comparison: da vs da: 1 da vs db: 0
Float vs Double in C++: Feature Comparison Table
The table below compares key aspects of float
and double
in C++:
Feature/Aspect | float | double |
---|---|---|
Size | 4 bytes (32 bits) | 8 bytes (64 bits) |
Precision | Single precision (~7 digits) | Double precision (~15-16 digits) |
Default Literal Type | No (requires suffix like 3.14f ) |
Yes (3.14 is double by default) |
Suffix Required | Yes (use f or F ) |
No suffix required |
Memory Consumption | Less | More |
Performance | Faster on some systems due to smaller size | Slower but more accurate |
Precision Loss | Higher chance | Lower chance |
Declaration Example | float pi = 3.14159f; |
double pi = 3.14159265358979; |
Conclusion
Choosing between float
and double
in C++ depends on your needs for precision and memory usage. While float
is faster and uses less memory, double
is more accurate and better suited for complex numerical computations.