Unix&中的不同代码行为视窗 [英] Different code behaviour in Unix & Windows

查看:55
本文介绍了Unix&中的不同代码行为视窗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我正在对C中的大数字进行一些数学分析。相同的

代码在Windows中完美运行,但在<\\ n>中产生不可预测的结果br />
Unix(比如负数或非常大的数字)。


我使用''long''数据类型,这足以容纳最大的数据/>
程序中的数据。我在操作系统中检查了''sizeof(long)''

是相同的(= 4)。


我需要将此程序与其他模块并从Unix运行。

所以我无法继续从Windows运行它。


如果您对可能有任何线索,请告诉我。问题。


谢谢,

Saumi

解决方案



tech guru写道:



我正在对C中的大数字做一些数学分析。同样的代码运行完全在Windows中但在Unix中产生不可预测的结果(如负数或非常大的数字)。

我使用的''long''数据类型足以容纳最大的<程序中的数据。我在两个OS相同(= 4)的OS中检查了''sizeof(long)''。


您是否检查过所有中间结果都没有溢出?例如:


a =(b * c)/ d;

如果`(b * c)`
仍会在你面前爆炸溢出,即使你的
算法保证最终结果适合`a`。

我需要将这个程序与其他模块集成并从Unix运行。所以我无法继续从Windows运行它。

如果您对可能出现的问题有任何线索,请告诉我。




问题最有可能发生在第42行。




tech guru写道:

你好,我正在对C中的大数字进行一些数学分析。相同的代码在Windows中完美运行但在Unix中产生不可预测的结果(如负数或非常大)数字)。

我使用的是''long''数据类型,足以保存程序中最大的数据。我在两个操作系统中检查了''sizeof(long)''
是相同的(= 4)。

我需要将这个程序与其他模块集成并从Unix运行。
所以我不能继续从Windows运行它。

如果您对可能出现的问题有任何线索,请告诉我。

谢谢,
Saumi




如果你想在这里得到很多帮助,可以将它减少到一个小程序,即
来证明这个问题是可取的。当然,如果你这样做,你会希望

自己看看答案。


如果那不是''可行的,我建议添加一些日志代码和

精确定位

,其中程序结果不同。应该帮助解决问题的本地化。


-David


文章< 11 ****** ****************@u72g2000cwu.googlegroups .com> ;,

tech guru< sa **** @ gmail.com>写道:

我正在对C中的大数字进行一些数学分析。相同的代码在Windows中完美运行但在Unix中产生不可预测的结果(如负数或非常非常)大数字)。


你碰巧使用右移位吗?如果是这样的话,那么你可能会遇到关于什么进入最高位的差异

当你转移时。

我正在使用''long''数据类型足以容纳程序中最大的数据。我在两个操作系统中检查了''sizeof(long)''
相同(= 4)。




如果你恰好是对的 - 然后你应该使用unsigned long

而不是long:使用unsigned,顶部位的行为是

可预测。

-

法律 - 它是一种商品

- Andrew Ryan(环球邮报,2005/11/26)


Hi,

I am doing some mathematical analysis on large numbers in C. The same
code runs perfectly in Windows but produces unpredictable results in
Unix (like negative numbers or very very big numbers).

I am using ''long'' data type which is sufficient to hold the largest
data in the program. I checked the ''sizeof(long)'' in both the OS which
is same (=4).

I need to integrate this program with other modules and run from Unix.
So I can not continue to run it from Windows.

Please let me know if you have any clue on the possible problem.

Thanks,
Saumi

解决方案


tech guru wrote:

Hi,

I am doing some mathematical analysis on large numbers in C. The same
code runs perfectly in Windows but produces unpredictable results in
Unix (like negative numbers or very very big numbers).

I am using ''long'' data type which is sufficient to hold the largest
data in the program. I checked the ''sizeof(long)'' in both the OS which
is same (=4).
Did you check that all intermediate results don''t overflow? E.g.:

a = (b * c) / d;

will still blow up in your face if `(b * c)` overflows, even if your
algorithm guarantees that the final result will fit into `a`.
I need to integrate this program with other modules and run from Unix.
So I can not continue to run it from Windows.

Please let me know if you have any clue on the possible problem.



The problem is most likely on line 42.



tech guru wrote:

Hi,

I am doing some mathematical analysis on large numbers in C. The same
code runs perfectly in Windows but produces unpredictable results in
Unix (like negative numbers or very very big numbers).

I am using ''long'' data type which is sufficient to hold the largest
data in the program. I checked the ''sizeof(long)'' in both the OS which
is same (=4).

I need to integrate this program with other modules and run from Unix.
So I can not continue to run it from Windows.

Please let me know if you have any clue on the possible problem.

Thanks,
Saumi



If you want much help here, reducing it to a small program that
demonstrates
the problem would be desirable. Of course, if you did that you''d
hopefully
see the answer yourself.

If that isn''t feasable, I''d suggest adding some logging code and
pinpoint
where the programs results differ. Should help localize the problem.

-David


In article <11**********************@u72g2000cwu.googlegroups .com>,
tech guru <sa****@gmail.com> wrote:

I am doing some mathematical analysis on large numbers in C. The same
code runs perfectly in Windows but produces unpredictable results in
Unix (like negative numbers or very very big numbers).
Do you happen to be using bit shifting to the right? If so then you
could be encountering differences as to what gets put into the top bit
as you do the shift.
I am using ''long'' data type which is sufficient to hold the largest
data in the program. I checked the ''sizeof(long)'' in both the OS which
is same (=4).



If you do happen to be right-shifting then you should use unsigned long
instead of long: with unsigned, the behaviour of the top bit is
predictable.
--
"law -- it''s a commodity"
-- Andrew Ryan (The Globe and Mail, 2005/11/26)


这篇关于Unix&amp;中的不同代码行为视窗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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