添加数字有问题 [英] Something wrong adding numbers

查看:62
本文介绍了添加数字有问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello All,


我在访问中使用VBA来执行一些计算。有一个

特定的sumation是错误的(几乎没有)。


这段代码正在循环。


TDist = TDist + TempDist


TDist和TempDist都被宣布为双打:


Dim TDist双倍

Dim TempDist As Double


循环7000次迭代后,以下添加产生

此结果

这些是确切的值变量:

TDist = 6388254.993

TempDist = 36.334


时间:TDist = TDist + TempDist


TDist = 6388291.32699999


TDist假设是6388291.327


虽然差异可以忽略不计,简单格式会修好

吧。我注意到它只发生在这里,我无法在

立即窗口中重现它。


我担心它是否会产生一个更大的错误。当它累积时,它往往会增加这样的错误,尤其是当运行200k次或更多次的
时。


是有没有人经历过这样的事情?

有没有解决方案可以防止出现这个错误?

或者这只是一些非常不可能的情况发生在我身上

a非常偏远和高度不可能的数据组合? (听起来很像

喜欢Douglas Adams)


- GL

Hello All,

I am using VBA in access to perform some calculations. There is a
particular sumation that is wrong (barely).

this code is withing a loop.

TDist = TDist + TempDist

Both TDist and TempDist are declared as doubles:

Dim TDist As Double
Dim TempDist As Double

After looping through 7000+ iterations the following addition produces
this result
These are the exact values of the variables:
TDist = 6388254.993
TempDist = 36.334

When: TDist = TDist + TempDist

TDist = 6388291.32699999

TDist is suppose to be 6388291.327

Although the difference is negligable and a simple Format would fix
it. I noticed it only happened here, I can''t reproduce it in the
Immediate window.

I am worried if it''s going to generate a much larger error. And when
its accumulative, it tends to grow errors like this, especially when
running 200k iterations or more.

Is this something anybody has experienced?
Is there a solution to prevent this error?
Or is this just some highly unlikely situation that occured to me for
a very remote and Highly Unlikely combination of data? (Sounds alot
like Douglas Adams)

- GL

推荐答案

Guillermo_Lopez写道:
Guillermo_Lopez wrote:

Hello All,


我在访问中使用VBA来执行一些计算。有一个

特定的sumation是错误的(几乎没有)。


这段代码正在循环。


TDist = TDist + TempDist


TDist和TempDist都被宣布为双打:


Dim TDist双倍

Dim TempDist As Double


循环7000次迭代后,以下添加产生

此结果

这些是确切的值变量:

TDist = 6388254.993

TempDist = 36.334


时间:TDist = TDist + TempDist


TDist = 6388291.32699999


TDist假设是6388291.327


虽然差异可以忽略不计,简单格式会修好

吧。我注意到它只发生在这里,我无法在

立即窗口中重现它。


我担心它是否会产生一个更大的错误。当它累积时,它往往会增加这样的错误,尤其是当运行200k次或更多次的
时。


是有没有人经历过这样的事情?

有没有解决方案可以防止出现这个错误?

或者这只是一些非常不可能的情况发生在我身上

a非常偏远和高度不可能的数据组合? (听起来很像

喜欢道格拉斯亚当斯)


- GL
Hello All,

I am using VBA in access to perform some calculations. There is a
particular sumation that is wrong (barely).

this code is withing a loop.

TDist = TDist + TempDist

Both TDist and TempDist are declared as doubles:

Dim TDist As Double
Dim TempDist As Double

After looping through 7000+ iterations the following addition produces
this result
These are the exact values of the variables:
TDist = 6388254.993
TempDist = 36.334

When: TDist = TDist + TempDist

TDist = 6388291.32699999

TDist is suppose to be 6388291.327

Although the difference is negligable and a simple Format would fix
it. I noticed it only happened here, I can''t reproduce it in the
Immediate window.

I am worried if it''s going to generate a much larger error. And when
its accumulative, it tends to grow errors like this, especially when
running 200k iterations or more.

Is this something anybody has experienced?
Is there a solution to prevent this error?
Or is this just some highly unlikely situation that occured to me for
a very remote and Highly Unlikely combination of data? (Sounds alot
like Douglas Adams)

- GL



计算机中的浮点数是不精确的,因为它们存储为

二进制文件。如果谷歌就此主题发现,你应该找到大量描述

