也许是堆栈问题?长计算 - 奇怪的错误? [英] perhaps a stack problem? Long calculations - strange error?

查看:64
本文介绍了也许是堆栈问题?长计算 - 奇怪的错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我遇到了一个真的,*真的*,非常奇怪的错误:-)


我有一个for-loop和8次运行后我得到了奇怪的结果......我

意思是:一个非常奇怪的结果....

我正在计算温度。 T [j] [i] = 20度完全

次...... 2D T阵列看起来像这样:


| 2:3:.....我的方向向右 - >

- + ----------

2: | 20 20

3:| 20 20


j方向下降


(指数1和4 = 0,但它们没关系)


代码*有效*如下:

- - - - - - - - - - -


{

/ *将2D(旧)温度复制到1D阵列* /

count = 0;

solve_temp [0] = 0 ;


/ * number_of_interior_cells = 4 * /

for(j = 2,i = 1,no = 1; no< =(int)number_of_interior_cells;没有++)

{

/ *更新列(x值)* /

i ++;


solve_temp [no] = T [j] [i];

solve_temp [no] + =(hx [j] [i] *(T [j] [i-1] -T [j] [i]))

/ h0 [j] [i];

solve_temp [no] + =(hx [j] [i + 1] * (T [j] [i + 1] -T [j] [i]))

/ h0 [j] [i];

solve_temp [no] + =(hy [j] [i] *(T [j-1] [i] -T [j] [i]))

/ h0 [j] [i];

solve_temp [no] + =(hy [j + 1] [i] *(T [j + 1] [i] -T [j] [i]))

/ h0 [j] [i];


/ *下方你看到的代码*没有科技工作* - 应该够了吧

一样??? * /


// solve_temp [no] = T [j] [i] +(

// hx [j] [i] *(T [j] [i-1] -T [j] [i])+ hx [j] [i + 1] *(

T [j] [i + 1] -T [j ] [i])+

// hy [j] [i] *(T [j-1] [i] -T [j] [i])+ hy [j + 1] [i] *(

T [j + 1] [i] -T [j] [i])

//)/ h0 [j] [i] ;


如果(i == nx-1)/ *击中东边界? * /

{

j ++; / *下一行* /

i = 1; / *重置x坐标* /

}

}

}


预期结果是solve_temp [1] = 20,solve_temp [2] =

20,solve_temp [3] = 20,solve_temp [4] = 20。如果我

互相交换上述评论/注释部分,我将获得前7次获得的预期。在第8次我得到了

下一个结果为solve_temp [1] = -1。#IND000000000000,但

其他结果还可以....


究竟是什么-1。#IND000000000000意味着什么?


并不是完全一样的代码???我很困惑......


万一你想知道,hx或hy是0或1你可以

见下文并且所有的温差都是0,所以它无论如何都不应该是...... b0 = 2但也不应该改变

因为一切都给了0所以我有一些东西是0

除以2而那个当然应该给0 ........一切都结束了

up(20 + 0 )= 20 ...


打印hx(步骤= 8):


i = 1:i = 2:i = 3:

i = 4:

j = 1: - > 0.00000 - > 0.00000 - > 0.00000 - > 0.00000

j = 2: - > 0.00000 - > 0.00000 - > 1.00000 - > 0.00000

j = 3: - > 0.00000 - > 0.00000 - > 1.00000 - > 0.00000

j = 4: - > 0.00000 - > 0.00000 - > 0.00000 - > 0.00000


打印hy(步骤= 8):


i = 1:i = 2:i = 3:

i = 4:

j = 1: - > 0.00000 - > 0.00000 - > 0.00000 - > 0.00000

j = 2: - > 0.00000 - > 0.00000 - > 0.00000 - > 0.00000

j = 3: - > 0.00000 - > 1.00000 - > 1.00000 - > 0.00000

j = 4: - > 0.00000 - > 0.00000 - > 0.00000 - > 0.00000


