Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

declaring parameterised real variables

Status
Not open for further replies.

billgray1234

Programmer
Mar 14, 2011
39
i program in Fortran 90. i have a question about declaring parameterised real variables, as follows.

first, to enable portability between different computers, i'm declaring real variables using the KIND parameter, according to

integer, parameter :: PN_RL = selected_real_kind(p=15)
real(KIND=PN_RL) :: B,C,D etc

where PN_RL stands for 'PrecisioN of ReaL numbers', and B,C,D are real variables.


i have two questions. one involves 'no trailing zeros', and the other involves 'trailing zeros'. the questions are given below.


NO TRAILING ZEROS
i have a number which is 1) real, and 2) constant throughout my program (i.e. i want to declare it as a 'parameter'). for example, the number is B=3.0
from everything i've read on this topic, there doesn't appear to be just one 'correct' way to declare such a variable.
so, my question is:- are any (or all) of the following valid ways to declare this variable? if so, then which one(s)? if there is more than one 'valid' answer, then please say ALL valid answers.

real(KIND=PN_RL),parameter :: B = 3.
real(KIND=PN_RL),parameter :: B = 3.0

real(KIND=PN_RL),parameter :: B = 3._PN_RL
real(KIND=PN_RL),parameter :: B = 3.0_PN_RL


TRAILING ZEROS
similar to the above, but the number is instead B=3.456, and i would like to 'add' some trailing zeros (i will explain why below).
so, my question is:- are any (or all) of the following valid ways to declare this variable? if so, then which one(s)? again, if there is more than one 'valid' answer, then please say ALL valid answers.

real(KIND=PN_RL),parameter :: B = 3.456
real(KIND=PN_RL),parameter :: B = 3.4560

real(KIND=PN_RL),parameter :: B = 3.456_PN_RL
real(KIND=PN_RL),parameter :: B = 3.4560_PN_RL


the reason why i would like to 'add' some trailing zeros is solely for 'appearance', when i'm declaring several variables whose number of decimals is different. for example,

real(KIND=PN_RL),parameter :: B = 3.4560
real(KIND=PN_RL),parameter :: B = 1.2345
real(KIND=PN_RL),parameter :: B = 7.1200

looks neater (to me) than

real(KIND=PN_RL),parameter :: B = 3.456
real(KIND=PN_RL),parameter :: B = 1.2345
real(KIND=PN_RL),parameter :: B = 7.12

also, if (as asked above) the term _PN_RL must be used at the end of each number (in the above), then

real(KIND=PN_RL),parameter :: B = 3.4560_PN_RL
real(KIND=PN_RL),parameter :: B = 1.2345_PN_RL
real(KIND=PN_RL),parameter :: B = 7.1200_PN_RL

looks neater (to me) than

real(KIND=PN_RL),parameter :: B = 3.456_PN_RL
real(KIND=PN_RL),parameter :: B = 1.2345_PN_RL
real(KIND=PN_RL),parameter :: B = 7.12_PN_RL

any help would be appreciated
 
You may add trailing zero if you want. they do not matter.

But you need to use the form with _PN_RL as tail, even if such tail is too long for me.

After that, declaring these values PARAMETER or not is often a question of taste. A parameter is just a value : this is not variable.

Demonstration :

Code:
program test

  integer,parameter :: dp=selected_real_kind(15)
  
  real(dp) :: a=7.256
  real(dp) :: b=7.2560
  real(dp) :: c=7.256_dp
  real(dp) :: d=7.2560_dp
  
  write(*,"(F0.15)") a
  write(*,"(F0.15)") b
  write(*,"(F0.15)") c
  write(*,"(F0.15)") d

end program

Result

Code:
[lcoul@localhost test]$ ifort t41.f90
[lcoul@localhost test]$ ./a.out
7.256000041961670
7.256000041961670
7.256000000000000
7.256000000000000


François Jacq
 
thanks for that reply

in response to your reply:-

1) i agree with you, that the tail term '_PN_RL' is rather long. however, in the context of the program that i'm writing, it happens to be the shortest (and most clear/understandable) term i can use.

2) in regard to your 'demonstration program', why is it that the number a=7.256 prints out as 7.256000041961670, rather than 7.256000000000000 (which is what the number c=7.256_dp prints out)?
 
2) in regard to your 'demonstration program', why is it that the number a=7.256 prints out as 7.256000041961670, rather than 7.256000000000000 (which is what the number c=7.256_dp prints out)?

Because the instruction "real(dp) :: a=7.256" initializes the variable "a" (double precision) with the value "7.256" which is a real single precision value (i.e. a value with a limited number of significant digits, usually 6-7 and here, by miracle, 8).

In fact, the compiler translates this instruction into something like : "a=DBLE(7.256)" which firstly transforms the single precision value into a double precision value before setting the variable "a". But the intrinsic function DBLE cannot reconstitute the precision initially lost by the use of a single precision value.

The same behavior will occur in your program each time you will mix real values having different precisions (in any type of instruction).

This is one of the main sources of precision leak.



François Jacq
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top