c# - Which numeric type conversion is better for simple math operation? -
i want know conversion better (regarding performance/speed , precision/most less loss) simple math operation , makes them different?
example:
double double1 = integer1 / (5 * integer2); var double2 = integer1 / (5.0 * integer2); var double3 = integer1 / (5d * integer2); var double4 = (double) integer1 / (5 * integer2); var double5 = integer1 / (double) (5 * integer2); var double6 = integer1 / ((double) 5 * integer2); var double7 = integer1 / (5 * (double) integer2); var double8 = convert.todouble(integer1 / (5 * integer2)); var double9 = integer1 / convert.todouble(5 * integer2);
actually question conversion not type itself.
edit
in response totally changed question:
the first line double double1 = integer1 / (5 * integer2);
integer division, don't that.
also line var double8 = convert.todouble(integer1 / (5 * integer2));
doing integer division before converting result double, don't either.
other that, different approaches list end calling il instruction conv.r8
once each line in sample code.
the real difference convert.todouble()
make method call so, should avoid that.
the results every line other double1
, double8
identical.
so should go simplest: var double2 = integer1 / (5.0 * integer2);
in more complicated situation, time code see if there's differences.
Comments
Post a Comment