avatar
fortran90的超级怪问题# Computation - 科学计算
g*s
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都对。
什么原因?先谢了!
avatar
f*r
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都对。
: 什么原因?先谢了!

avatar
g*s
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
avatar
r*y
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.

avatar
g*s
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.

avatar
r*y
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

avatar
g*s
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

avatar
l*n
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

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。