2008-11-12 67 views

回答

5

您是否嘗試過比較IL(例如,與Reflector)?

static double A(int numerator, int denominator) 
{ return (double)numerator/denominator; } 

static double B(int numerator, int denominator) 
{ return numerator/(double)denominator; } 

static double C(int numerator, int denominator) 
{ return (double)numerator/(double)denominator; } 

所有這三個成爲(給予或自己取的名字):

.method private hidebysig static float64 A(int32 numerator, int32 denominator) cil managed 
{ 
    .maxstack 8 
    L_0000: ldarg.0 // pushes numerator onto the stack 
    L_0001: conv.r8 // converts the value at the top of the stack to double 
    L_0002: ldarg.1 // pushes denominator onto the stack 
    L_0003: conv.r8 // converts the value at the top of the stack to double 
    L_0004: div  // pops two values, divides, and pushes the result 
    L_0005: ret  // pops the value from the top of the stack as the return value 
} 

因此,沒有:有確切零差價。

1

即使您使用VB.NET,分子和分母都會在進行實際分割之前轉換爲雙精度,因此您的示例是相同的。

相關問題