问题描述
我想使用以下程序计算 gamma(-170.1):
程序算术!程序进行计算实数(8) :: xx = 伽马(-170.1)打印 *, x结束程序
但我得到了错误:
test.f95:4.10:
x = GAMMA(-170.1)1错误:GAMMA 的结果在 (1) 处下溢
当我用 gfortran 编译时.根据 Maple gamma(-170.1) = 5.191963205*10^(-172) 我认为应该在我定义的变量 x 的指数范围内.
您的程序的以下修改应该可以工作.请记住,在 Fortran 中,在分配给 LHS 之前先评估 RHS,并且浮点文字是默认类型的,即单精度.因此,将 GAMMA 参数设为双精度编译器会选择双精度 GAMMA.
I would like to calculate gamma(-170.1) using the program below:
program arithmetic
! program to do a calculation
real(8) :: x
x = GAMMA(-170.1)
print *, x
end program
but I get the error:
when I compile with gfortran. According to Maple gamma(-170.1) = 5.191963205*10^(-172) which I think should be within the range of the exponent of the variable x as I've defined it.
The below modification of your program should work. Remember that in Fortran the RHS is evaluated before assigning to the LHS, and that floating point literals are of default kind, that is single precision. Thus, making the argument to GAMMA double precision the compiler chooses the double precision GAMMA.
program arithmetic
! program to do a calculation
integer, parameter :: dp = kind(1.0d0)
real(dp) :: x
x = GAMMA(-170.1_dp)
print *, x
end program
这篇关于GAMMA 的结果低于同类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!