问题描述
我有一个非常古老的程序,我想在我的64位计算机上运行。有很多折旧报表。在调试过程中,我发现很多变量变成NaN或Infinity ...因此,我将变量从4字节改为8字节(即REAL变为REAL * 8),但现在两个计算机上的计算和结果都大不相同。如果我使用更长的类型以及为什么在32位计算机上一切正常,但是在64位系统上,我可以获得Infinity和NaN值吗?有人可以解释我吗?
PS我使用 gfortran 编译器和 -fbackslash -ffixed-line-length-0 -std = legacy -g -O0 -fno-inline
options p>
问题,
kozooh
归因于编译器之间的差异,而不是机器的比特级之间的差异。例如,一些FORTRAN 77编译器隐式地将 save
应用于所有过程(子例程和函数)局部变量。这不是标准所要求的,不应该依赖这种行为。当使用现代编译器编译遗留程序时,如果局部变量的值在一个过程的调用中保持不变,则需要使用 save
,这经常会导致问题。我不知道g77是否有这个功能。你可以在编译器选项 -fno-automatic
中打开gfortran的行为。
子程序MySub
逻辑FirstCall
保存FirstCall
数据FirstCall / .TRUE。 /
整数I
if(FirstCall)then
I = 0
FirstCall = .FALSE。
end if
I = I + 1
write(6,*)Call,I
end
程序主体
整数j
do j = 1,4
调用MySub()
end do
end program main
使用g77编译(无编译器选项),输出结果为:打电话2
打电话3
打电话4
$ c $ <$
>
本地变量
I
在调用 MySub 。所以看起来,即使没有请求save
,g77仍然保存局部变量。至少在默认优化级别。
用gfortran编译选项
fbackslash -ffixed-line-length-0 -std = legacy -g - O0 -fno-inline
输出是相同的。现在改为-O3
,输出为:
Call 1
致电2
致电3
致电129
有时<$ c
将程序的一行更改为:
save FirstCall,保存其值,有时不保存。
呼叫1
呼叫2
致电3
致电4
尝试
-fno-automatic
...
I've got really old program which I want to run on my 64bit computer. There are a lot of depreciated statements. During debugging I found that a lot of variables become NaN or Infinity... Hence, I changed variables from 4-bytes to 8-bytes long (i.e. REAL to REAL*8) but now caluclations and results are considerably different on both computers. Could somebody explain me if it really does matter if I use longer types and why on a 32-bit computer everything was OK but on a 64-bit one I get Infinity and NaN values?
P.S. I use gfortran compiler with
-fbackslash -ffixed-line-length-0 -std=legacy -g -O0 -fno-inline
optionsRegards,kozooh
解决方案Your problem is probably due to differences between the compilers and not between the bit-levels of the machines. For example, some FORTRAN 77 compilers implicitly apply
save
to all procedure (subroutine and function) local variables. This is not required by the standard and this behavior should not be relied upon. It frequently causes problems when a legacy program is compiled with a modern compiler that demands thatsave
be used if local variables should have their values retained across invocations of a procedure. I don't know if g77 has this "feature". You can turn on this behavior in gfortran with the compiler option-fno-automatic
.EDIT: consider:
subroutine MySub logical FirstCall save FirstCall data FirstCall / .TRUE. / integer I if ( FirstCall ) then I = 0 FirstCall = .FALSE. end if I = I + 1 write (6, *) "Call", I end program main integer j do j=1, 4 call MySub () end do end program main
Compiled with g77 (no compiler options), the output is:
Call 1 Call 2 Call 3 Call 4
The local variable
I
is retaining its value across invocations ofMySub
. So it appears that g77 is saving local variables even when not requested withsave
. At least at the default optimization level.Compiled with gfortran with options
fbackslash -ffixed-line-length-0 -std=legacy -g -O0 -fno-inline
the output is the same. Now change to-O3
and the output is:Call 1 Call 2 Call 3 Call 129
Sometimes
I
retains its value, sometimes not.Change one line of the program to:
save FirstCall, I
and the value is always retained:Call 1 Call 2 Call 3 Call 4
Try
-fno-automatic
...这篇关于Fortran - 从32位机器重新编译为64位机器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!