Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

NaN result is inconsistent

Kipling__Michael
Beginner
960 Views

The Sdrnd routine below is part of a much larger program. When the program is run for a particular set of input, which results in the Sdrnd input of X = 0, and N = 4, the result is 0 the first two times the function is called, but the third time and each time after that, the result is NaN.

I fixed a similiar problem with a NaN being produced elsewhere in the program instead of a very small number, by using the compile option /fp:precise for the project.

I can do a work-around by bypassing calculations for an input of zero, but am afraid that a similiar problem may exist elsewhere. Do you have any other suggestions.

I've added the build log for routine below.

Thanks,

Mike

FUNCTION Sdrnd( x, n )

!!!

!-----------------------------------------------------------------------

! PURPOSE: Rounds X to N number of decimal places

! Examples: (49.9938,1) input rounds to 50.0

! (49.9938,2) input rounds to 49.99

!-----------------------------------------------------------------------

! PARAMETERS IN CALL LIST

! NAME TYPE #DIM DESCRIPTION

! ------ ---- ---- -----------

! INPUT:

! x R*8 decimal number

! n I*4 number of decimal points after rounding

!

! OUTPUT:

! Sdrnd R*8 rounded decimal number

!-----------------------------------------------------------------------

!!!

IMPLICIT NONE

INTEGER, PARAMETER :: dbl = SELECTED_REAL_KIND(14)

! List of calling arguments:

REAL(KIND=dbl), INTENT(IN) :: x

INTEGER, INTENT(IN) :: n

REAL(KIND=dbl) :: Sdrnd

! List of local variables:

REAL (KIND=dbl) :: y = 0.0

!*** Begin execution:

Sdrnd = 0.0

IF ( n == 0 ) y = 1.0d0

IF ( n == 1 ) y = 1.0d1

IF ( n == 2 ) y = 1.0d2

IF ( n == 3 ) y = 1.0d3

IF ( n == 4 ) y = 1.0d4

IF ( n == 5 ) y = 1.0d5

IF ( n == 6 ) y = 1.0d6

IF ( n == 7 ) y = 1.0d7

IF ( n == 8 ) y = 1.0d8

IF ( n == 9 ) y = 1.0d9

Sdrnd = ANINT( x * y ) / y

RETURN

END FUNCTION Sdrnd

Build Log

Build started: Project: CLASH_Fsrc, Configuration: Debug|Win32

Output

Compiling with Intel Visual Fortran 11.0.074 [IA-32]...

ifort /nologo /debug:full /Od /fp:precise /module:"Debug\\\\" /object:"Debug\\\\" /traceback /check:bounds

/libs:dll /threads /dbglibs /c /Qvc8

/Qlocation,link,"C:\\Program Files\\Microsoft Visual Studio 8\\VC\\bin" "D:\\ClearCase\\views\\cl_mwk_vs\\clash\\fsrc_f90\\Sdrnd.f90"

ifort: command line warning #10212: /fp:precise evaluates in source precision with Fortran.

CLASH_Fsrc - 0 error(s), 1 warning(s)

0 Kudos
2 Replies
DavidWhite
Valued Contributor II
960 Views

Apparently Random NaN results are often due to memory problems - are the arguments properly defined and initialized in your calling program? Are the arguments the same type in the calling program and subroutine.

Regards,

David

0 Kudos
Kipling__Michael
Beginner
960 Views

The calling routine uses initialized variables of the correct type. There may be memory problems somewhere in the 110,000 lines of program Fortran code, or the 140,000 lines of C/C++ code, but this area is OK.

From other posts I've read, it appears that there is sometimes a problem when zero is divided by a number.

Mike

0 Kudos
Reply