声振论坛

 找回密码
 我要加入

QQ登录

只需一步,快速开始

查看: 2290|回复: 1

[Fortran] 一个返回复数的函数,老是出错,请师兄弟们帮忙看看

[复制链接]
发表于 2010-3-22 01:21 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?我要加入

x
程序很简单,计算平方根,返回复数值,结果编译警告三个,说第0个形参与实参类型不对。运行结果说access violation
    program main
complex(8) x
x=sqrt1(2.0)
print*, x
end program main
complex(8) function sqrt1(a)
   real(8)::x,e=1E-4,p,q,x0=1.0
   real(8) a
   parameter(N=1E8)
   logical::s=.false.
   x=abs(a)
   p=(x0+x/x0)/2
   do i=1,N
      q=(p+x/p)/2
   if (abs(q-p)<=e) then
      s=.true.
   exit
      else
         p=q
   endif
enddo
   if (s==.false.) then
      sqrt=-1
   print *,'The Method failed'
   return
   endif
   if (a<0) then
       sqrt1=cmplx(0,q)
   else
       sqrt1=cmplx(q,0)
   endif
end function

编译警告:
E:\fortran works\numeric1\test.f90(3) : Warning: In the call to SQRT1, actual argument #0 does not match the type and kind of the corresponding dummy argument.
x=sqrt1(2.0)
---------^
E:\fortran works\numeric1\test.f90(3) : Warning: In the call to SQRT1, there is no actual argument corresponding to the dummy argument A.
x=sqrt1(2.0)
---^
E:\fortran works\numeric1\test.f90(3) : Warning: Routine SQRT1 called with different number and/or type of actual arguments in earlier call - C attribute required if intended.
x=sqrt1(2.0)
---^
test.obj - 0 error(s), 3 warning(s)
回复
分享到:

使用道具 举报

 楼主| 发表于 2010-3-25 21:44 | 显示全部楼层
已经自己解决了。加一个interface就可以了,贴出来给大家参考参考吧。
program main
interface
      complex(8) function sqrt1(a)
   real(8) a
      end function
end interface
complex(8) x
x=sqrt1(dble(2.0))
print *, x
end program main
complex(8) function sqrt1(a)
   real(8)::x,e=1E-4,p,q,x0=1.0
   real(8) a
   parameter(N=1E8)
   logical::s=.false.
   x=abs(a)
   p=(x0+x/x0)/2
   do i=1,N
      q=(p+x/p)/2
   if (abs(q-p)<=e) then
      s=.true.
   exit
      else
         p=q
   endif
enddo
   if (s==.false.) then
      sqrt=-1
   print *,'The Method failed'
   return
   endif
   if (a<0) then
       sqrt1=cmplx(0,q)
   else
       sqrt1=cmplx(q,0)
   endif
end function

评分

1

查看全部评分

您需要登录后才可以回帖 登录 | 我要加入

本版积分规则

QQ|小黑屋|Archiver|手机版|联系我们|声振论坛

GMT+8, 2024-4-29 20:19 , Processed in 0.238008 second(s), 24 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表