! Reprezentarea in Format a numerelor REALE
! Valori x, admise la intrare:
! 1) Numere finite (normale; denormale)
! 2) 0.0; -0.0
! 3) infinity; -infinity
! 4) nan
!
! Se va complia cu optiunea: Enable IEEE Minus Zero Support: Yes

Program Main_Represent

real(4) x4, x4_max, x4_min; real(4)  x4_min_den;
real(8) x8, x8_max, x8_min; real(8) x8_min_den;

character(8) C, fname*100, kind_char*1
character(20) char_x
integer bias


print '(a,\)', ' REAL(kind). Introdu "kind" (4;8): '
read*, kx

if(kx /=4 .and. kx /=8 .and. kx /=16) then
kx =4
write(*,*) ' "kind" invalid!'
write(*,*) ' S-a luat kind =4.' 
endif

print '(/,a)', ' Nume fisier de iesire = "Repr-C.rez"'
print '(a,\)', ' Introdu C (<=8 char): '
read '(a)', C

if(C /=' ') then
fname ='Repr-'//trim(C)//'.rez'
else
fname ='Repr.rez'
endif

open(1, file =fname, status ='unknown', form ='formatted')
write(1, '(a)') ' Reprezentarea numerelor reale'

write(kind_char, '(I1)') kx
write(1, '(a,/)') ' Tipul: REAL('//kind_char//')' 


x4=1E0; x8=1D0; 

x4_max =huge(x4); x4_min =tiny(x4);
imin4 = minexponent(x4); x4_min_den =2.0**(imin4-1-23);

x8_max =huge(x8); x8_min =tiny(x8);
imin8 = minexponent(x8); x8_min_den =2.D0**(imin8-1-52);

select case(kx)
case(4)
write(*,'(a, 1pE11.3)') ' xmax:', x4_max
write(*,'(a, 1pE11.3, 4x,1pE11.3, a,/)') ' xmin:', x4_min, x4_min_den, ' (denormal)' 
write(1,'(a, 1pE11.3)') ' xmax:', x4_max
write(1,'(a, 1pE11.3, 4x,1pE11.3, a,/)') ' xmin:', x4_min, x4_min_den, ' (denormal)'
bias =127; iwbias =3;

case(8)
write(*,'(a, 1pD13.3)') ' xmax:', x8_max
write(*,'(a, 1pD13.3, 4x,1pD11.3, a,/)') ' xmin:', x8_min, x8_min_den, ' (denormal)' 
write(1,'(a, 1pD13.3)') ' xmax:', x8_max
write(1,'(a, 1pD13.3, 4x,1pD11.3, a,/)') ' xmin:', x8_min, x8_min_den, ' (denormal)' 
bias =1023; iwbias =4;

end select

write(1,'(a)')  '  S: bit de semn'
write(1,'(a)')  'Exp: camp exponent'
write(1,'(a,/)')'Sig: camp semnificand'

write(1,'(a, I<iwbias>,a)')   '(E  = exponent; bias =', bias, ')'
write(1,'(a)')   '(iSig = intreg stocat; Sig =semnificand; Fr =fractia in model.)'
write(1, '(a,/)') '---------------------------------------------------------------'

100 print '(a,\)', ' Introdu x: '
read (*,*) char_x

select case(kx)
case(4)
!read (*,*, iostat =io) x4
read(char_x, *, iostat =io) x4

case(8)
!read (*,*, iostat =io), x8
read(char_x, *, iostat =io) x8

end select

if(io >0) then
write(*, *) ' Valoare invalida!'
write(1, '(a,a,/)') ' x = '//trim(char_x), ': Valoare invalida!'
goto 100
endif

call Represent(kx, x4, x8)

print '(//,a,\)', ' Alt "x" (Y,y/char): '
read '(a1)', char1

if (char1 =='Y' .or. char1 =='y') then
write(1,*)
goto 100
endif

ENDProgram Main_Represent