(只显示5位小数,但它不应该改变任何东西

我猜的大图)....


为什么我在5个单独的

步骤中计算solve_temp [no]而不是在一个长计算中?甚至Microsoft Visual

Studio 2005调试器也同意我的观点,但后来我跳过了

计算线并找到了这个愚蠢/奇怪的结果......!


我希望有人可以解释这个奇怪的行为...我希望

有一个自然的像堆栈溢出这样的解释或者

无论如何....?


如果我没有查看我的代码,我有点害怕发布这个

正常,但现在我已经看了大约1-2个小时,当我的调试器甚至与我同意时,我选择F10跳过

并得到另一个结果,然后我想我需要专业帮助:-)


它现在有效(有5个人计算),但我只是想要

请确保此错误以后不会再回来....

祝你好运/ Med venlig hilsen

Martin J?rgensen


-

---------------------------------- -----------------------------------------

主页of Martin J?rgensen - http://www.martinjoergensen.dk

Hi,

I''ve encountered a really, *really*, REALLY strange error :-)

I have a for-loop and after 8 runs I get strange results...... I
mean: A really strange result....
I''m calculating temperatures. T[j][i] = 20 degrees at all
times.... The 2D T-array looks like this:

| 2: 3: ..... i-direction goes to the right ->
--+----------
2: | 20 20
3: | 20 20

j-direction goes down

(index 1 and 4 = 0, but they don''t matter)

The code *that works* looks like this:
- - - - - - - - - - - -

{
/* copy 2D (old) temperatures to 1D-array */
count = 0;
solved_temp[0] = 0;

/* number_of_interior_cells = 4 */
for(j=2, i=1, no=1; no<=(int) number_of_interior_cells; no++)
{
/* update columns (x-value) */
i++;

solved_temp[no] = T[j][i];
solved_temp[no] += ( hx[j][i] * ( T[j][i-1]-T[j][i] ) )
/ h0[j][i];
solved_temp[no] += ( hx[j][i+1] * ( T[j][i+1]-T[j][i] ) )
/ h0[j][i];
solved_temp[no] += ( hy[j][i] * ( T[j-1][i]-T[j][i] ) )
/ h0[j][i];
solved_temp[no] += ( hy[j+1][i] * ( T[j+1][i]-T[j][i] ) )
/ h0[j][i];

/* below you see the code that *doesn''t work* - isn''t it
the same??? */

//solved_temp[no] = T[j][i] + (
// hx[j][i] * ( T[j][i-1]-T[j][i] ) + hx[j][i+1] * (
T[j][i+1]-T[j][i] ) +
// hy[j][i] * ( T[j-1][i]-T[j][i] ) + hy[j+1][i] * (
T[j+1][i]-T[j][i] )
// ) / h0[j][i];

if(i == nx-1) /* hits east boundary? */
{
j++; /* next row */
i=1; /* reset x-coordinate */
}
}
}

The expected result is that solved_temp[1] = 20, solved_temp[2] =
20, solved_temp[3] = 20, solved_temp[4] = 20 at all times. If I
swap the above outcommented/commented sections with each other, I
get what I expect the first 7 times. On the 8th time I get that
the next result for solved_temp[1] = -1.#IND000000000000, but the
other results are okay....

What exactly is that -1.#IND000000000000 means?

And isn''t it exactly the same code??? I''m pretty confused...

In case you''re wondering, hx or hy is either 0 or 1 as you can
see below and all the temperature differences is 0, so it
shouldn''t matter anyway... h0 = 2 but also that shouldn''t change
anything since everything gives 0 so I have something which is 0
divided by 2 and that ofcourse should give 0........ It all ends
up in (20 + 0) = 20...

Printing hx (step = 8):

i=1: i=2: i=3:
i=4:
j=1: -> 0.00000 -> 0.00000 -> 0.00000 -> 0.00000
j=2: -> 0.00000 -> 0.00000 -> 1.00000 -> 0.00000
j=3: -> 0.00000 -> 0.00000 -> 1.00000 -> 0.00000
j=4: -> 0.00000 -> 0.00000 -> 0.00000 -> 0.00000

