Type conversion strange bug
-
Yesterday I've caught a very strange bug float a = 81.1666641F; float b = 18F; float c = 6.3166666F; float d = (a - b) / c; int e = (int)((a - b) / c); int f = (int)d; Console.WriteLine(d); Console.WriteLine(e); Console.WriteLine(f); -------------------------- OUTPUT: 10.0 9 10 :~
-
Yesterday I've caught a very strange bug float a = 81.1666641F; float b = 18F; float c = 6.3166666F; float d = (a - b) / c; int e = (int)((a - b) / c); int f = (int)d; Console.WriteLine(d); Console.WriteLine(e); Console.WriteLine(f); -------------------------- OUTPUT: 10.0 9 10 :~
Hi, there is no bug here. It is a result of rounding (and the implicit conversion rules). Do the same code with double, and try to explain ! :)
Luc Pattyn [My Articles]
-
Yesterday I've caught a very strange bug float a = 81.1666641F; float b = 18F; float c = 6.3166666F; float d = (a - b) / c; int e = (int)((a - b) / c); int f = (int)d; Console.WriteLine(d); Console.WriteLine(e); Console.WriteLine(f); -------------------------- OUTPUT: 10.0 9 10 :~
it is not a bug you are casting the result set to in so you are getting the results in Int which is close round of result
Regards, Jaiprakash M Bankolli jaiprakash.bankolli@gmail.com http://jaiprakash.blog.com/