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

Why do I get different results if I break a line with 2 lines?

ronghewang
Beginner
1,124 Views
I have a program. Sometime it works, sometime it does not. The code is:
ACOF = TAU * BETA/(2.0 * G * ATOP**2)

at sometime, the result is
ACOF =NaN.

But if when I break the line as 2 lines:
ACOF = 2.0 * G * ATOP**2
ACOF = TAU * BETA/ACOF

ACOF will always get a valid value.

I have hundreds test cases, when I change just this line, I get dozens of failures.

Any comments and suggestions on this mysterious problem for me?

Thanks
Victor Wang

0 Kudos
11 Replies
Peter
Beginner
1,124 Views
Quoting - ronghewang
I have a program. Sometime it works, sometime it does not. The code is:
ACOF = TAU * BETA/(2.0 * G * ATOP**2)

at sometime, the result is
ACOF =NaN.

But if when I break the line as 2 lines:
ACOF = 2.0 * G * ATOP**2
ACOF = TAU * BETA/ACOF

ACOF will always get a valid value.

I have hundreds test cases, when I change just this line, I get dozens of failures.

Any comments and suggestions on this mysterious problem for me?

Thanks
Victor Wang


Victor,

Can you please give input

I gave input as follows
parameter (tau=1.567d0,beta=2.0d0,g=34.987d0,atop=5.67d0)

i got ACOF=1.39314e-3

I think you migth have messed with type declaration.
0 Kudos
ronghewang
Beginner
1,124 Views
Quoting - ronghewang

Such as:

Tau =-1.000
Beta = 1.58000
G=32.2000
ATOP = 97.72558

then ACOF= -2.5689442E-06
If I used the single line, it will get: NaN.

Thanks,
Victor

0 Kudos
ronghewang
Beginner
1,124 Views
Quoting - peter
Hi Peter,

To make it clear, I copied the debug info as following:

TAU -1.000000 REAL(4)
BETA 1.580000 REAL(4)
G 32.20000 REAL(4)
ATOP 97.72558 REAL(4)
ACOF NaN REAL(4)
TAU * BETA/(2.0 * G * ATOP**2) -2.5689442E-06

Thanks,
Victor

0 Kudos
Peter
Beginner
1,124 Views
Victor,

Here is my code

program roghewang
real*4 ACOF,TAU,BETA, G , ATOP
parameter (Tau =-1.000,Beta = 1.58000,G=32.2000,ATOP = 97.72558)
ACOF = TAU * BETA/(2.0 * G * ATOP**2)
write(*,*) acof
end

I get -2.5689e-6 when i run the above code. Did you use format statement while writing output?
0 Kudos
ronghewang
Beginner
1,124 Views
Quoting - Peter

Hi peter,

I do not use the output. I just get the weird value.

Another finding: If I change the settings for "Fortran > Run-Time > Check Array and String Bounds" from "No" to "Yes", I can get the valid value. Why?

Thanks,
Victor
0 Kudos
Peter
Beginner
1,124 Views
Quoting - ronghewang
Quoting - Peter

Hi peter,

I do not use the output. I just get the weird value.

Another finding: If I change the settings for "Fortran > Run-Time > Check Array and String Bounds" from "No" to "Yes", I can get the valid value. Why?

Thanks,
Victor

Victor,

when you say'yes' to 'check array and string bounds',Fortran generates code to check for array subscripts.
It is all about checking if an array subscript is declared beyond the declared bounds of the array. Are you using subroutines, it might be with your dummy variables!

And do you get any error message when you get 'nan' for ACOf?
0 Kudos
ronghewang
Beginner
1,124 Views
I really use subroutines, and I do not get any error/warning messages. In the subroutine, I have a loop which it works for the first few iterations, but it will break in the middle.

Thanks,
Victor
0 Kudos
Peter
Beginner
1,124 Views
Victor,


It might meanyour array is exceeding out of bounds. Fortran wont check array boundsfor arrays that are dummy arguments in which the last dimension bound is specified as * or when both upper and lower dimensions are 1.

For more information refer to compiler documentation and search for 'run time' and click on the second result ('check').

I tried to complile the previous code by calling a subroutine, but it doesnt show me any 'nan' values. Can you send me the subroutine that calls ACOF value??
0 Kudos
ronghewang
Beginner
1,124 Views
Quoting - Peter
Victor,


