! The smallest number, that added to x produces a number >x. 
! Double precision.
Program ULP_x

implicit real(8) (a-h, o-z)
character(260) fname, fname1, w*1


99 format(71(1H-))

print '(a, \)', 'Input file name (NO extension): '
read '(a)', fname
fname1 =trim(fname)//'.rez'

open (1, file= fname1, status ='unknown')
write(1,*) ' ULP(x) =2**(exponent(x) -53):'
write(1,*)
write(1,*) ' x1 =x +ULP(x)'
write(1,*) ' x2 =x +ULP(x)/2.'


100 print '(a\)',' Input xStart, xEnd (0=stop), xStep: '
read*, xStart, xEnd, xStep
if(xEND ==0.) STOP

write(1,*)
ULP_ant =0.

DO x =xStart, xEnd, xStep

ULPx =ULP(x)	! ULP(x) =smallest add to 'x'
x1 =x +ULPx
x2 =x +ULPx/2.

if(ULPx /=ULP_ant) write(1, 99)

write(1,*)
write(1,*) ' x:',x
write(1,*) ' exponent(x)=',exponent(x),'; fractie(x)=', fraction(x)
write(1,*) ' ULP(x)=', ULPx
write(1,*)
write(1,*) ' x1=',x1,';	fractie(x1)=', fraction(x1)
write(1,*) ' x2=',x2,';	fractie(x2)=', fraction(x2)

if(x1 ==x) then
write(1,*) ' x1=x',';	x1-x =',x1-x
elseif(x1 >x) then
write(1,*) ' x1>x',';	x1-x =',x1-x
else
write(1,*) ' x1<x',';	x1-x =',x1-x
endif

if(x2 ==x) then
write(1,*) ' x2=x', ';	x2-x =', x2-x
elseif(x2 >x) then
write(1,*) ' x2>x', ';	x2-x =', x2-x
else
write(1,*) ' x2<x',';	x2-x =',x2-x
endif

ULP_ant =ULPx
ENDDO


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

END