Printing hy (step = 8):

i=1: i=2: i=3:
i=4:
j=1: -> 0.00000 -> 0.00000 -> 0.00000 -> 0.00000
j=2: -> 0.00000 -> 0.00000 -> 0.00000 -> 0.00000
j=3: -> 0.00000 -> 1.00000 -> 1.00000 -> 0.00000
j=4: -> 0.00000 -> 0.00000 -> 0.00000 -> 0.00000

(only 5 decimal digits shown, but it shouldn''t change anything in
the big picture I guess)....

Why does it work when I calculate solved_temp[no] in 5 individual
steps instead of in one long calculation? Even Microsoft Visual
Studio 2005 debugger agrees with me, but then I step over the
calculation line and find this stupid/strange result......!

I hope somebody can explain this really weird behaviour... I hope
there''s a "natural" explanation like "stack overflow" or
whatever....?

I was a bit afraid of posting this if I didn''t check my code
properly but now I''ve looked at it for about 1-2 hours and when
my debugger even agrees with me and I then choose F10 "step over"
and get another result, then I think I need professional help :-)

It works now (with 5 individual calculations), but I just want to
be sure that this error doesn''t come back later....
Best regards / Med venlig hilsen
Martin J?rgensen

--
---------------------------------------------------------------------------
Home of Martin J?rgensen - http://www.martinjoergensen.dk

推荐答案

Martin Joergensen认为:
Martin Joergensen opined:


我有遇到真的,*真的*,非常奇怪的错误:-)

我有一个for循环,经过8次运行我得到了奇怪的结果......我的意思是:真的奇怪的结果....

我正在计算温度。 T [j] [i] = 20度所有时间...... 2D T阵列看起来像这样:

| 2:3:.....我向右走 - >
- + ----------
2:| 20 20
3:| 20 20

j方向下降

(索引1和4 = 0,但它们没关系)

代码*有效*看起来像这样:
- - - - - - - - - - -

/ *将2D(旧)温度复制到1D阵列* /
count = 0;
solve_temp [0] = 0;

/ * number_of_interior_cells = 4 * /
for(j = 2,i = 1,no = 1;没有< =(int)number_of_interior_cells;没有++)
{
/ *更新列(x值)* /
i ++;

solve_temp [no] = T [j] [i];
solve_temp [no] + =(hx [j] [i] *(T [j] [i-1] -T [j] [i]))
/ h0 [j] [i];
solve_temp [no] + =(hx [j] [i + 1] *(T [j] [i + 1] -T [j] [i] ))
/ h0 [j] [i];
solve_temp [no] + =(hy [j] [i] *(T [j-1] [i] -T [j] [ i]))
/ h0 [j] [i];
solve_temp [no] + =(hy [j + 1] [i] *(T [j + 1] [i] -T [j] [i]))
/ h0 [j] [i];

/ *下面你看到*不起作用的代码* - 不是吗<无线电通信/>一样??? * /

// solve_temp [no] = T [j] [i] +(
// hx [j] [i] *(T [j] [i-1] -T [j] [i])+ hx [j] [i + 1] *(
T [j] [i + 1] -T [j] [i])+
// hy [j] [i] *(T [j-1] [i] -T [j] [i])+ hy [j + 1] [i] *(
T [j + 1] [ i] -T [j] [i])
//)/ h0 [j] [i];

如果(i == nx-1)/ *击中东边界? * /
{
j ++; / *下一行* /
i = 1; / *重置x坐标* /
}
}

预期的结果是solve_temp [1] = 20,solve_temp [2] =
20,solve_temp [3] = 20,solve_temp [4] = 20。如果我将上述评论/评论的部分相互交换,我将获得前7次的预期。在第8次我得到了
下一个结果为solve_temp [1] = -1。#IND000000000000,但
其他结果还可以....

