Reputation: 2284
I was trying to divide (float)200 / (float)500
but the result is 0.0. Why is this so and how can we have 0.4 as the result? Thanks a lot.
Upvotes: 1
Views: 956
Reputation: 4540
I experienced this problem in MonoDevelop debugger with Unity3D project. Somehow Evaluate window shows 1.0f/10.0f = 0 long. This is just a bug of MonoDevelop IDE.
Upvotes: 0
Reputation: 941218
It is a very common mistake, every programmer makes it at least once. There are two kind of division operators, they both use the same symbol, integral and floating point. The compiler chooses which it uses based on the types of the operands. If both the left- and right-hand operands are integral then you'll get an integral division. The idiv instruction in machine code. Which truncates to zero and produces an integral result.
As soon as at least one operand is floating point, you'll get the fdiv instruction in machine code and the result you are looking for. Simply casting to (float) or (double) is enough, like you did in your question. You only have to cast one of them. Or use a floating point literal like 200f or 200.0
Upvotes: 3
Reputation: 15535
Nothing wrong here - I'm with Andreas.
Console.WriteLine((float)200 / (float)500);
Console.WriteLine("Press any key to continue");
Console.ReadKey(true);
Results in:
0.4
Press any key to continue
Upvotes: 2
Reputation: 44742
That's impossible. I can think of the following scenarios:
Upvotes: 2