37

The manual of a program written in Fortran 90 says, "All real variables and parameters are specified in 64-bit precision (i.e. real*8)."

According to Wikipedia, single precision corresponds to 32-bit precision, whereas double precision corresponds to 64-bit precision, so apparently the program uses double precision.

But what does real*8 mean?

I thought that the 8 meant that 8 digits follow the decimal point. However, Wikipedia seems to say that single precision typically provides 6-9 digits whereas double precision typically provides 15-17 digits. Does this mean that the statement "64-bit precision" is inconsistent with real*8?

nbro
  • 15,395
  • 32
  • 113
  • 196
Andrew
  • 1,499
  • 9
  • 25
  • 37

3 Answers3

38

As indicated in comments, real*8 isn't standard Fortran. This FAQ entry has more details. That said, once we're into the non-standard realm...

The 8 refers to the number of bytes that the data type uses.

So a 32-bit integer is integer*4 along the same lines. (But is also non-standard.)

A quick search found this guide to Fortran data types, which includes:

The "real4" statement specifies the variable names to be single precision 4-byte real numbers which has 7 digits of accuracy and a magnitude range of 10 from -38 to +38. The "real" statement is the same as "real4" statement in nearly all 32-bit computers.

and

The "real8" statement specifies the variable names to be double precision 8-byte real numbers which has 15 digits of accuracy and a magnitude range of 10 from -308 to +308. The "double precision" statement is the same as "real8" statement in nearly all 32-bit computers.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 12
    -1 for Jon Skeet answering a Fortran question by quoting from an archaic and misleading reference. The other answers to this question demonstrate a better understanding of modern Fortran. – High Performance Mark May 24 '13 at 05:34
  • 12
    @HighPerformanceMark: Perhaps you could give a more useful link for me to change to then? I'm certainly not going to claim that I'm even slightly knowledgeable about Fortran, but is the core of the answer (that the 8 represents the number of bytes) actually incorrect or misleading? – Jon Skeet May 24 '13 at 14:28
  • This answer is seriously misleading because it appears to show that `integer*4` is actually Fortran. It is not Fortran at all, it has never been a part of any Fortran standard, not even any old one. – Vladimir F Героям слава Apr 28 '23 at 06:01
  • @VladimirFГероямслава: I'll readily admit that I'm not a Fortran programmer at all, but https://docs.oracle.com/cd/E19957-01/805-4939/6j4m0vna5/index.html suggests that `INTEGER*4` is part of Fortran 77. But I'd be happy to just take that line out of the answer entirely if it would be better. – Jon Skeet Apr 28 '23 at 06:13
  • It is a non-standard extension in an old Sun Fortran 77 compiler. The whole `*4` and `*8` is only valid for `character` types in standard Fortran 77. Not for `integer` or `real`, but it is a common extension that compilers are forced to implement. The standard documents are available in the [tag:fortran] tag description. – Vladimir F Героям слава Apr 28 '23 at 06:27
  • This [faq](https://www.nag.com/content/nag-fortran-compiler-frequently-asked-questions#faq32) also touches it. – Vladimir F Героям слава Apr 28 '23 at 06:32
  • @VladimirFГероямслава: Okay - given that the question is in *terms* of `real*8`, it feels reasonable to keep another similarly-non-standard reference in there. I'll edit the answer to refer to the FAQ and indicate that it's non-standard though. – Jon Skeet Apr 28 '23 at 06:41
26

There are now at least 4 ways to specify precision in Fortran.

As already answered, real*8 specifies the number of bytes. It is somewhat obsolete but should be safe.

The new way is with "kinds". One should use the intrinsic functions to obtain the kind that has the precision that you need. Specifying the kind by specific numeric value is risky because different compilers use different values.

Yet another way is to use the named types of the ISO_C_Binding. This question discusses the kind system for integers -- it is very similar for reals.

nbro
  • 15,395
  • 32
  • 113
  • 196
M. S. B.
  • 28,968
  • 2
  • 46
  • 73
  • 9
    UPDATE: the Fortran 2008 extension of the ISO environment module is now more commonly supported. So if you `use ISO_FORTRAN_ENV` you will have access to kind values that specify real and integer types by number of bits (e.g., `real (real32) ::` and `real (real64) ::`) as a standard & portable way of specifying types by storage size. – M. S. B. May 26 '13 at 07:00
16

The star notation (as TYPE*n is called) is a non-standard Fortran construct if used with TYPE other than CHARACTER.

If applied to character type, it creates an array of n characters (or a string of n characters).

If applied to another type, it specifies the storage size in bytes. This should be avoided at any cost in Fortran 90+, where the concept of type KIND is introduced. Specifying storage size creates non-portable applications.

nbro
  • 15,395
  • 32
  • 113
  • 196
Hristo Iliev
  • 72,659
  • 12
  • 135
  • 186