Exceeding work priority in C #

Will the differences below be significant in C #?

int a, b; double result; result = (double)a / b; result = a / (double)b; result = (double)a / (double)b; 

Which one are you using?

+7
source share
3 answers

Listing will occur before division.

In your examples, it doesn't matter which one you do, as if one operand were double, the runtime also converts the other to double.

It looks like micro-optimization - no need to worry or deal if measurements do not show that this is really a bottleneck.

+10
source

I'm doing it:

 result = (double)a / (double)b; 

This may be completely unnecessary, but in general I want to make sure that it will not do integer division, and I don’t really remember the specific rules for this scenario, so it’s easier (if a few more keystrokes) to be explicit.

+2
source

I do (double)a / b because I imagine that int is something indivisible, like a stone or something like that. When rushing into a double, it becomes divisible, like a cake. You can divide 3.0 cakes into four parts, but you cannot divide three stones into 4.0 parts. Or something. Is there any reason?

0
source

All Articles