Understanding Fortran's Kinds: A Guide to Type Parameters and Their Meanings

  • Fortran
  • Thread starter MathematicalPhysicist
  • Start date
In summary, the author provides a programming code that determines the kind parameters that are available on the system. The code prints the available kind parameters.
  • #1
MathematicalPhysicist
Gold Member
4,699
371
I read the following paragraph in the book: "Guide to Fortran 2008 programming":
It is possible to determine which kind parameters are available for each type on
your system by using the parameters real_kinds , integer_kinds , logical_kinds ,
and character_kinds in the intrinsic module iso_fortran_env .

And then they provide a programming code:
Fortran:
program kinds
use iso_fortran_env
implicit none
print *, real_kinds
end program kinds

I can of course change real to integer to logical to character etc.

But I don't understand what are the numbers that are shown in the screen? what do they designate?

For real_kinds I get:
4 8 10 16

For logical_kinds I get:
1 2 4 8 16

For character_kinds I get:
1 4

and for integer_kinds the same as logical.

What do these numbers designate?

The text doesn't clarify it to me.
 
Technology news on Phys.org
  • #3
@jedishrfu I think I get it now.
For the real there are four kinds: single,long,double and quadruple.

In the book for real there are only 3 kinds for real, but it's a bit outdated the book. Here's the paragraph:
The kind parameter is an integer. These numbers are processor dependent, so that
kind parameters 1, 2, and 3 might be single, double, and quadruple precision; or on a
different system, kind parameters 4, 8, and 16 could be used for the same things. There
are at least two real and complex kinds and at least one kind for the integer, logical,
and character data types. There must be an integer kind capable of representing all 18-
digit integers. Note that the value of the kind parameter is not usually the number of
decimal digits of precision or range; on many systems, it is the number of bytes used to
represent the value.
 
  • #4
Yes, it mentions the number of bytes as the last sentence.
 
  • #5
It's been more than 10 years since I wrote any Fortran, (and I never used Fortran beyond F77) but I think the values being returned are the declarable byte values for the variables.

For example, LOGICAL*1 would declare a logical variable using 1 byte, REAL*16 would be a 16 byte REAL variable, and so on.

[edit] jedishrfu beat me to this. evidently I'm not the only old Fortran guy on here.
 
  • #6
Mono Kakata said:
It's been more than 10 years since I wrote any Fortran, (and I never used Fortran beyond F77) but I think the values being returned are the declarable byte values for the variables.

For example, LOGICAL*1 would declare a logical variable using 1 byte, REAL*16 would be a 16 byte REAL variable, and so on.

[edit] jedishrfu beat me to this. evidently I'm not the only old Fortran guy on here.

I'm a robot, I don't age and I don't forget anything / Also Google is my personal friend. (Pay no attention to that man behind the curtain!)

My fortran was fortran-IV and a derivative called Fortran-Y after that it was C to shining C++ and now Java!
 

1. What is the purpose of type parameters in Fortran?

Type parameters in Fortran allow the user to specify the kind or precision of a variable. This can impact the accuracy and performance of calculations, making it a crucial aspect of scientific computing.

2. How do I declare a variable with a specific kind in Fortran?

To declare a variable with a specific kind in Fortran, you can use the "kind=" keyword in the variable declaration. For example, "real(kind=8) :: my_var" will declare a real variable with a kind of 8, which corresponds to a double precision floating point number.

3. Can I change the kind of a variable after it has been declared?

No, the kind of a variable cannot be changed after it has been declared. It is important to carefully choose the appropriate kind for your variables before using them in calculations.

4. What are the different kinds available in Fortran?

Fortran has several kinds available for different types of variables, including integer, real, character, and logical. The exact number and range of each kind may vary depending on the compiler being used.

5. How can I ensure my code is portable across different platforms with different kinds?

To ensure portability of your code, it is best to use the intrinsic functions "selected_int_kind", "selected_real_kind", and "selected_char_kind" to determine the appropriate kind for your variables based on the desired precision. These functions will return the appropriate kind value for the current platform, making your code more portable.

Similar threads

  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
2
Replies
37
Views
3K
  • Programming and Computer Science
Replies
4
Views
616
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
7
Views
3K
  • Programming and Computer Science
Replies
20
Views
5K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
4
Views
2K
Back
Top