浮点/双精度precision在调试/发行方式 [英] Float/double precision in debug/release modes

查看:180
本文介绍了浮点/双精度precision在调试/发行方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

做C#/。NET浮点运算的不同之处precision调试模式和发布模式之间?

Do C#/.NET floating point operations differ in precision between debug mode and release mode?

推荐答案

它们确实是不同的。根据CLR的ECMA规范:

They can indeed be different. According to the CLR ECMA specification:

存储浮点地点   数字(静态,数组元素,并   类字段)是固定的大小。   支持的存储大小   FLOAT32与float64。其他地方   (计算堆栈上,作为   参数,如返回类型,并作为   局部变量)浮点   使用数字重新presented   内部浮点类型。在每个   这样的实例中,标称类型的   变量或EX pression或者是R4或   R 8,但其值可以重新presented   内部有额外的范围   和/或precision。的大小   内部浮点再presentation   是实现相关的,可以改变,   并应至少有precision   大作为该变量的或   EX pression被重新presented。一个   隐式扩大转换到   从FLOAT32内部重新presentation   或float64时执行的那些   类型从存储加载。该   内部重新presentation一般   对于硬件天然大小,或者   如需要有效   执行的操作。

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.

什么这基本上意味着,下面的比较可能或可能不等于:

What this basically means is that the following comparison may or may not be equal:

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?).
    }
  }
}

拿回家的消息:从不检查浮动值是否相等

Take-home message: never check floating values for equality.

这篇关于浮点/双精度precision在调试/发行方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