I feel like there is a simple solution here, but I just can’t seem to find it. I have a constant literal 0.05. Throughout the execution of my program, I’ve found that the double constant is being treated not as 0.05, but as 0.049999…etc.
Why is this happening, and how can I prevent it?
Thanks in advance for your help!
-Matt
PS – I would post a snapshot to give more information, but apparently I can’t because I’m a new user… Rats.
There is no binary representation of 0.05, because 1/20 is not a power of two, so floating point (double precision) representation only approximates it. 0.5, on the other hand, can be represented exactly.
If v is your literal, then (v == 0.05) should be ‘true’