5

当我通过步进代码在 VS C++ 中调试我的软件时,我注意到一些浮点计算显示为一个带有尾随点的数字,即:

1232432.

导致此结果的一项操作是:

float result = pow(10, a * 0.1f) / b

其中 a 是一个很大的负数,大约 -50 到 -100,b 通常在 1 左右。我阅读了一些关于浮点数精度问题的文章。我的问题是,如果尾随点是 Visual-Studio 的一种方式,它告诉我这个数字的精度非常低,即在变量结果中。如果不是,那是什么意思?

这出现在今天的工作中,我记得较大的数字存在问题,所以每次都会发生这种情况(“这个”是指尾随点)。但我确实记得它发生在数字中有七位数字时。在这里,他们认为浮点数的精度是七位数:

C++ 浮点除法和精度

这可能是事情吗,Visual Studio 通过在最后加一个点来告诉我这一点?

我想我找到了!它说“尾数被指定为数字序列,后跟一个句点”。尾数是什么意思?这在 PC 上和在 DSP 上运行代码时会有所不同吗?因为问题是我得到了不同的结果,唯一让我觉得奇怪的是这个时期的东西,因为我不知道这意味着什么。

http://msdn.microsoft.com/en-us/library/tfh6f0w2(v=vs.71).aspx

4

3 回答 3

4

如果您指的是“sig figs”约定,其中“4.0”表示 4±0.1,“4.00”表示 4±0.01,那么不,在floator中没有这样的概念double。数字总是*以 24 或 53 个有效位(7.22 或 15.95 个十进制数字)存储,无论有多少实际“有效”。

尾随点只是一个小数点,后面没有任何数字(这是一个合法的 C 文字)。它要么意味着

  • 值是1232432.0,他们修剪了不必要的尾随零,或者
  • 一切都被四舍五入到 7 位有效数字(在这种情况下,真实值也可能是 1232431.5、1232431.625、1232431.75、1232431.875、1232432.125、1232432.25、1232432.375 或 1232432.5。)

真正的问题是,你为什么要使用floatdouble是 C(++) 中的“普通”浮点类型,并且float是一种节省内存的优化。

* 学究们会很快指出非正规、x87 80 位中间值等。

于 2011-12-16T00:33:26.687 回答
3

精度不是可变的,这只是 VS 格式化它以进行显示的方式。对于给定的浮点数,精度(或缺乏)始终是恒定的。

于 2011-12-15T21:36:09.263 回答
1

The MSDN page you linked to talks about the syntax of a floating-point literal in source code. It doesn't define how the number will be displayed by whatever tool you're using. If you print a floating-point number using either printf or std:cout << ..., the language standard specifies how it will be printed.

If you print it in the debugger (which seems to be what you're doing), it will be formatted in whatever way the developers of the debugger decided on.

There are a number of different ways that a given floating-point number can be displayed: 1.0, 1., 10.0E-001, and .1e+1 all mean exactly the same thing. A trailing . does not typically tell you anything about precision. My guess is that the developers of the debugger just used 1232432. rather than 1232432.0 to save space.

If you're seeing the trailing . for some values, and a decimal number with no . at all for others, that sounds like an odd glitch (possibly a bug) in the debugger.

If you're wondering what the actual precision is, for IEEE 32-bit float (the format most computers use these days), the next representable numbers before and after 1232432.0 are 1232431.875 and 1232432.125. (You'll get much better precision using double rather than float.)

于 2011-12-15T23:15:52.280 回答