g******s 发帖数: 733 | 1 程序如下
PROGRAM MAIN
IMPLICIT NONE
real aa
aa=1023.0/2.0*1.0e-4
print*,aa
END PROGRAM
用f90 filename.f90 -o filename之后,filename出来的结果是0.051149997
用f77,把1023.0改成1021.0或1025.0,或者把*1.0e-4改成/1.0e4都对。
什么原因?先谢了! | f****r 发帖数: 27 | 2 It's called truncation error
【在 g******s 的大作中提到】 : 程序如下 : PROGRAM MAIN : IMPLICIT NONE : real aa : aa=1023.0/2.0*1.0e-4 : print*,aa : END PROGRAM : 用f90 filename.f90 -o filename之后,filename出来的结果是0.051149997 : 用f77,把1023.0改成1021.0或1025.0,或者把*1.0e-4改成/1.0e4都对。 : 什么原因?先谢了!
| g******s 发帖数: 733 | 3 i don't think it's truncation error. Even using double precision, the error
doesnot change. I believe sth is wrong in the codes or somewhere.
【在 f****r 的大作中提到】 : It's called truncation error
| r****y 发帖数: 1437 | 4 For single precision, the machine precision is about 1e-7. Your result
is correct within machine precision.
For double precision, if you explicitly use 2.0D0, 1.0D-4. You will
get higher precision than single precision. If just 2.0, then your compiler
might convert it to 2.0000011111.
Different compilers can be a problem as well. Recently I have a code
working perfectly on SGI, Sun UNIX, Linux gnu compiler, but Linux PGI compiler
will give me a wrong answer.
【在 g******s 的大作中提到】 : i don't think it's truncation error. Even using double precision, the error : doesnot change. I believe sth is wrong in the codes or somewhere.
| g******s 发帖数: 733 | 5 Thanks a lot for your post, it's very helpful. I just found the problem you
said, and another very similar problem is coming for double double precision,
or real*16 (kind=16). Say I want to give pi a value 3.1415926536, the value
would not change if I explicitly use "D0". However, if I declare pi to be kind
=16, the value will change even when I explicitely used "D0".
Here is the code,
PROGRAM MAIN
IMPLICIT NONE
real(kind=16) pi
pi=3.1415926536d0
print*,pi
END
the value will change to 3.14159265
【在 r****y 的大作中提到】 : For single precision, the machine precision is about 1e-7. Your result : is correct within machine precision. : For double precision, if you explicitly use 2.0D0, 1.0D-4. You will : get higher precision than single precision. If just 2.0, then your compiler : might convert it to 2.0000011111. : Different compilers can be a problem as well. Recently I have a code : working perfectly on SGI, Sun UNIX, Linux gnu compiler, but Linux PGI compiler : will give me a wrong answer.
| r****y 发帖数: 1437 | 6 real (kind=16) is not double precision, it is quadraple precision. That's
why
your 3.14159***D0 failed.
if you really have to consider the precision proble up to kind=16, then most
time it means your code is not well written. There must be other way to do it
.
Usually, should not specify inside code, just write down
real a
**********
Then when you compile, use switch -r8, or -r16. This would be better than
your in-code declaration.
Seems you are such a rookie. Hehe.
,
kind
【在 g******s 的大作中提到】 : Thanks a lot for your post, it's very helpful. I just found the problem you : said, and another very similar problem is coming for double double precision, : or real*16 (kind=16). Say I want to give pi a value 3.1415926536, the value : would not change if I explicitly use "D0". However, if I declare pi to be kind : =16, the value will change even when I explicitely used "D0". : Here is the code, : PROGRAM MAIN : IMPLICIT NONE : real(kind=16) pi : pi=3.1415926536d0
| g******s 发帖数: 733 | 7 thanks a lot! But I don't know how to separate the compiling and link commands
. I use "f90 filename.f90 -o filename" which has put the compiling and link
together; or "f77 -c [-O4] filename.f" which doesn't recognize the switch "-r8
" or "-r16". What kind of compile and link commands shall I use if I want to
add the switch -r8 or -r16?
most
it
【在 r****y 的大作中提到】 : real (kind=16) is not double precision, it is quadraple precision. That's : why : your 3.14159***D0 failed. : if you really have to consider the precision proble up to kind=16, then most : time it means your code is not well written. There must be other way to do it : . : Usually, should not specify inside code, just write down : real a : ********** : Then when you compile, use switch -r8, or -r16. This would be better than
| l******n 发帖数: 9344 | 8 write a makefile
commands
r8
【在 g******s 的大作中提到】 : thanks a lot! But I don't know how to separate the compiling and link commands : . I use "f90 filename.f90 -o filename" which has put the compiling and link : together; or "f77 -c [-O4] filename.f" which doesn't recognize the switch "-r8 : " or "-r16". What kind of compile and link commands shall I use if I want to : add the switch -r8 or -r16? : : most : it
|
|