Be careful with floats!
Let's consider a simple example in C where we sum floating-point numbers and compare the result to what we might expect. This example will illustrate how floating-point precision issues can lead to unexpected results.
We will sum the floating point number 0.1
ten times and compare the result to 1.0
. Intuitively, one might expect that summing 0.1
ten times should give exactly 1.0
.
#include <stdio.h>
int main() {
double sum = 0.0;
for (int i = 0; i < 10; i++) {
sum += 0.1;
}
if (sum == 1.0) {
printf("Sum is exactly 1.0\n");
} else {
printf("Sum is not exactly 1.0, it is %f\n", sum);
}
return 0;
}
The code sums 0.1
ten times and checks if the result is 1.0
. Due to the way floating point numbers are represented, the sum might not be exactly 1.0
.
The output will likely indicate that the sum is not exactly 1.0
. The actual sum printed will be very close to 1.0
, but not exactly 1.0
, due to the precision issues with floating-point arithmetic.
Comparing floating point values in C can be tricky due to the way these values are represented and handled in computer systems. Here's an overview of the issues and best practices:
Why Not Compare Floating Points Directly?
Precision Issues: Floating point numbers are represented in a format that cannot precisely represent all decimal numbers. This leads to small rounding errors. For example, the result of a computation like
0.1 + 0.2
might not be exactly0.3
.Representation: Floating points are stored in a binary format that can result in unexpected differences even for seemingly identical values.
What Can Go Wrong?
False Mismatches: Due to precision issues, two values that should be equal might not be the same in their binary representation. This can lead to incorrect comparison results.
Inaccurate Computations: Operations involving floating points can accumulate errors, leading to significant discrepancies over time.
How to compare:
Use a Tolerance Value (Epsilon): Instead of checking for equality, check if the absolute difference between the numbers is less than a small threshold value.
#include <math.h> #define EPSILON 1e-9 int floatCompare(double a, double b) { return fabs(a - b) < EPSILON; }
Relative Comparison: Sometimes it's better to use a relative difference comparison, especially when dealing with very large or very small numbers.
int relativeCompare(double a, double b) { return fabs(a - b) <= EPSILON * fmax(fabs(a), fabs(b)); }
Corner Cases and Sample Code:
Zero Comparison: Directly comparing to zero can be misleading due to precision errors.
if (fabs(myFloat) < EPSILON) { // Treat as zero }
Large Numbers: For very large numbers, the epsilon value might need to be scaled accordingly.
Denormalized Numbers: Very small floating point numbers can become 'denormalized', leading to a loss of precision. Be cautious when comparing values close to the limits of floating-point representation.
NaN and Infinity: Be aware that floating points can represent 'Not a Number' (NaN) and infinity. Comparisons involving these special values need special handling.
if (isnan(myFloat) || isinf(myFloat)) { // Special handling for NaN and infinity }
Accumulated Errors in Loops: Repeated operations can accumulate errors. Be careful when comparing the results of iterative processes.
Subscribe to my newsletter
Read articles from Jyotiprakash Mishra directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Jyotiprakash Mishra
Jyotiprakash Mishra
I am Jyotiprakash, a deeply driven computer systems engineer, software developer, teacher, and philosopher. With a decade of professional experience, I have contributed to various cutting-edge software products in network security, mobile apps, and healthcare software at renowned companies like Oracle, Yahoo, and Epic. My academic journey has taken me to prestigious institutions such as the University of Wisconsin-Madison and BITS Pilani in India, where I consistently ranked among the top of my class. At my core, I am a computer enthusiast with a profound interest in understanding the intricacies of computer programming. My skills are not limited to application programming in Java; I have also delved deeply into computer hardware, learning about various architectures, low-level assembly programming, Linux kernel implementation, and writing device drivers. The contributions of Linus Torvalds, Ken Thompson, and Dennis Ritchie—who revolutionized the computer industry—inspire me. I believe that real contributions to computer science are made by mastering all levels of abstraction and understanding systems inside out. In addition to my professional pursuits, I am passionate about teaching and sharing knowledge. I have spent two years as a teaching assistant at UW Madison, where I taught complex concepts in operating systems, computer graphics, and data structures to both graduate and undergraduate students. Currently, I am an assistant professor at KIIT, Bhubaneswar, where I continue to teach computer science to undergraduate and graduate students. I am also working on writing a few free books on systems programming, as I believe in freely sharing knowledge to empower others.