Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

real/dbl precision question

lklawrie1
Beginner
899 Views
Sort of an "odd" question. Actually, two questions.
1) I've been searching through the documentation but not finding what I want. Real (4) precision says that numbers up to 1E38 (?) are good but only 7 digits of precision (from my recollection, details may not be correct).
=> is a 1E-30 constant going to be "okay" in single precision calculations?
2) if you compile "double" (i.e. promote all Real(4) to Real(8), and have a real parameter constant as 1E-30 or 1E-10, does that automatically get converted to a 1D-30 or 1D-10 constant?
How stringent (ok, 3 questions) must one be on putting in a constant as a 1D vs 1E if you specify something as Double precision?
Linda
0 Kudos
4 Replies
Steven_L_Intel1
Employee
899 Views
1. 1E-30 is within range of single precision. I don't know what you mean by "okay".

2. Constants of the form 1E-30 are of "default real kind". If you use the -r8 switch (/real_size:64), then "default real kind" changes to 8 (double). Therefore, 1E-30 will become a double precision constant and the only way to force a constant to be single is to add the _4 kind specifier.

3. If you're passing arguments, you must get the "kind" right. If you're using constants in expressions, you'll get default conversions from single to double, but sometimes the results might surprise you, depending on the values. It's best not to give the compiler the opportunity to do a default conversion - specify all the constant kinds consistently.
0 Kudos
lklawrie1
Beginner
899 Views
Steve replied: 1. 1E-30 is within range of single precision. I don't know what you mean by "okay".
If you put it in as .0000000000001, would that still be okay? Vs entering it as 1.E-30
?: How stringentmust one be on putting in a constant as a 1D vs 1E if you specify something as Double precision?
i.e. if you have DOUBLE PRECISION, PARAMETER :: xx =1E-30
does the compiler recognize that as a 1D-30?
I'm sure it may depend on the compiler so I suppose I should make up a test I can run on several.
Linda


0 Kudos
TimP
Honored Contributor III
899 Views
1e-30 gives you full precision of default real, as it is larger than TINY(1.). If you can type all those zeros without making a mistake, and without running into issues from a wide source line, you should get the same internal result that way.
If you have set one of the options which promotes to double precision, 1E-30 should produce identical results to 1D-30. By default (without any special options), the Fortran standard requires that 1E-30 should be rounded first to default precision, then promoted to double. Then 1E-30 and 1d-30 will differ beyond the first 6 decimal places. So you can create confusion for yourself and others, when you mix constant types.
0 Kudos
Steven_L_Intel1
Employee
899 Views
In regard to your question about PARAMETER - no, if you type 1E-30 that is a single precision value which is then converted to double precision before being assigned to the PARAMETER constant, just as in normal assignment.

The modern style is to not use E or D at all, but rather to define some constants for appropriate kinds and to use those constants consistently. For example:

integer, parameter :: rk = selected_real_kind(6)
integer, parameter :: dk = selected_real_kind(15)

real(dk), parameter :: pi = 3.141592653589723_dk
0 Kudos
Reply