I need to check is my complex number a zero. How can I do it for floating point numbers?
You can compare it with a floating point literal with value of 0. You cannot use an integer literal with std::complex<double>
. Example:
a == 0.0
Can I use something like ...
What you've shown doesn't compare whether the complex number is zero; it compares whether the complex number is near zero. This can be a reasonable operation for example in the case the number is result of a calculation with a known margin of error, and you want to know whether the result is within the margin. But it is a separate operation
Whether that is a good way to compare if the number is near zero depends on use case. For example, epsilon
is not necessarily the best threshold of "near". Another thing that you might consider is whether you should compare std::abs(a)
to the threshold instead of comparing the components separately i.e. whether you should use euclidean distance instead of manhattan distance.