It might meanyour array is exceeding out of bounds. Fortran wont check array boundsfor arrays that are dummy arguments in which the last dimension bound is specified as * or when both upper and lower dimensions are 1.

For more information refer to compiler documentation and search for 'run time' and click on the second result ('check').

I tried to complile the previous code by calling a subroutine, but it doesnt show me any 'nan' values. Can you send me the subroutine that calls ACOF value??
Hi Peter,

I checked according your suggestion, and I can not find any problem for the array bounds and uninitialized variables. I got the random problem.

Such as the simple subroutine:

SUBROUTINE QUAD2 ( A,B,C,X,IRET )
IF ( B .EQ. 0. ) GOTO 99
E = -4.0 * A * C / B**2
IF ( E .LT. -1.0 ) GOTO 99
AE = ABS(E) - 0.1
IF ( AE .LE. 0. ) THEN
! X = -(C/B) *(1.0-E*(0.25-E*(0.125-E*0.078125)))
X = 0.125-E*0.078125
X = 0.25-E*X
X= -(C/B) *(1.0-E*X)
ELSE
X = SQRT(1.0 + E) - 1.0
X = (B / (2.0*A)) * X
ENDIF
IRET = 0 ! Root calculated
GOTO 200
99 WRITE (6,199) ! Error
199 FORMAT (' Entry error in subroutine "QUAD2" ')
write ( 6, '( a, 4e13.5 ) ') ! Error
* ' a, b, c and e are: ' , a, b, c, e
IRET = 1 ! Root not obtained.
200 RETURN
END

You will find that I broke "X = -(C/B) *(1.0-E*(0.25-E*(0.125-E*0.078125)))" as 3 lines. I think there is merrory problem for me.

Can anybody tell me how to find the problem?

Regards,
Victor
0 Kudos
Peter
Beginner
1,124 Views
Victor,

Ido not havedata for the variables in your subroutine. However I used my data and tried two cases.
Case 1. using X = -(C/B) *(1.0-E*(0.25-E*(0.125-E*0.078125)))
Case 2. Using your 3 part equations.

For both the cases I get 0.39541 for 'X' . Following is the code slightly modified.

Note: 1. Implicit real*8 (a-z) is declared for my convenience. Its is not a good practice to use it very often
2. In Run-time it is Yes to 'check array subscrips and substrings'

[cpp]program main
implicit real*8 (a-z)
call quad2(u,v,w,y,z)
write(*,10) y
10 format(2(/),f10.5,2(/))
end 

SUBROUTINE QUAD2 ( A,B,C,X,IRET )
implicit real*8 (a-z)
a= 2.4
b=1.58
c=-1.d0
IF ( B .EQ. 0.) GOTO 99
E = -4.d0 * A * C / B**2
IF ( E .LT. -1.d0 ) GOTO 99
AE = ABS(E) - 0.1
IF ( AE .LE. 0.) THEN
X = -(C/B) *(1.0-E*(0.25-E*(0.125-E*0.078125)))
!X = 0.125-E*0.078125
!X = 0.25-E*X
!X= -(C/B) *(1.d0-E*X)
ELSE
X = SQRT(1.d0 + E) - 1.d0
X = (B / (2.d0*A)) * X
ENDIF
IRET = 0 ! Root calculated 
GOTO 200
99 WRITE (6,199) ! Error
199 FORMAT (' Entry error in subroutine "QUAD2" ')
write ( 6, '( a, 4e13.5 ,2(/)) ') ' a, b, c and e are: ' , a, b, c, e !! Error
IRET = 1 ! Root not obtained.
200 RETURN
END

[/cpp]

Try running this code and see if you get the same answer as mine. Otherwise we can check memory problems.
0 Kudos
Jugoslav_Dujic
Valued Contributor II
1,124 Views
Quoting - ronghewang
I have a program. Sometime it works, sometime it does not. The code is:
ACOF = TAU * BETA/(2.0 * G * ATOP**2)

at sometime, the result is
ACOF =NaN.

I have hundreds test cases, when I change just this line, I get dozens of failures.

Any comments and suggestions on this mysterious problem for me?


Victor, could you please zip and attach your source code, project files, and one test case where it fails? (Here are instructions for attaching -- it's a bit counterintuitive). It's almost impossible to tell what went wrong solely by looking at the code.


0 Kudos
Reply