Re: whole number constants
- From: *** Hendrickson <***.hendrickson@xxxxxxx>
- Date: Mon, 21 Jul 2008 21:30:53 GMT
dancerchris@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 ?This would be my choice, because it's clearer about
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
what you are doing.
The bad thing about getting in the habit of using just
plain constants is with dummy arguments. Here, you need
to specify the kind value as there is no automatic conversion.
*** Hendrickson
.
! or the slightly less ledgable ?
60 error = 1_dp-xnew/x
Thanks for any advice.
- Follow-Ups:
- Re: whole number constants
- From: robin
- Re: whole number constants
- From: dancerchris
- Re: whole number constants
- References:
- whole number constants
- From: dancerchris
- 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):