concept real in category fortran

appears as: real, reals
Modern Fortran: Building efficient parallel applications

This is an excerpt from Manning's book Modern Fortran: Building efficient parallel applications.

Table 1.1. Comparison between Fortran and Python (CPython specifically)

Language

Fortran

Python

First appeared

1957

1991

Latest release

Fortran 2018

3.8.1 (2019)

International standard

ISO/IEC

No

Implementation language

C, Fortran, Assembly (compiler dependent)

C

Compiled vs. interpreted

Compiled

Interpreted

Typing discipline

Static, strong

Dynamic, strong

Parallel

Shared and distributed memory

Shared memory only

Multidimensional arrays

Yes, up to 15 dimensions

Third-party library only (numpy)

Built-in types

character, complex, integer, logical, real

bool, bytearray, bytes, complex, dict, ellipsis, float, frozenset, int, list, set, str, tuple

Constants

Yes

No

Classes

Yes

Yes

Generic programming

Limited

Yes

Pure functions

Yes

No

Higher order functions

Limited

Yes

Anonymous functions

No

Yes

Interoperability with other languages

C (limited)

C

OS interface

Limited

Yes

Exception handling

Limited

Yes

This is where several elements of the language come to work together. In chapter 2, we wrote our first arithmetic expressions with built-in numeric types such as integer and real. In chapter 3, we learned how to write functions and subroutines to define these expressions as reusable miniprograms that we can invoke as many times as needed. In chapter 5, we worked with arrays, Fortran’s fundamental data collection. In chapter 7, we focused mostly on the concept of images and coarrays for parallel processing—we’ll take a break from these for this chapter. Finally, in chapter 8, you learned how to define your own custom data types of arbitrary complexity. In this chapter, we’ll tie these elements together in a powerful concept called generic procedures that will help you write cleaner and more expressive code. Generic procedures allow you to define a function or subroutine that accepts input arguments of different data types or shapes. We’ll wrap up the chapter with an intro to custom operators and overriding built-in operators, which will serve as a hook for chapter 10, where we’ll explore the power of user-defined operators for derived types.

Listing 9.1. A function to average real arrays
language="fortran" linenumbering="unnumbered">pure real function average_real(x) result(res)
  real, intent(in) :: x(:) #1
  res = sum(x) / size(x)   #2
end function average_real

This function takes a real, one-dimensional array x as input, computes the sum of all its elements (sum(x)), and divides it by the total number of elements (size(x)) to get us to the arithmetic average. Recall the rules about type coercion and mixed mode arithmetic from section 5.2.2. When you mix different numeric types (such as integer and real) in an expression, a variable or expression of a lower type is always promoted to the higher type (integer < real < complex) before evaluating the operation. Here, sum(x) evaluates to a real number because x is a real array, while size(x) always returns an integer. However, since here we’re dividing a real number by an integer, the integer is promoted to a real before the division operation is evaluated. Unlike before, I’ve appended the name of the input type (real) to the function name so we can set it apart from other specific functions.

Figure  9.2. From the real input array to the result of a generic function
ch09 generic procedures

We start with a real array of air temperature values. We pass this array to a generic average function. Thanks to static typing, the compiler knows the type of the input array (real), and it has a list of specific procedures that the interface average overrides: