I have this little function here that converts a range into another range.
When i convert a range lets say between 10-20 into 0-10 or vs. theres no problem.
The problem begins when i try to convert it into smaller ranges when ranges start is below 0 and the resulting number is supposed to be exact 0.0f.
Forexample if i convert it between -1 to 1, result is a funny float number. This happens between -2 to 2, -3 to 3 and -4 to 4
but it is back to normal again after a range of -5 to 5 only when the result should be 0.0f(other numbers are correct).
Question is, how do i improve on it and the resulting number is exact 0.0f ?
What do you mean buy “funny float number”? Float number’s do have a tendency to give weird results (for example, 1.0 == 10.0/10.0 isn’t always true, as suggested by the documentation), so it could be that.
Floating point numbers are stored as very close approximations to their given values for efficiency usually. So comparing floats with == is usually a no-no in general programming as it may not always be true, especially for smaller numbers I believe. So when your testing against 0.0f, the number may be stored as 0.000000001f in memory due to the approximation. Therefore, you end up comparing a 0.0000001f with a 0.0f, which obviously won’t pass as true.
Not sure the best method to get around this, usually test < 0.000001f and > -0.000001f should be enough.