在Fortran中将逻辑类型转换为double [英] Convert logical type to double in Fortran
问题描述
我正在寻找一种将逻辑类型变量转换为真实类型的方法,该类型可以在ifort和gfortran中使用。以下是ifort中的工作,但gfortran中 :
逻辑:: a
real :: b
a = .true。
b = dble(a)
gfortran引发的错误是 $ b (1)处的'dble'固有的'a'参数必须是(b)一个数字类型
显然,.true。应映射到1.d0和.false。到0.d0。这样做的最好方法是什么?
我不确定是否有内置工具可以做到这一点。我不知道为什么ifort接受这个,我的猜测是它是一个编译器特定的功能。
一个选项,特别是因为你希望它是bullet proof,就是创建你自己的函数。
我没有测试过这个,但是下面的代码可能会工作:
< (a)
逻辑,意图(in):: a
if(a)then
logic2dbl = 1.d0
else
logic2dbl = 0.d0
end if
end function logic2dbl
I'm looking for a bulletproof way of converting logical type variables to real type that will work in both ifort and gfortran. The following works in ifort, but not in gfortran:
logical :: a
real :: b
a = .true.
b = dble(a)
The error thrown in gfortran is
b = dble(a)
1
Error: 'a' argument of 'dble' intrinsic at (1) must be a numeric type
Obviously, .true. should map to 1.d0, and .false. to 0.d0. What's the best way of doing this?
I am not sure if there is an intrinsic tool that does this. I do not know why ifort accepts this, and my guess would be that it is a compiler specific functionality.
an option to to this, specifically since you want this to be bullet proof, is to create your own function.
I have not tested this, but the following might work:
double precision function logic2dbl(a)
logical, intent(in) :: a
if (a) then
logic2dbl = 1.d0
else
logic2dbl = 0.d0
end if
end function logic2dbl
这篇关于在Fortran中将逻辑类型转换为double的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!