Answers for "int conversion gives bad results !"
http://answers.unity.com/questions/1725860/int-conversion-gives-bad-results.html
The latest answers for the question "int conversion gives bad results !"Answer by Bunny83
http://answers.unity.com/answers/1725952/view.html
Even others have answered the question already I'd like to mention a few things.
<br>
First of all if you are surprised by this result I highly recommend to watch [this numberphile video][1]. Once you understand that floating point numbers will never really be accurate except for some special numbers you should always keep that in mind when you want to convert it into an integer.
<br>
Note that casting a floating point value to an integer will actually not round down but rounds towards 0. So a value of "1.5" is casted to "1" but a value of "-1.5" is casted to "-1" (so rounded up). Unity provides several methods to perform rounding. First there's Mathf.Floor and Mathf.Ceil. Those are the equivalent functions from actual math. Floor will always round down while Ceil will always round up. Though we also have the Mathf.Round method which rounds down if the value is below 0.5 and rounds up if it's above. 0.5 is a special case I won't go into detail here.
<br>
In addition to those 3 methods Unity also provides another set of those methods which directly perform an int conversion for you. Specifically Mathf.RoundToInt, Mathf.FloorToInt and Mathf.CeilToInt. If you perform a floating point calculation where the result would be very close to a whole number, you should always use RoundToInt to be on the safe side. Because if the value of 24.999999 or 25.000001 both would come out at 25
int val = Mathf.RountToInt(5f / 0.2f);
<br>
Unity, C# and almost all other programming languages just use the hardware implementation of floating point numbers as it's defined in the IEEE 754 standard. It specifically defines 2 types: 32 bit (single precision floating point numbers) and 64 bit (double precision floating point numbers). In .NET they are called System.Single and System.Double. However C# has two alias names for those types we all know: float (==System.Single) and double (System.Double).
<br>
If you want to quickly explore how 32 bit floating point numbers work under the hood, have a look at [this online converter][2]. If you think you want / need to convert or examine those numbers more in detail you might want to have a look at [my little editor window][3] which provides a similar functionality but also for double and integer values.
[1]: https://www.youtube.com/watch?v=PZRI1IfStY0
[2]: https://www.h-schmidt.net/FloatConverter/IEEE754.html
[3]: https://github.com/Bunny83/Floating-Point-FormatSun, 03 May 2020 17:51:53 GMTBunny83Answer by GregoryFenn
http://answers.unity.com/answers/1725877/view.html
It's not a bug. When you are using floats, you are by definition only intested in lengths/weights/scalers up to an approximate value, where being over or under by 2^(-22) is acceptable. In your case, you are complaining that 24.99999 is being cast to int 24. That's not a bug with the system or the language, it's a bug with your code.
If you need to round to the NEAREST int, then use Mathf.RoundToInt, or something like that.
Strictly speaking, you shouldn't even assume that the statement
int x = (int)(25f); // could assign 24 or 25 to x
will define x as 25. Casting to int will always round towards zero, and it's possible that the closest float that the compiler will find to 25f is in fact 24.99999 (remember it works in binary, and my examples have been in decimal).
I suspect x would be 25 in this case, because for small integers, you can exactly represent them with floats, but for larger integers, that wouldn't be true: e.g. int x = (int)(9876543f); might return 9876540, for example.
If you have a float which is meant to be an int, such as 25f, or 5/(0.2f), then one approach would be to define
int x = (int)(25f + 0.0001f);
to stop the potential rounding error.
I don't know how C# handles floats, but if you look at how 32 bit floats ("single-precision") work in C, you'll see what I'm talking about: https://en.wikipedia.org/wiki/Single-precision_floating-point_format#IEEE_754_single-precision_binary_floating-point_format:_binary32Sun, 03 May 2020 14:44:52 GMTGregoryFennAnswer by Eh3an
http://answers.unity.com/answers/1725865/view.html
It's very important that the cast will occur before the division, so you can store division operation into a variable and then cast that variable to int.
float r = 5f / 0.2f;
(int)(r)Sun, 03 May 2020 14:00:16 GMTEh3an