的网站。你必须围绕它进行编码或使用不精确的比较而不是测试平等。


-

Rick Brandt,微软访问MVP

电子邮件(视情况而定)至...

在Hunter dot的RBrandt

Floating point numbers in computers are imprecise because they are stored as
binary. If you Google on the topic you should find tons of sites describing
the issue. You have to code around that or use inexact comparisons rather
than testing for equality.

--
Rick Brandt, Microsoft Access MVP
Email (as appropriate) to...
RBrandt at Hunter dot com


" Guillermo_Lopez" ; < g。***** @ iesdr.com写信息

news:52 ************************ ********** @ y38g2000 hsy.googlegroups.com ...
"Guillermo_Lopez" <g.*****@iesdr.comwrote in message
news:52**********************************@y38g2000 hsy.googlegroups.com...

Hello All,


我在访问中使用VBA来执行一些计算。有一个

特定的sumation是错误的(几乎没有)。


这段代码正在循环。


TDist = TDist + TempDist


TDist和TempDist都被宣布为双打:


Dim TDist双倍

Dim TempDist As Double


循环7000次迭代后,以下添加产生

此结果

这些是确切的值变量:

TDist = 6388254.993

TempDist = 36.334


时间:TDist = TDist + TempDist


TDist = 6388291.32699999


TDist假设为6388291.327
Hello All,

I am using VBA in access to perform some calculations. There is a
particular sumation that is wrong (barely).

this code is withing a loop.

TDist = TDist + TempDist

Both TDist and TempDist are declared as doubles:

Dim TDist As Double
Dim TempDist As Double

After looping through 7000+ iterations the following addition produces
this result
These are the exact values of the variables:
TDist = 6388254.993
TempDist = 36.334

When: TDist = TDist + TempDist

TDist = 6388291.32699999

TDist is suppose to be 6388291.327



< SNIP>


尝试使用Round函数:


Debug.Print Round(TDist + TempDist,3)


结果:6388291.327

<SNIP>

Try using the Round function:

Debug.Print Round(TDist + TempDist, 3)

Result: 6388291.327


4月24日,8:22 * pm,Stuart McCall < smcc ... @ myunrealbox.comwrote:
On Apr 24, 8:22*pm, "Stuart McCall" <smcc...@myunrealbox.comwrote:

" Guillermo_Lopez" < g.lo ... @ iesdr.com写了留言


新闻:52 ********************* ************* @ y38g2000 hsy.googlegroups.com ...
"Guillermo_Lopez" <g.lo...@iesdr.comwrote in message

news:52**********************************@y38g2000 hsy.googlegroups.com...

Hello All,
Hello All,


我在访问中使用VBA来执行一些计算。有一个

特定的sumation是错误的(勉强)。
I am using VBA in access to perform some calculations. There is a
particular sumation that is wrong (barely).


此代码带有循环。
this code is withing a loop.


* * * * TDist = TDist + TempDist
* * * *TDist = TDist + TempDist


Both TDist和TempDist被声明为双打:
Both TDist and TempDist are declared as doubles:


* * Dim TDist As Double

* * Dim TempDist As Double
* *Dim TDist As Double
* *Dim TempDist As Double


循环7000次迭代后,以下添加产生

此结果

这些是确切的变量值:

TDist = 6388254.993

TempDist = 36.334
After looping through 7000+ iterations the following addition produces
this result
These are the exact values of the variables:
TDist = 6388254.993
TempDist = 36.334


时间:* * * TDist = TDist + TempDist
When: * * *TDist = TDist + TempDist


TDist = 6388291.32699999
TDist = 6388291.32699999


TDist假设为6388291.327
TDist is suppose to be 6388291.327



< SNIP>


尝试使用Round函数:


Debug.Print Round(TDist + TempDist,3)


结果:6388291.327-隐藏引用文字 -


- 显示引用文字 -


<SNIP>

Try using the Round function:

Debug.Print Round(TDist + TempDist, 3)

Result: 6388291.327- Hide quoted text -

- Show quoted text -



好​​奇。我想我将不得不使用Round(TDIst + TempDist,7)。我得到了关于浮点数及其限制的文章




感谢您的帮助。


- GL

Curious. I guess i''ll have to use Round(TDIst + TempDist,7). I got
into the articles about Floating Point Numbers and their limitations.

Thanks for your help.

- GL


这篇关于添加数字有问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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