说我有以下程序:
program derp
implicit none
integer, parameter :: ikind = selected_real_kind(18)
real (kind = ikind) :: a = 2.0 / 3.0
print*, a
end program derp
Run Code Online (Sandbox Code Playgroud)
程序derp输出0.6666666865348815917,显然不是18位精度.但是,如果我定义a=2.0并b=3.0使用相同的方法,然后定义c=a/b我得到一个输出0.666666666666666666685,这是好的.我如何将变量定义为整数的商并让它存储我想要的所有精度数字selected_real_kind?
我们都知道DO循环比FORALLFortran中的语句更强大.也就是说,您总是可以用a代替,而不是FORALL用a 代替DO.
怎么样的WHERE陈述和块?
我可以一直替换IF一个WHERE吗?是否总是可以用a编码条件和分叉WHERE,从而避免IF?
Fortran 90 及更高版本强烈建议不要使用goto语句。
但是,我仍然觉得必须在以下两种情况下使用它:
情况 1 -- 指示重新输入输入值,例如
program reenter
10 print*,'Enter a positive number'
read*, n
if (n < 0) then
print*,'The number is negative!'
goto 10
end if
print*,'Root of the given number',sqrt(float(n))
stop
end program reenter
Run Code Online (Sandbox Code Playgroud)
案例 2 —— 注释程序的一个大的连续部分(相当于/* ... */C 中的)。例如。
print*,'This is to printed'
goto 50
print*,'Blah'
print*,'Blah Blah'
print*,'Blah Blah Blah'
50 continue
print*,'Blahs not printed'
Run Code Online (Sandbox Code Playgroud)
goto在 Fortran 90 中的上述两种情况下,如何摆脱 using语句并使用一些替代方案?
在Fortran中将两个整数连接到整数的最佳方法是什么?
integer a = 999
integer b = 1111
Run Code Online (Sandbox Code Playgroud)
integer c 应该 9991111
谢谢,SM.
我注意到如果我使用integer(16)变量,当我使用时print,输出包含预期的确切空格数.因此,当我使用时(有些人可能会认识到项目的euler问题)
implicit none
integer(16)::sum_sq,sq_sum,diff,i
sum_sq=0;sq_sum=0;
do i=1,100
sum_sq=sum_sq+i*i
sq_sum=sq_sum+i
enddo
diff=abs(sq_sum**2-sum_sq)
print *, "The difference is", diff
Run Code Online (Sandbox Code Playgroud)
我明白了
The difference is 25164150
Run Code Online (Sandbox Code Playgroud)
作为输出,但当我使用integer(8)或integer为变量,我得到
The difference is 25164150
Run Code Online (Sandbox Code Playgroud)
作为输出.这种情况发生在有和没有-m64标志的情况下,只有在gfortran上(我的ifort似乎不接受kind=16,一个单独的问题要处理,但返回带有空格的输出integer(8)).
有谁知道为什么会这样?
我正在尝试用 Fortran 重新编码旧的 C++ 程序以使用 LAPACK(我知道 C++ 确实有 LAPACK++,但我在安装它时遇到了很多麻烦,所以我放弃了)。
我最初没有任何编译问题,但那是当我将所有变量声明为REAL. 当我开始编写需要 LAPACK 的程序部分时,我发现传递给的所有参数都DSYEV需要是DOUBLE PRECISION. 因此,我尝试将所有内容更改为双精度(包括将所有硬编码数字更改为双精度对应数字,即 0.0 -> 0.0D0) 现在,当我尝试编译时,我得到的所有函数和子例程均出现以下错误已经写道:
Error: Return type mismatch of function <function> at (1) (REAL(4)/REAL(8))
Run Code Online (Sandbox Code Playgroud)
我不确定这是从哪里来的,因为程序中的所有内容都已更改为双精度。
例如,我声明了以下内容:
double precision :: alpha(3),d(3),zeta1,zeta2
double precision :: A1(3),A2(3),D1(3),D2(3)
double precision :: PI
PI = 3.14159265359D0
alpha = (/0.109818D0, 0.405771D0, 2.22766D0/)
d = (/0.444635D0, 0.535328D0, 0.154329D0 /)
do 10 i=1,3
A1(i) = alpha(i)*zeta1**2.0D0
A2(i) = alpha(i)*zeta2**2.0D0
D1(i) = d(i)*(2.0D0*A1(i)/PI)**(3.0D0/4.0D0)
D2(i) = d(i)*(2.0D0*A2(i)/PI)**(3.0D0/4.0D0)
10 continue …Run Code Online (Sandbox Code Playgroud) 我有一个Fortran程序,它从打开和读取.txt文件中的数据开始.在程序结束时,将写入一个新文件,该文件将替换旧文件(最初导入的文件).
但是,可能会发生需要打开的文件不存在,对于这种情况,应该从.txt文件导入的变量应该是0.
我想通过下面的代码执行此操作,但是这不起作用,并且当文件history.txt不存在时脚本被中止.
当history.txt文件不存在时,如何让脚本将默认值设置为我的变量?
OPEN(UNIT=in_his,FILE="C:\temp\history.txt",ACTION="read")
if (stat .ne. 0) then !In case history.txt cannot be opened (iteration 1)
write(*,*) "history.txt cannot be opened"
KAPPAI=0
KAPPASH=0
go to 99
end if
read (in_his, *) a, b
KAPPAI=a
KAPPASH=b
write (*, *) "KAPPAI=", a, "KAPPASH=", b
99 close(in_his)
Run Code Online (Sandbox Code Playgroud)
导入的文件非常简单,如下所示:
9.900000000000006E-003 3.960000000000003E-003
Run Code Online (Sandbox Code Playgroud) 我可以有一个指向目标数组的不连续部分的指针数组吗?在以下程序中:
program test
implicit none
integer :: i
integer, dimension(4), target :: orig
integer, dimension(:), pointer :: ref
orig = (/ (i , i = 1,4) /)
ref(1:2) => orig(1:2)
ref(3:3) => orig(4:4)
print *, 'orig = ', orig
print *, 'ref = ', ref
end program test
Run Code Online (Sandbox Code Playgroud)
我希望指针ref指向(/1,2,4/)目标数组的不连续部分orig.代码编译时没有警告,但是输出不是所希望的:
code output:
orig = 1 2 3 4
ref = 4
Run Code Online (Sandbox Code Playgroud)
看起来编译器强制指针指向一个连续的子部分,即使我首先指向索引1:2但编译器只强制指向4:4.结果,我在末尾的指针只指向一个条目.
我可以将条目打包(/1,2,4/)到另一个数组中然后连续使用它,但这对于大数据集来说不是有效的Fortran编码.你知道怎么做这个吗?
我正在尝试编写一个递归遍历目录的shell脚本,然后在每个文件中将所有大写字母转换 为小写字母.要清楚,我不是要更改文件名,而是更改文件中的文本.
注意事项:
最好的方法是什么?
我试图说明如何将函数传递给Newton Raphson过程.我成功了一个非常简单的函数(unefonction见下文)但它不适用于有参数的函数.这第二个fonction叫gaussienne,它需要一个参数,x和两个可选参数mu和sig.在我的牛顿拉夫森程序中,我用这种方式调用了这个函数:f(x).什么是奇怪,我是在执行过程中,程序操作,就好像可选参数sig和mu存在,但他们不...因此我不明白...
这是包含这些功能的模块
module fonction
implicit none
! parametre pour la gaussienne
double precision :: f_sigma = 1.d0, f_mu = 0.d0
! pi accessible uniquement en interne
double precision, parameter :: pi = 3.14159265359d0
contains
double precision function unefonction(x)
! fonction : unefonction
! renvoie
! $\frac{e^x - 10}{x + 2}$
implicit none
! arguments
double precision, intent(in) :: x
unefonction = (exp(x) - 10.) / …Run Code Online (Sandbox Code Playgroud)