# Tag Info

1

The reason why this is happening is because 0.1 and 0.2 cannot be accurately represented in binary, so when you try to store 0.2 in a float variable, you are not actually storing 0.2, you are storing 0.20000021 or 0.19999932 or something like that. There are a few ways to resolve this problem: Stay away from equality comparison between floats. You will ...

5

Please consult article by John D. Cook: Five Tips for Floating Point Programming. You can solve the issue by specifying a tolerance (from above article): double tolerance = ... if (fabs(x - y) < tolerance) {...}

2

You can't. Every single floating point operation doesn't give the result that you expect, but a result that is more or less close to the expected result (unless you have a complete understanding of floating point maths and can provide mathematical proofs of properties). When using "float", it's not very close. When using "double", it's a lot closer. ...

1

use an int and multiply all constant values by 10.

Top 50 recent answers are included