简体   繁体   English

在 Fortran 中将逻辑类型转换为 double

[英]Convert logical type to double in Fortran

I'm looking for a bulletproof way of converting logical type variables to real type that will work in both ifort and gfortran.我正在寻找一种将逻辑类型变量转换为真实类型的防弹方法,它可以在 ifort 和 gfortran 中使用。 The following works in ifort, but not in gfortran:以下在 ifort 中有效,但在 gfortran 中无效

logical :: a
real :: b
a = .true.
b = dble(a)

The error thrown in gfortran is gfortran 中抛出的错误是

b = dble(a)
         1
Error: 'a' argument of 'dble' intrinsic at (1) must be a numeric type

Obviously, .true.显然,.true。 should map to 1.d0, and .false.应该映射到 1.d0 和 .false。 to 0.d0.到 0.d0。 What's the best way of doing this?这样做的最佳方法是什么?

In addition to writing a function to handle this, you could also directly use the intrinsic merge function: b = merge(1.d0, 0.d0, a) .除了编写一个函数来处理这个问题,您还可以直接使用内部合并函数: b = merge(1.d0, 0.d0, a) Or you could write a defined assignment subroutine that does this, so that you can just type b = a .或者您可以编写一个已定义的赋值子例程来执行此操作,以便您只需键入b = a

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.我不知道为什么 ifort 接受这个,我的猜测是它是编译器特定的功能。

Edit: As pointed out in https://stackoverflow.com/a/15057846/1624033 below, there is the intrinsic merge function which is exactly what is needed here.编辑:正如下面的https://stackoverflow.com/a/15057846/1624033所指出的,有一个内在的合并函数,这正是这里所需要的。

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:我没有测试过这个,但以下可能有效:

elemental pure double precision function logic2dbl(a)
  logical, intent(in) :: a

  if (a) then
    logic2dbl = 1.d0
  else
    logic2dbl = 0.d0
  end if
end function logic2dbl

Edit: I added elemental to the function declaration based on advice below.编辑:我根据以下建议在函数声明中添加了元素 I also added pure to this function as it adds the extra ability to use this in parallel situations and it is good documentation.我还为这个函数添加了pure ,因为它增加了在并行情况下使用它的额外能力,并且它是很好的文档。 This however is just my opinion and it is not necessary.然而,这只是我的意见,没有必要。

In gfortran, I am using the TRANSFER intrinsic for that type of job.在 gfortran 中,我将 TRANSFER 内在函数用于该类型的工作。 Assuming a integer variable my_int then:假设一个整数变量 my_int 然后:

    my_int = transfer(.false.,my_int)

the result of my_int is 0 as expected. my_int 的结果如预期的那样为 0。

Just a note, TRANSFER(.true.,1) correctly returns the value of 1 with Gfortran and (incorrectly?) values of -1 with the current versions of Intel and Portland compilers.请注意, TRANSFER(.true.,1) 使用 Gfortran 正确返回 1 的值,使用当前版本的 Intel 和 Portland 编译器(错误地?)返回 -1 的值。 Interestingly, TRANSFER(-1,logical) returns TRUE with the latter two compilers while throws a syntax error with Gfortran.有趣的是,后两个编译器的 TRANSFER(-1,logical) 返回 TRUE,而 Gfortran 则抛出语法错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM