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

sqrt

keremusu1
ビギナー
2,219件の閲覧回数

Hello!

I've gottwo real(8) type variables X and Y. When I calculate Y=sqrt(X), thereappears noproblem with the value of Y. But when I multiply Y with another real(8) number, I cannot get the true result.

What do I need to do to obtain true result of this multiplication.

0 件の賞賛
11 返答(返信)
Jeffrey_A_Intel
従業員
2,219件の閲覧回数

As with your previous question about using the LOG function, a smallexample program or some sample code which shows the problem is necessary before anyone can giverelevant advice.

keremusu1
ビギナー
2,219件の閲覧回数

program

question

implicit none

parameter

wp = KIND( 1.0D0 )

real (8) n,a,y,g,q0,q17,q19,q12,q13,q1

real (wp) T1,T2,q18

n=10e21_8

a=2.5_8

y=0.01_8

g=0.129_8

T1=0.096_wp

T2=209000_wp

q0=((a**2)+(b**2)+(c**2))/8

q17=(n*(a**2))/(8*q0)

q18=y/(1+(g*

log(T2/T1)))

q19=4*

sqrt(2*q0)

q12=q17*q18*q19

q13=g*(T2-T1)

q1=q12*q13

end program question

Everything isOK until q1. But q1 is not calculatedcorrectly. What is the reason?

TimP
名誉コントリビューター III
2,219件の閲覧回数
ifort -check all sqrtbug.f90
fortcom: Error: sqrtbug.f90, line 23: This name does not have a type, and must have an explicit type.
q0=((a**2)+(b**2)+(c**2))/8
------------^
fortcom: Error: sqrtbug.f90, line 23: This name does not have a type, and must have an explicit type.
q0=((a**2)+(b**2)+(c**2))/8
-------------------^
compilation aborted for sqrtbug.f90 (code 1)
Steven_L_Intel1
従業員
2,219件の閲覧回数
Since the program includes IMPLICIT NONE, I would expect it to fail to compile even without switches. Note that -check is for run-time diagnostics.
Jeffrey_A_Intel
従業員
2,219件の閲覧回数

The variables b and c have not only not been declared butthey have not been givenvalues before they are used in the computation ofq0. Thus, any result calculated for q1 is meaningless.

keremusu1
ビギナー
2,219件の閲覧回数

Sorry for the missing part. Here it is again:

program

question

implicit none

parameter

wp = KIND( 1.0D0 )

real (8) n,a,b,c,y,g,q0,q17,q19,q12,q13,q1

real (wp) T1,T2,q18

n=10e21_8

a=2.5_8

b=2.4_8

c=2.3_8

y=0.01_8

g=0.129_8

T1=0.096_wp

T2=209000_wp

q0=((a**2)+(b**2)+(c**2))/8

q17=(n*(a**2))/(8*q0)

q18=y/(1+(g*

log(T2/T1)))

q19=4*

sqrt(2*q0)

q12=q17*q18*q19

q13=g*(T2-T1)

q1=q12*q13

end program question

Everything isOK until q1. But q1 is not calculatedcorrectly. What is the reason?

Steven_L_Intel1
従業員
2,219件の閲覧回数
Well, this program doesn't print anything, so how do you know the value is wrong? Even so, you have not said what is incorrect about the computation. From a glance at the values it looks correct to me. What values are you seeing for q12, q13 and q1 and what do you think they should be (and why)?
TimP
名誉コントリビューター III
2,219件の閲覧回数
gfortran -O -Wall sqrtbug.f90
sqrtbug.f90:5:

parameter wp = KIND( 1.0D0 )
1
Error: Unclassifiable statement at (1)
sqrtbug.f90:9.8:

real (wp) T1,T2,q18
1
Error: Symbol 'wp' at (1) has no IMPLICIT type
sqrtbug.f90:23.11:

T1=0.096_wp
1
Error: Missing kind-parameter at (1)
sqrtbug.f90:25.12:

T2=209000_wp
1
Error: Missing kind-parameter at (1)
sqrtbug.f90:31.3:

q18=y/(1+(g*log(T2/T1)))
1
Error: Symbol 'q18' at (1) has no IMPLICIT type
sqrtbug.f90:31.21:

q18=y/(1+(g*log(T2/T1)))
1
Error: Symbol 't1' at (1) has no IMPLICIT type
sqrtbug.f90:31.18:

q18=y/(1+(g*log(T2/T1)))
1
Error: Symbol 't2' at (1) has no IMPLICIT type

i.e. it looks like ifort failed to diagnose the failure to declare wp, so the program indeed is still non-compliant.
Steven_L_Intel1
従業員
2,219件の閲覧回数
Naw - that's just because gfortran doesn't support the VAX Fortran "no parentheses" PARAMETER syntax (or may need an option for it). Otherwise the program is valid.
keremusu1
ビギナー
2,219件の閲覧回数

I hadn't written the "write" part of the code not to preoccupy more space. The full version of the code is:

program question

implicit none

parameter

wp = KIND( 1.0D0 )

real (8) n,a,b,c,y,g,q0,q17,q19,q12,q13,q1

real (wp) T1,T2,q18

n=10e21_8

a=2.5_8

b=2.4_8

c=2.3_8

y=0.01_8

g=0.129_8

T1=0.096_wp

T2=209000_wp

q0=((a**2)+(b**2)+(c**2))/8

q17=(n*(a**2))/(8*q0)

q18=y/(1+(g*

log(T2/T1)))

q19=4*

sqrt(2*q0)

q12=q17*q18*q19

q13=g*(T2-T1)

q1=q12*q13

write (*,*) q1

end program question

There is no error about the "parameter" statement. The code runs well. But there is something wrong about the calculation of q1. The real value of it must be 2.810892534E24, but the calculated value is2.810892535844940E24.

What is the reason?

Steven_L_Intel1
従業員
2,219件の閲覧回数
The PARAMETER statement ought to be:

integer, parameter :: wp = kind(1.0D0)

or
integer wp
parameter (wp = kind(1.0D0))

The version you have is an extension.

That said, I don't know why you expect a different answer. As best as I can tell, you are getting a correct result. I reran the program in quad precision and it agreed with the double value to all the digits.

On what combination of computer, OS and compiler did you see the other answer?

You do have one interesting bug in your code, though it is harmless. You write:

T2=209000_wp

Since 209000 is an integer constant, you are getting 209000_8 (INTEGER(8)) which is then converted to REAL(8). This should really be:

T2 = 209000._wp

I can't see how this would change the result unless the other compiler you ran this on did not support INTEGER(8).

It would be useful for you to print all the variables and run it on both systems to see where the difference comes in.

返信