本帖最后由 kyra 于 2023-3-21 08:54 编辑
[Fortran] 纯文本查看 复制代码 module NUMERTICAL
implicit none
real, parameter :: zero = 0.00001
contains
real function bisect(A,B)!这是声明bisect返回值为一个浮点数
IMPLICIT NONE
real A,B
real C
real FA
real FB
real FC
C=(A+B)/2.0
FC=func(C)
do while (abs(fc)> zero)
FA = func(A)
FB = func(B)
if(FA*FB < 0)then
B=C
C=(A+B)/2.0
else
A=C
C=(A+B)/2.0
end if
FC = FUNC(C)
end do
bisect = C
return
end function
real function func(X)
implicit none
real X
FUNC = (X+3)*(X-3)
return
end function
end module
program main
use NUMERTICAL
implicit none
real A,B
real ANS
do while(.true.)
write(*,*) '输入两个猜测值'
read(*,*)A,B
if(func(A)*func(B)<0) exit
write(*,*) "不正确的猜测"
end do
ANS=bisect(A,B)
write(*,"('x=',F6.3)")ans
stop
end
当输入浮点数如2.1和4.1时就会显示出现断点,求助大佬如何解决 |