whole number constants
  Home FAQ Contact Sign in
comp.lang.fortran only
 
Advanced search
POPULAR GROUPS

more...

comp.lang.fortran Profile…
 Up
whole number constants         


Author: dancerchris
Date: Jul 21, 2008 14:16

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

! 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
Show full article (0.83Kb)
36 Comments
Re: whole number constants         


Author: Craig Powers
Date: Jul 21, 2008 14:20

dancerchris@earthlink.net wrote:
> integer, parameter :: dp = selected_real_kind(15, 307)
>
> real(dp), parameter :: onend = 1_dp

This form is hazardous, because 1_dp is an integer number with kind DP,
which may or may not be a proper integer kind.
no comments
Re: whole number constants         


Author: dancerchris
Date: Jul 21, 2008 14:27

On Jul 21, 2:20 pm, Craig Powers wrote:
> dancerch...@earthlink.net wrote:
>> integer, parameter :: dp = selected_real_kind(15, 307)
>
>> real(dp), parameter :: onend = 1_dp
>
> This form is hazardous, because 1_dp is an integer number with kind DP,
> which may or may not be a proper integer kind.

Yes, thanks for reminding me, I have run into that issue already.

I still would like to understand what the best practice is. writing
global constants for whole numbers like 1 seems redundant.
no comments
Re: whole number constants         


Author: Dick Hendrickson
Date: Jul 21, 2008 14:30

dancerchris@earthlink.net 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
>
Show full article (2.01Kb)
no comments
Re: whole number constants         


Author: dancerchris
Date: Jul 21, 2008 14:49

On Jul 21, 2:30 pm, Dick Hendrickson att.net> wrote:
> dancerch...@earthlink.net 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 ...
Show full article (2.34Kb)
1 Comment
Re: whole number constants         


Author: dancerchris
Date: Jul 21, 2008 14:52

On Jul 21, 2:49 pm, dancerch...@earthlink.net wrote:
> On Jul 21, 2:30 pm, Dick Hendrickson att.net> wrote:
>
>
>
>
>
>> dancerch...@earthlink.net 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
> ...
Show full article (2.71Kb)
no comments
Re: whole number constants         


Author: nospam
Date: Jul 21, 2008 15:00

earthlink.net> wrote:
> I still would like to understand what the best practice is. writing
> global constants for whole numbers like 1 seems redundant.

It pretty much is. You will have no precision loss from expressing whole
numbers of "reasonable" magnitude in any actual compiler. It doesn't
matter whether you express it as a real or an integer, and it doesn't
matter what kind.

There are two cases where it does matter.

1. When you start doing operations. There is a big difference between
1/3.0 and 1/3.0d0. There is an even bigger difference between these and
1/3.

2. In procedure arguments, you must have the type and kind correct.
That's pretty much all there is to say about that. There is no single
type and kind that is always the best one. You just have to use the
correct one for the particular procedure in question.
Show full article (3.22Kb)
no comments
Re: whole number constants         


Author: nospam
Date: Jul 21, 2008 15:27

earthlink.net> wrote:
>> 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
> I understand they may never be equal but then I am trying to establish
> a truly constant value to two so they are equal not just to the "dp"
> accuracy.
Show full article (1.77Kb)
no comments
Re: whole number constants         


Author: glen herrmannsfeldt
Date: Jul 21, 2008 16:33

dancerchris@earthlink.net wrote:
(snip)
> ! 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
Show full article (1.34Kb)
no comments
Re: whole number constants         


Author: dancerchris
Date: Jul 21, 2008 15:51

On Jul 21, 3:27 pm, nos...@see.signature (Richard Maine) wrote:
>
> I think you are confusing yourself by inventing concepts that don't
> exist. There is no such thing as a distinction between a "truly
> constant" value and any other constant. A constant is a constant.

Thanks RM. I do understand that a parameter has a constant value. My
question was the relationship of inline usage of a number like label
50 of my original post. Do compilers use the same value for multiple
use of an integer in an equation of specified kind

for example if I specify a constant:

real(dp), parameter :: one = 1.0_dp
real(dp) :: x,xnew,error

100 error = one-xnew/x+one
200 error = 1.0_dp-xnew/x+1.0_dp
300 error = 1-xnew/x+1
Show full article (1.24Kb)
no comments
1 2 3 4