Silverfrost Forums

Welcome to our forums

format mismatch

15 Jul 2024 9:01 #31399

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.

16 Jul 2024 6:01 #31400

Please post some code that illustrates the failure together with the command line options used when compiling.

16 Jul 2024 9:35 #31401

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
16 Jul 2024 10:00 #31402

I can see the problem using Kenneth's code and will add it to the list of things to investigate.

16 Jul 2024 10:48 #31403

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

16 Jul 2024 2:15 #31406

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.

17 Jul 2024 5:43 #31410

jib

That is the point that is being made and I will investigate shortly.

17 Jul 2024 7:37 #31414

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
17 Jul 2024 7:55 #31415

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.

17 Jul 2024 8:04 #31416

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.

17 Jul 2024 9:36 #31417

Paul,

Also B with real data.

implicit none
real    :: number = 123.987
  write(*,'(B32.32)') number
end

Ken

17 Jul 2024 9:51 #31419

Thanks. Yes.

18 Jul 2024 7:16 #31427

This feature has now been added for the next release of the DLLs.

Please login to reply.