I have a problem when trying to determine the internal hexadecimal represent of variables. Everything is fine if the variable was declared to be INTEGER. However if the variable was declared to be REAL the following error message is received at run time: FORMET mismatch; Z format with REAL item. Compiling with Plato does not generate an error message.
format mismatch
Please post some code that illustrates the failure together with the command line options used when compiling.
You can use TRANSFER to convert the bit pattern of the real number to an integer number, and then print that integer in hex.
implicit none
real :: number
integer :: i
character(len=8) :: hex_str
character(len=32) :: binary_str
number = 123.987
! Convert bit pattern to integer using TRANSFER and write as hex
i = transfer(number, i)
write(hex_str, '(Z8.8)') i
! Convert bit pattern to binary string for printing
call int2bin(i, binary_str)
print*, number
print*, binary_str
print*, hex_str
end
subroutine int2bin(input_int, bin_str)
integer, intent(in) :: input_int
character(len=32), intent(out) :: bin_str
integer :: j, temp_int
temp_int = input_int
do j = 32, 1, -1
if (iand(temp_int, 1) .eq. 1) then
bin_str(j:j) = '1'
else
bin_str(j:j) = '0'
end if
temp_int = ishft(temp_int, -1)
end do
end subroutine int2bin
!Remember the internal binary representation will include a sign bit, bits for the exponent, and bits for the mantissa.
Returns
123.987
01000010111101111111100101011000
42F7F958
I can see the problem using Kenneth's code and will add it to the list of things to investigate.
I did not see an error in my example, but then I missed the really obvious case:
implicit none
real :: number = 123.987
print'(Z8)', number
end
which should also return 42F7F958
Kenneth Your last ('obvious') example code compiles but doesn't run (FORMAT mismatch: Z-format with REAL item), on both Win32 and x64, FTN95 version is 9.03.0.0.
jib
That is the point that is being made and I will investigate shortly.
Paul Sorry, my mistake, I did not read Kenneth's comments on the 'obvious' case carefully enough.
Kenneth Is there a reason for using the subroutine int2bin instead of the 'B' format?
write(*, '(1X,B32.32)') i
jlb, int2bin is the solution to one of a series of simple Fortran tutorial exercises I have been developing recently, so it came to mind before the 'B' format.
It looks like the use of Z with REAL data is included in the 2008 Standard but not before.
At first sight this would require a non-trivial extension to the FTN95 library but I will add it to the wish list.
Paul,
Also B with real data.
implicit none
real :: number = 123.987
write(*,'(B32.32)') number
end
Ken
Thanks. Yes.
This feature has now been added for the next release of the DLLs.