Re: whole number constants
- From: dancerchris@xxxxxxxxxxxxx
- Date: Mon, 21 Jul 2008 14:49:41 -0700 (PDT)
On Jul 21, 2:30 pm, *** Hendrickson <***.hendrick...@xxxxxxx> wrote:
dancerch...@xxxxxxxxxxxxx wrote:
This may be a dumb question but what is "best practice" when coding
whole numbers in real (kind specified) formulas. I am looking for
accuracy, consistancy and prortability reagardless of what value dp
has below. Many of my calcs have numerical cancellations or accuracy
checks as per the error function below. What are the pitfalls?
for example:
integer, parameter :: dp = selected_real_kind(15, 307)
real(dp) :: x,xnew
real(dp), parameter :: one = 1.0_dp
real(dp), parameter :: onend = 1_dp
real(dp), parameter :: onenk = 1.0
real(dp), parameter :: onei = 1
For real constants that are exact integers and are small enough
to fit in one word, those 4 are (almost) exactly equivalent.
No processor in the world will possibly mess up the conversion
of 1 or 1.0 or 1.0_dp into some other kind. My advice would
be to do what looks natural, as long as you declare the result
kind, via the "real(dp)".
The interesting exception here is the "1_dp" form. This
declares an integer of kind dp. It's not guaranteed that
this will be portable. Many processors only support one kind
of integer and the selected_real_kind refereence you used
to get the value of "dp" isn't likely to return a valid
integer kind.
As you almost certainly know, it's different for values that
aren't exactly representable. 3.14 and 3.14_dp are not the
same thing (or at least aren't portably the same).
! which should I use ?
10 error = one-xnew/x
20 error = onend-xnew/x
30 error = onenk-xnew/x
40 error = onei-xnew/x
! or the more ledgable ?
50 error = 1-xnew/x
This would be my choice, because it's clearer about
what you are doing.
I like label 50 as well but then what about cancellation, for example:
real(dp), parameter :: pi = 3.1415926535_dp
real(dp), parameter :: two = 2.0_dp
real(dp) :: x,y
! this looks good/ledgable but.....
x = 2*pi
y = x/2
! does the following result...
if (y == pi) then
! aha cancellation
else
! integer value of two assigned different values of 'extra' digits
! (beyond precision of dp) ergo not a constant
end if
Sorry to be so nitpicky.
.
- Follow-Ups:
- Re: whole number constants
- From: glen herrmannsfeldt
- Re: whole number constants
- From: dancerchris
- Re: whole number constants
- References:
- whole number constants
- From: dancerchris
- Re: whole number constants
- From: *** Hendrickson
- whole number constants
- Prev by Date: Re: whole number constants
- Next by Date: Re: whole number constants
- Previous by thread: Re: whole number constants
- Next by thread: Re: whole number constants
- Index(es):