When I add 0.1 to a float, it doesn’t always produce 1.1, sometimes it is 1.0999999999 or similar.
Why is this? Is this because bytes are power of two?
2 Answers
2This is how floats work, lots of numbers do not fit exactly into a floating point. In most cases you should not need to worry, there is the Decimal type that can be used if it’s really problem, but then it has it’s own issues to contend with. For more precision there is also the Double type, but again even then all decimal number may not fit perfectly, and I think Unity ends up using floats in the end anyway.
Basic theory from computer cience ![]()
http://en.wikipedia.org/wiki/Computer_number_format
unstable algorithms are amazing, execute this
[MenuItem("Game/Test")]
public static void Test()
{
var x = 0.1f;
for (var i = 0 ; i < 9 ; i ++) {
x = x * 100f - 9.9f;
Debug.Log(i+":"+x);
}
Debug.Log(x);
}
If you write by hand, x didn’t change. If you write in a computer, the final x will be the amazing value of “5.343345E+09”
In general case is not a problem. But you need to know to identify when its happens.
Could be floating point imprecision. Are you using this in an objects vector position, this will often happen when you do? Also write 1 as 1.0, this may help... If the "1" and "0.1" are stored in vars make sure both vars are of type float, again no promises but it can't hurt to try...
– MrSoadActually, it's for a volume display in an options menu, and I'm using C# btw.
– BDX777I get different results when I reach zero and then start adding up. Also how come a float can't support 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, or 0.9 sometimes? Suddenly floats just sound like garbage and I suddenly want to stop using them but Unity has a lot of things that use floats. Oh well...
– BDX777By volume display do you mean some sort of progress bar type thing? If so rather than adding parts to fill it, you could stretch the dimensions of one image depending upon a percentage calculation of the total max bar size. This way your floating point imprecision will be negligible(never more than an invisible fraction off) rather than cumulative. Sorry if I have misunderstood what you mean by volume display :s
– MrSoad