Float with Scientific Notation breaks simple addition?

Ok, I feel like I’m going nuts. Is it possible that floats above 10,000,000 don’t support simple addition?

I’ve been working on a bug for an hour: it seems that if a float is above 10,000,000, you literally can’t add 1 to it.

I made the following test code to show the problem:

Debug.Log("=== TEST 1");
float bigNumber = 90000000;
Debug.Log(bigNumber);
Debug.Log(bigNumber.ToString("N0"));
bigNumber += 1;
Debug.Log(bigNumber.ToString("N0"));
bigNumber = bigNumber + 1;
Debug.Log(bigNumber.ToString("N0"));

Debug.Log("=== TEST 2");
float smallNumber = 90000;
Debug.Log(smallNumber);
Debug.Log(smallNumber.ToString("N0"));
smallNumber += 1;
Debug.Log(smallNumber.ToString("N0"));
smallNumber = smallNumber + 1;
Debug.Log(smallNumber.ToString("N0"));

And here’s the result I get from running that code:

> === TEST 1
> 9E+07
> 90,000,000
> 90,000,000
> 90,000,000
> === TEST 2
> 90000
> 90,000
> 90,001
> 90,002

Is that the expected behavior? You simply can’t add 1 to a float with 8 digits or more?

ha!

excellent diagnostic debugging.
you are correct.
it’s not the scientific notation which is causing accuracy problems, but the size of the number. floats have 24 bits for the “mantissa”, with the rest of bits being the exponent. 24 binary bits can represent up to about 16 million, and above that the numbers are no longer precise.
for higher accuracy, use a double.

this is an aspect of floats in any language, not just c# / unity.