gpt4 book ai didi

c# - 调试/ Release模式下的浮点/ double

转载 作者:可可西里 更新时间:2023-11-01 08:15:04 25 4
gpt4 key购买 nike

C#/.NET 浮点运算在 Debug模式和 Release模式之间的精度是否不同?

最佳答案

它们确实可以不同。根据 CLR ECMA 规范:

Storage locations for floating-point numbers (statics, array elements, and fields of classes) are of fixed size. The supported storage sizes are float32 and float64. Everywhere else (on the evaluation stack, as arguments, as return types, and as local variables) floating-point numbers are represented using an internal floating-point type. In each such instance, the nominal type of the variable or expression is either R4 or R8, but its value can be represented internally with additional range and/or precision. The size of the internal floating-point representation is implementation-dependent, can vary, and shall have precision at least as great as that of the variable or expression being represented. An implicit widening conversion to the internal representation from float32 or float64 is performed when those types are loaded from storage. The internal representation is typically the native size for the hardware, or as required for efficient implementation of an operation.

这基本上意味着以下比较可能相等也可能不相等:

class Foo
{
double _v = ...;

void Bar()
{
double v = _v;

if( v == _v )
{
// Code may or may not execute here.
// _v is 64-bit.
// v could be either 64-bit (debug) or 80-bit (release) or something else (future?).
}
}
}

重要信息:永远不要检查浮点值是否相等。

关于c# - 调试/ Release模式下的浮点/ double ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/90751/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com