Floating point precision in C++
I've heard that floating point numbers can sometimes be imprecise in C++. Can you explain why this happens and how to handle it?
Floating point numbers in C++ (and most other languages) are often approximations due to the way they're represented in binary. This can lead to some surprising results.
In C++, float
and double
types are typically implemented as IEEE 754 floating point numbers. These numbers are represented in binary with a fixed number of bits for the mantissa (the fraction part) and the exponent.
Because of this fixed size representation, not all decimal numbers can be represented exactly in binary. For example, 0.1 in decimal is a repeating fraction in binary (just like how 1/3 is a repeating decimal in base 10). The binary representation of 0.1 is cut off at some point, leading to a slight inaccuracy.
Here's a famous example of this inaccuracy:
#include <iostream>
int main() {
double a = 0.1;
double b = 0.2;
double c = a + b;
std::cout << std::boolalpha;
// Outputs "false"
std::cout << (c == 0.3);
}
false
In this case, a + b
is not exactly equal to 0.3 due to the imprecision in the binary representations of 0.1 and 0.2.
To handle this, you usually want to avoid comparing floating point numbers directly for equality. Instead, check if the difference between the numbers is less than some small threshold value (often called an epsilon):
#include <iostream>
#include <cmath>
bool almost_equal(double a, double b,
double epsilon = 1e-8) {
return std::abs(a - b) < epsilon;
}
int main() {
double a = 0.1;
double b = 0.2;
double c = a + b;
std::cout << std::boolalpha;
// Outputs "true"
std::cout << almost_equal(c, 0.3);
}
true
Another way to handle this is to use integer types for arithmetic when possible (if you're dealing with fixed-point numbers, for example), or to use a decimal library for financial calculations where exact decimal representations are crucial.
It's also worth noting that double
has more precision than float
(usually 64 bits vs 32 bits), so it can represent numbers more accurately. But it still suffers from the same fundamental issue of not being able to exactly represent all decimal numbers.
Variables, Types and Operators
Learn the fundamentals of C++ programming: declaring variables, using built-in data types, and performing operations with operators