到底是什么是-1。#IND000000000000是什么意思?


我的猜测是你注释掉的代码溢出了。上面看起来好像是一些IEEE错误/溢出/ NaN的东西。周末还是太多了,这是为了仔细观察。另外,你应该真的发布一个

可编辑的例子来说明问题。

并不是完全相同的代码???我很困惑......


数学上,它可能是。不幸的是,计算机没有

无限精度。其中可能有你的撤销。

如果你想知道,hx或hy是0或1,你可以看到下面所有的温差是0,所以它<无论如何都不应该...... h0 = 2但也不应该改变什么因为一切都给0所以我有一些东西是0
除以2而且那个应该是给0 ........一切都结束了(20 + 0)= 20 ......

打印hx(步骤= 8):
< br => i = 1:i = 2:i = 3:
i = 4:
j = 1: - > 0.00000 - > 0.00000 - > 0.00000 - > 0.00000
j = 2: - > 0.00000 - > 0.00000 - > 1.00000 - > 0.00000
j = 3: - > 0.00000 - > 0.00000 - > 1.00000 - > 0.00000
j = 4: - > 0.00000 - > 0.00000 - > 0.00000 - > 0.00000

打印hy(步骤= 8):

i = 1:i = 2:i = 3:
i = 4:
j = 1: - > 0.00000 - > 0.00000 - > 0.00000 - > 0.00000
j = 2: - > 0.00000 - > 0.00000 - > 0.00000 - > 0.00000
j = 3: - > 0.00000 - > 1.00000 - > 1.00000 - > 0.00000
j = 4: - > 0.00000 - > 0.00000 - > 0.00000 - > 0.00000

(只显示5位小数,但是我觉得它不应该改变任何东西)....

为什么呢当我在5个单独的步骤中计算solve_temp [no]而不是在一个长计算中时工作?甚至Microsoft Visual
Studio 2005调试器也同意我的观点,但后来我走过了
计算线,找到了这个愚蠢/奇怪的结果......!

我希望有人可以解释这个奇怪的行为......我希望
有一个自然的像堆栈溢出这样的解释或者
什么......?

如果我没有正确检查我的代码,我有点害怕发布这个但现在我已经看了它大约1-2个小时,当我的调试器甚至同意我,然后我选择F10跳过
并得到另一个结果,然后我想我需要专业的帮助: - )
它现在有效(有5次个人计算),但我只是想确定这个错误以后不会再回来......
Hi,

I''ve encountered a really, *really*, REALLY strange error :-)

I have a for-loop and after 8 runs I get strange results...... I
mean: A really strange result....
I''m calculating temperatures. T[j][i] = 20 degrees at all
times.... The 2D T-array looks like this:

| 2: 3: ..... i-direction goes to the right ->
--+----------
2: | 20 20
3: | 20 20

j-direction goes down

(index 1 and 4 = 0, but they don''t matter)

The code *that works* looks like this:
- - - - - - - - - - - -

{
/* copy 2D (old) temperatures to 1D-array */
count = 0;
solved_temp[0] = 0;

/* number_of_interior_cells = 4 */
for(j=2, i=1, no=1; no<=(int) number_of_interior_cells; no++)
{
/* update columns (x-value) */
i++;

solved_temp[no] = T[j][i];
solved_temp[no] += ( hx[j][i] * ( T[j][i-1]-T[j][i] ) )
/ h0[j][i];
solved_temp[no] += ( hx[j][i+1] * ( T[j][i+1]-T[j][i] ) )
/ h0[j][i];
solved_temp[no] += ( hy[j][i] * ( T[j-1][i]-T[j][i] ) )
/ h0[j][i];
solved_temp[no] += ( hy[j+1][i] * ( T[j+1][i]-T[j][i] ) )
/ h0[j][i];

/* below you see the code that *doesn''t work* - isn''t it
the same??? */

//solved_temp[no] = T[j][i] + (
// hx[j][i] * ( T[j][i-1]-T[j][i] ) + hx[j][i+1] * (
T[j][i+1]-T[j][i] ) +
// hy[j][i] * ( T[j-1][i]-T[j][i] ) + hy[j+1][i] * (
T[j+1][i]-T[j][i] )
// ) / h0[j][i];

