@gelatine
i believe that should work if you add an f to it. |
The only thing the f does is to say that it is a float, so that doesn't fix the problem.
The value of a float goes like this:
FloatValue = DesiredValue plus or minus a very small value.
So float 0.1 equals something like 0.099999997. Multiplying it by 10.0 gives 0.99999997 which is not equal to 1.0.
To fix this you need an EPSILON value. EPSILON is the biggest number so that 1.0 + EPSILON is still equal to 1.0. The EPSILON needs to be scaled to fit the numbers you have, ie if your number is 1000.0 then you need to have MyEpsilon = 1000.0 * EPSILON. There are different EPSILON's for doubles and 32 bit and 64 bit.
To check whether something equals a value, you check that something is between value + EPSILON, and value - EPSILON.
This all happens because of the way floats are represented. They are represented as binary fractions, so 1.11 in binary is equal to 1.0 + 0.5 + 0.25. Doing this means that most numbers (up to a limit of significant figures) can be closely represented.
It isn't worthwhile to find out in advance which numbers can represented exactly, so one really has to do the comparisons above.
EssGeEich's method is a good one, probably easier than what I just explained.