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

前端 未结 5 1223
谎友^
谎友^ 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:34

    The problem is that Math.Sqrt expects a double as argument. v1 * v1 cannot be stored as double and overflows resulting in undefined behavior.

提交回复
热议问题