No wonder - it is floating point arithmetic. Depending on the order in which the calculations were made, the final result may differ slightly from the expected. Plus, rounding takes effect - it is not necessary that the computer will, say, 32 bit floating point numbers not be converted to 64 or 80 bit, read the result, and then overtake it back.
In this case, if you need to understand whether we received a zero, then you just need to enter the condition that result < epsilon
. epsilon is some very small number, which will tell us about the indistinguishability of floating numbers. It can be picked up either in a practical way (considering the accuracy of the calculations that we need), or it can already be prepared using the functions of the language library or from header files.
See the article machine epsilon