! 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;
real(16) x16, x16_max, x16_min; real(16) x16_min_den

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

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

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

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

if(kx <16) then
write(kind_char, '(I1)') kx
endif

if(C ==' ') then
C ='x'
endif

if(kx <16) then
fname =trim(C)//'-'//kind_char//'.rez'
else
fname =trim(C)//'-16.rez'
endif

open(1, file =fname, status ='unknown', form ='formatted')
write(1, '(a)') ' Reprezentarea numerelor reale'
if(kx <16) then
write(kind_char, '(I1)') kx
write(1, '(a,/)') ' Tipul: REAL('//kind_char//')' 

else
write(1, '(a,/)') ' Tipul: REAL(16)' 
endif

x4=1E0; x8=1D0; x16 =1Q0;


x4_max =huge(x4); x4_min =tiny(x4);
imin4 = minexponent(x4)-1; x4_min_den =2.0**(imin4-23); !Emin -(P-1)
imax4 = maxexponent(x4)-1

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

x16_max =huge(x16); x16_min =tiny(x16);
imin16 = minexponent(x16)-1; x16_min_den =2.Q0**(imin16-112);
imax16 = maxexponent(x16)-1

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, t49,a,I4)') ' xmax:', x4_max, 'Emax =' ,imax4
write(1,'(a, 1pE11.3, 4x,1pE11.3,a, t49,a,I4/)') ' xmin:', x4_min, x4_min_den, ' (denormal)', 'Emin =' ,imin4
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, t53,a,I5)') ' xmax:', x8_max, 'Emax =', imax8
write(1,'(a, 1pD13.3, 4x,1pD11.3,a, t53,a,I5/)') ' xmin:', x8_min, x8_min_den, ' (denormal)','Emin =', imin8
bias =1023; iwbias =4;

case(16)
write(*,'(a, 1pD13.3)') ' xmax:', x16_max
write(*,'(a, 1pD13.3, 4x,1pD11.3, a,/)') ' xmin:', x16_min, x16_min_den, ' (denormal)' 

write(1,'(a, 1pD13.3, t53,a,I6)') ' xmax:', x16_max, 'Emax =', imax16
write(1,'(a, 1pD13.3, 4x,1pD11.3,a, t53,a,I6/)') ' xmin:', x16_min, x16_min_den, ' (denormal)', 'Emin =', imin16
bias =16383; iwbias =5;

end select

write(1,'(/,a)')  ' bx: reprezentarea in format'

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

!iT = T ca intreg
!xT = T ca real (pentru REAL(16))
if(kx /=16) then
write(1,'(a)')   'iT = T ca intreg'
else
write(1,'(a)')   'xT = T ca real (pentru REAL(16))'
endif

!E_stocat  = exponentul cu deplasare; E = exponent;
!Sig = semnificand; (Fr = fractia in model)

write(1,'(/, a)')   'E_stocat  = exponentul cu deplasare; E = exponent;'
write(1,'(a, /,a)')   '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

case(16)
!read (*,*, iostat =io), x16
read(char_x, *, iostat =io) x16

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, x16)

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
