何时发生浮点舍入错误? [英] When does floating-point rounding-errors occur?

查看:178
本文介绍了何时发生浮点舍入错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在调试我的VBA代码时,我遇到了这个奇怪的现象:

This loop

  Dim x,y as Double 
x = 0.7
y = 0.1
For x = x - y To x + y Step y
Next x


  • 用简单的数字(0.6到0.8)代替循环边界 - li>
  • 用数字替换变量(所有的组合) - 没有帮助。
  • 用while替换for循环直到循环 - 帮助。 / li>
  • 替换x和y的值(y = 0.01,0.3,0.4,0.5,0.7,0.8,0.9 - 帮助0.2,0.6 - 没有帮助,x = 1,2 ,3帮助。x = 4,5,6,7,8,9 - 没有帮助。
  • 转换 Double Decimal CDec() - 帮助。
  • 使用 Currency 数据类型而不是 D ouble - 帮助。



所以我们在这里是一个浮点四舍五入错误发生在神秘的条件。

我想知道的是这些条件是什么,所以我们可以避免它们。

谁将揭开这个谜?



(请原谅我的英文,这不是我的母语)

解决方案

<通常在解决 For ... Next 循环时,不建议使用(xy,plus(nxy)= x)时,会导致代码的严重程度不准确,因此会导致代码double或decimal或c​​urrency + y)就绝对值而言是一个不可解的等式,除非你限制它使用的小数位数。

通常认为使用整数(或长整数)变量在 For ...下一步循环中,因为它们的结果是更确定的。



如何使循环工作与非整数



如果你想它运行成功和迭代3次(正如我所期望的那样)



试试像下面这样:

  Dim x,y As Double 
x = 0.7
y = 0.1
对于x = Round(x-y,1) (y,1)
Debug.Print x
Next x

最好不要以这种特殊的方式使用双打,但是如果你必须限制他们计算的小数位数,或者设置一个更模糊的终点(即 x> y ,而不是 x = y

您使用的编码意味着您希望根据y的容忍度测试某个值x。
假设这是正确的,这意味着测试3次,其中;

  test_1:x = x  -  y 
test_2:x = x
test_3:x = x + y

  Dim i As Integer 
Dim x,y,w作为Double

x = 0.7
y = 0.1

For i = -1 To 1 Step 1
w = x +(i * y)
Debug.Print w
下一个我

祝你好运

As I was debugging my VBA code, I came across this weird phenomenon:
This loop

Dim x,y as Double  
x = 0.7  
y = 0.1  
For x = x - y To x + y Step y
Next x

runs only twice!

I tried many variations of this code to nail down the problem, and here is what I came up with:

  • Replacing the loop boundaries with simple numbers (0.6 to 0.8) - helped.
  • Replacing variables with numbers (all the combinations) - didn't help.
  • Replacing the for-loop with do while/until loops - helped.
  • Replacing the values of x and y (y=0.01, 0.3, 0.4, 0.5, 0.7, 0.8, 0.9 - helped. 0.2, 0.6 -didn't help. x=1, 2 ,3 helped. x=4, 5, 6, 7, 8, 9 - didn't help.
  • Converting the Double to Decimal with CDec() - helped.
  • Using the Currency data type instead of Double - helped.

So what we have here is a floating-point rounding-error that happens on mysterious conditions.

What I'm trying to find out is what are those conditions, so we can avoid them.
Who will unveil this mystery?

(Pardon my English, it's not my mother tongue).

解决方案

GD Falcon,

Generally in solving a For...Next loop it would not be advisable to use 'double' or 'decimal' or 'currency' variables as they provide a level of uncertainty in their accuracy, it's this level of inaccuracy that is wrecking havoc on your code as the actual stop parameter (when x-y, plus (n x y) = x+y) is, in terms of absolutes, an insolvable equation unless you limit the number of decimals it uses.

It is generally considered better practice to use integers (or long) variables in a For...Next loop as their outcome is more certain.

See also below post:

How to make For loop work with non integers

If you want it to run succesfully and iterate 3 times (as I expect you want)

Try like below:

Dim x, y As Double
x = 0.7
y = 0.1
For x = Round(x - y, 1) To Round(x + y, 1) Step Round(y, 1)
   Debug.Print x
Next x

Again, it is better not to use Doubles in this particular way to begin with but if you must you would have to limit the number of decimals they calculate with or set a more vague end point (i.e. x > y, rather than x = y)

The coding you use implies that you wish to test some value x against a tolerance level of y. Assuming this is correct it would imply testing 3 times where;

test_1: x = x - y
test_2: x = x 
test_3: x = x + y

The below code would do the same but it would have a better defined scope.

Dim i As Integer
Dim x, y, w As Double

x = 0.7
y = 0.1

For i = -1 To 1 Step 1
   w = x + (i * y)
   Debug.Print w
Next i

Good luck !

这篇关于何时发生浮点舍入错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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