Convert logical type to double in Fortran

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). Or you could write a defined assignment subroutine that does this, so that you can just type 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.

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.

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. This however is just my opinion and it is not necessary.