if(i == nx-1) /* hits east boundary? */
{
j++; /* next row */
i=1; /* reset x-coordinate */
}
}
}

The expected result is that solved_temp[1] = 20, solved_temp[2] =
20, solved_temp[3] = 20, solved_temp[4] = 20 at all times. If I
swap the above outcommented/commented sections with each other, I
get what I expect the first 7 times. On the 8th time I get that
the next result for solved_temp[1] = -1.#IND000000000000, but the
other results are okay....

What exactly is that -1.#IND000000000000 means?
My guess is that your commented out code overflows. The above looks
like some IEEE error/overflow/NaN thingy. It''s still too much weekend
''round here for a closer look. Also, you should have really posted a
compilable example that shows the problem.
And isn''t it exactly the same code??? I''m pretty confused...
Mathematically, it probably is. Computers, unfortunately don''t have
infinite precision. Therein may lie your undoing.
In case you''re wondering, hx or hy is either 0 or 1 as you can
see below and all the temperature differences is 0, so it
shouldn''t matter anyway... h0 = 2 but also that shouldn''t change
anything since everything gives 0 so I have something which is 0
divided by 2 and that ofcourse should give 0........ It all ends
up in (20 + 0) = 20...

Printing hx (step = 8):

i=1: i=2: i=3:
i=4:
j=1: -> 0.00000 -> 0.00000 -> 0.00000 -> 0.00000
j=2: -> 0.00000 -> 0.00000 -> 1.00000 -> 0.00000
j=3: -> 0.00000 -> 0.00000 -> 1.00000 -> 0.00000
j=4: -> 0.00000 -> 0.00000 -> 0.00000 -> 0.00000

Printing hy (step = 8):

i=1: i=2: i=3:
i=4:
j=1: -> 0.00000 -> 0.00000 -> 0.00000 -> 0.00000
j=2: -> 0.00000 -> 0.00000 -> 0.00000 -> 0.00000
j=3: -> 0.00000 -> 1.00000 -> 1.00000 -> 0.00000
j=4: -> 0.00000 -> 0.00000 -> 0.00000 -> 0.00000

(only 5 decimal digits shown, but it shouldn''t change anything in
the big picture I guess)....

Why does it work when I calculate solved_temp[no] in 5 individual
steps instead of in one long calculation? Even Microsoft Visual
Studio 2005 debugger agrees with me, but then I step over the
calculation line and find this stupid/strange result......!

I hope somebody can explain this really weird behaviour... I hope
there''s a "natural" explanation like "stack overflow" or
whatever....?

I was a bit afraid of posting this if I didn''t check my code
properly but now I''ve looked at it for about 1-2 hours and when
my debugger even agrees with me and I then choose F10 "step over"
and get another result, then I think I need professional help :-)

It works now (with 5 individual calculations), but I just want to
be sure that this error doesn''t come back later....




我还建议你找一个关于使用有限的

精度浮点运算的陷阱的好文本。几个月后,这里发布了一个相当不错的链接

。我唯一记得的是

它是在Sun网站上的某个地方。


-

为什么要使用Windows ,既然有门?

fa****@galileo.rhein-neckar .de ,Andre Fachat)


< http://clc-wiki.net/wiki/Introduction_to_comp.lang.c>



I''d also recommend you find a good text on the pitfalls of using finite
precision floating point arithmetic. One fairly good link has been
posted here a couple of months back. The only thing I remember about
it was that it was on the Sun site somewhere.

--
Why use Windows, since there is a door?
(By fa****@galileo.rhein-neckar.de, Andre Fachat)

<http://clc-wiki.net/wiki/Introduction_to_comp.lang.c>


Vladimir Oka写道:
Vladimir Oka wrote:
我还建议你找一个关于使用有限的精确浮点运算的陷阱的好文本。几个月前,这里发布了一个相当不错的链接。我唯一记得的是
它是在某个地方的太阳网站上。
I''d also recommend you find a good text on the pitfalls of using finite
precision floating point arithmetic. One fairly good link has been
posted here a couple of months back. The only thing I remember about
it was that it was on the Sun site somewhere.




你可能是指纸什么每个计算机科学家

应该知道浮点算术作者:David Goldberg在Sun的网站上提供了


< http://docs.sun.com/source/806-3568/ncg_goldberg。 HTML>其中包括许多

其他地方。


Robert Gamble



You are probably referring to the paper "What Every Computer Scientist
Should Know About Floating-Point Arithmetic" by David Goldberg which is
available on Sun''s website at:
<http://docs.sun.com/source/806-3568/ncg_goldberg.html> among many
other places.

Robert Gamble


Vladimir Oka写道:
Vladimir Oka wrote:
Martin Joergensen认为:
Martin Joergensen opined:



-snip-


-snip-

究竟是什么那-1。#IND000000000000意味着什么?
What exactly is that -1.#IND000000000000 means?



我的猜测是你注释掉的代码溢出了。以上看起来像某些IEEE错误/溢出/ NaN东西。这周仍然是太多的周末了。仔细看看。另外,你应该已经发布了一个显示问题的可编辑的例子。


My guess is that your commented out code overflows. The above looks
like some IEEE error/overflow/NaN thingy. It''s still too much weekend
''round here for a closer look. Also, you should have really posted a
compilable example that shows the problem.




你不应该自己编写代码......我只是尝试过,但是我的

代码非常大。我的意思是:hx,hy和h0是在每一步中计算的,并且动态分配
所以最小可能的代码可能仍然是
是几百行。


我刚做了一个100行的程序,没有计算hx和hy而且这里

没有错误....嗯。所以我想我的

代码中有错误...


我仍​​然无法解释添加5个数字之间的区别然后

得到正确的结果而不是一次做所有事情......所以这基本上是

为什么我问。当Microsoft Visual Studio 2005调试器同意我的时候,我真的很困惑,因为我无法想象它会对我说谎。


-snip-



You shouldn''t try to code anything yourself... I just tried it but my
code is pretty big. I mean: hx, hy and h0 is calculated in each step and
dynamically allocated so the smallest possible code would probably still
be some hundred lines.

I just made a 100 line program that didn''t calculate hx and hy and here
there was no error.... Hmm. So I guess I have an error somewhere in my
code...

Still I can''t explain the difference between adding 5 numbers and then
get the right result vs. doing everything at once... So that''s basically
why I asked. And when Microsoft Visual studio 2005 debugger agrees with
me, then I''m really confused because I couldn''t imagine that it would
lie to me about anything.

-snip-

现在可以使用(有5次个人计算),但我只是想
请确保此错误以后不会再发生....
It works now (with 5 individual calculations), but I just want to
be sure that this error doesn''t come back later....



我也建议你找一个关于使用有限的陷阱的好文本<精确浮点运算。几个月前,这里发布了一个相当不错的链接。我唯一记得的是
它是在Sun网站上的某个地方。


I''d also recommend you find a good text on the pitfalls of using finite
precision floating point arithmetic. One fairly good link has been
posted here a couple of months back. The only thing I remember about
it was that it was on the Sun site somewhere.




我看着它......好像它需要很漂亮很长时间阅读它

全部。也许我会稍后再看。谢谢你的回答。

祝你好运/ Med venlig hilsen

Martin J?rgensen


-

----------------------------------------------- ----------------------------

Martin J?rgensen的故乡 - http://www.martinjoergensen.dk


这篇关于也许是堆栈问题?长计算 - 奇怪的错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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