floating point - Adapting an existing underflow SO answer for my underflow error -
i have looked @ accepted answer question - how avoid underflow when multiplying real numbers , had question on how adapt situation. converting fixed format code free format code(modern fortran constructs) , 1 of modules throwing ieee_underflow floating point exception. compiling gfortran 5.4.
real(kind=sp) mdv,epsilon mdv = -999. epsilon = 1.12e-07 j=1,ny i=1,nx k=1,nz ! if ((abs(dtdz(i,j,k)-mdv).gt.eps).and.& (abs(dudz(i,j,k)-mdv).gt.eps).and.& (abs(dtdy(i,j,k)-mdv).gt.eps).and.& (abs(dvdz(i,j,k)-mdv).gt.eps).and.& (abs(rho3(i,j,k)-mdv).gt.eps).and. & (abs(dtdx(i,j,k)-mdv).gt.eps).and.& (abs(dvdx(i,j,k)-mdv).gt.eps).and.& (abs(dudy(i,j,k)-mdv).gt.eps)) pv3(i,j,k)=scale*1./rho3(i,j,k)*& ((dvdx(i,j,k)-dudy(i,j,k)+f2(i,j))*dtdz(i,j,k)& +dudz(i,j,k)*dtdy(i,j,k)*& -dvdz(i,j,k)*dtdx(i,j,k)) else pv3(i,j,k)=mdv endif enddo enddo enddo as requested these values on right hand side of program
scale=1000000,rho3=1.1385392,dvdx=1.33094426e-18,dudy=-5.52740852e-19 dtdz=3.42506403e-03,dudz=-4.76887450e-03,dtdy=-1.39981190e-19 dvdz=1.25626021e-03 dtdz=3.425064403e-03 the pv3(i,j,k) assignment underflow happening. have used same code accepted answer has provided except bit confused on role of below code
x = tiny(x) note underflow not cause problem on final result. able reproduce test data given me associate. question of handling underflow( if ain't broke why bother fixing ?) arise. answer using idealized data @ moment , concerned happen when use more realistic data.
update - have tried scaling @tim18 talks in comments , here attempt @ toy example , not sure whether overflow or not.
x = 1.0e-39 y = digits(x) = exponent(x) if (abs(exponent(x)) .gt. y+1) x = scale(x,exponent(x)) end if
Comments
Post a Comment