Just my 5 cents in addition to other answers:

It's not a good idea to use the operator == with floating-point operands. That's right, I don't know the situations where it can be useful at all. Even with floating-point values representing numbers with zero fractional part, you should not use it; instead you can round a value and typecast to one of the integer types, and even that is rarely needed. Floating-point values represent

*approximate* values. If is impossible to represent "all" real number is such a finite-state machine as computer, even a single real number, generally, contains infinite volume of information. A typical comparison, in case when it makes sense, may look like:

#include <cmath>
double margin = double myValue = double testValue = if (std::abs(myValue - testValue) < margin)

[EDIT]

After I submitted this post, I found that this code sample is essentially the same as in Solution 1. Crediting that, I decided not to remove mine, maybe it may provide some extra clarification.

Sometimes you need a different criterion, "relative closeness". For example

double margin = 0.001; if (std::abs(myValue - testValue) * 2 <
margin * (myValue + testValue))

It is useful if the compared values are supposed to be "big" values with "small" difference between them, so the criterion is to check if the relative difference is "small enough".

[END EDIT]

See also:

std::abs(float), std::fabs - cppreference.com[

^].

The principles of floating-point calculation have been analyzed and explained to the practical software developers quite deeply in the classical

Donald Knuth's book

The Art of Computer Programming.

—SA