C# - Inconsistent math operation result on 32-bit and 64-bit

前端 未结 5 1227
谎友^
谎友^ 2020-12-01 19:01

Consider the following code:

double v1 = double.MaxValue;
double r = Math.Sqrt(v1 * v1);

r = double.MaxValue on 32-bit machine r = Infinity

5条回答
  •  一向
    一向 (楼主)
    2020-12-01 19:38

    I tried this in x86 and x64 in debug and release mode:

    x86 debug:   Double.MaxValue
    x64 debug:   Infinity
    x86 release: Infinity
    x64 release: Infinity
    

    So, it seems that it's only in debug mode that you get that result.

    Not sure why there is a difference, though, the x86 code in debug mode:

                double r = Math.Sqrt(v1 * v1);
    00025bda  fld         qword ptr [ebp-44h] 
    00025bdd  fmul        st,st(0) 
    00025bdf  fsqrt            
    00025be1  fstp        qword ptr [ebp-5Ch] 
    00025be4  fld         qword ptr [ebp-5Ch] 
    00025be7  fstp        qword ptr [ebp-4Ch] 
    

    is the same as the code in release mode:

                double r = Math.Sqrt(v1 * v1);
    00000027  fld         qword ptr [ebp-8] 
    0000002a  fmul        st,st(0) 
    0000002c  fsqrt            
    0000002e  fstp        qword ptr [ebp-18h] 
    00000031  fld         qword ptr [ebp-18h] 
    00000034  fstp        qword ptr [ebp-10h]
    

提交回复
热议问题