Gasan Gouseinov wrote:It might if the decimal fractions was the matter. But here I get different values because of adding an integer value, e.g. one.
Unfortunately, this is an issue with all floating points -- you don't get infinite precision. And it doesn't matter whether you add an integer to it, or not. You can get this even if you add one -- as it depends on both operands of the addition.
A possible solution -- instead of doing a direct comparison, take a difference, and check if the result is within a certain tolerance from zero.
Floating-point arithmetic is intended for engineers. When I was an undergraduate, there was this joke:
What is the difference between engineers and mathematicians?
If you ask a mathematician what 2 × 2 means, he says 4.
If you ask an engineer the same question, he gets his slide rule out (this was several years ago ) and says, "Err, 2, multiply, err, umm, 2, . . . . 3.99"
"Well, that's 4 near as makes no difference."
Engineers can cope with the fact that you can write 0.1 and really get 0.09999999999998735872873956243562195. When you try exact comparisons (by the way, you were not adding an integer, but a double 1.0), your 0.66 might have turned to 1.6600000000000000003635346857325 or 1.65999999999999999973456324875435. But it probably wasn't exactly 0.66 to start with.