! The "Unit in the Last Place".
! The distance to the next FP number;
! precision: 4;8;16;
Program ULP_x
use ifcore
use comun

character(260) fname, fname1 
character(1) char1, w, char2*2

integer width
logical pressed/.false./

real(4) x40(3), x4

real(8) x80(3), x8
real(8) ULP_ant8, ULPx8, x18, x28

real(16) x160(3), x16
real(16) ULP_ant16, ULPx16, x116, x216



xn_min =2.0**Emin   ! 2^-126
xs_min =2.0**(Emin-P+1) ! 2^-149

xn_min8 =2.D0**Emin8   !
xs_min8 =2.D0**(Emin8-P8+1) !

xn_min16 =2.q0**Emin16   !
xs_min16 =2.q0**(Emin16-P16+1) !

gr(1) ='x1 >x'
eg(1) ='x1 =x'
lt(1) ='x1 <x'

gr(2) ='x2 >x'
eg(2) ='x2 =x'
lt(2) ='x2 <x'


90  print '(a\)',' Input real kind (4;8;16): '
read(*, *, iostat =ik) kind

if(ik /=0) then
write(*,*) 'Error: Input 4, or 8, or 16.'
goto 90
endif

if(.not. (kind ==4 .or. kind ==8 .or. kind ==16) ) then
kind =4
write(*,*) 'Invalid kind. Default kind =4 has been chosen.'
endif

print '(a, \)', 'Input file name (NO extension): '
read '(a)', fname
if(kind /=16) then
write(char1,'(I1)') kind
fname1 =trim(fname)//'-'//char1//'.rez'
else
write(char2,'(I2)') kind
fname1 =trim(fname)//'-'//char2//'.rez'
endif

open (1, file= fname1, status ='unknown')
write(1, '(a)') ' Format: x =s*2^E*Semnificand'

write(1,*)
write(1,*) 'ULP =2^(E+1-P)'
write(1,*) 'x1 =x +ULP(x)'
write(1,*) 'x2 =x +ULP(x)/2.'

iwb =32
iwe =8


100 print '(a\)',' Input xStart, xEnd (0=stop), xStep: '

SELECT CASE(kind)
CASE(4)
read*, x40
if(x40(3) ==0.) STOP

call Print_Ulp4(x40)
nh =45

CASE(8)
read*, x80
if(x80(3) ==0.) STOP

call Print_Ulp8(x80)
nh =61

CASE(16)
read*, x160
if(x160(3) ==0.) STOP

call Print_Ulp16(x160)
nh =89
END SELECT

99 format(<nh>(1H-))

print '(/a\)', ' Another Range? (Y,y / char): '
read '(a1)', w
if(w =='Y' .or. w=='y') then
goto 100
else
write(1,99)
print '(/, a, a)', 'Fisier de rezultate: ', trim(fname1)
endif

write(*, '(/,a)') 'Apasa orice tasta'
DO WHILE (.NOT. pressed)   
pressed = PEEKCHARQQ ( )
END DO

END

