How can i do convert an integer value to a real value in Fortran?

75 Views Asked by At

I don't know how to coding a 4-byte integer value to a 4-byte real. in C Language ... I want to do something like this: float value = *((float*)&intValue))

I would like to complete the code following....

  real function castingInt32_to_real( intValue )

  implicit none
  integer,intent(in) :: intValue
  
  !
  ! if intValue == 1065353216(0x3f800000)  Its means 1.0 on Intel.

  !I want to do following....
  castingInt32_to_real <-- 1.0
  
  end function
1

There are 1 best solutions below

0
steve On BEST ANSWER

Given a BOZ entity, you can convert that to real with the real intrinsic subprogram. Consider,

program main
   real x
   x = real(z'3f800000', kind(1.e0))
   print *,x
end program main

which gives with gfortran

% gfortran -o z a.f90
% ./z
   1.00000000

As an addendum, you can also use the transfer intrinsic subprogram.

program main
   real x
   x = transfer(1065353216,x)
   print *,x
end program main

The above essentially does a bit-wise transfer.