Fortran

Guide To Learn

Writing user-defined operators for the Field type

In chapter 8, we implemented the tsunami solver using the Field derived type to model physical fields such as water height and velocity. This allowed us to initialize the fields and express the solver equation cleanly and concisely, as shown in the following listing. Listing 9.9 Main tsunami solver using the Field derived type ❶ Imports the type from the […]

Redefining built-in operators

To wrap up the chapter, in this section we’ll practice redefining Fortran’s built-in operators to do something more or other than originally intended. For example, this could involve either of the following: I leave this one as an exercise for you (“Exercise 2” sidebar). It’s a fairly new concept, but we’ve already covered all the […]

Writing custom operators

A custom operator is essentially a procedure under the hood, with an extended syntax that allows it to be used in expressions much like built-in operators: Once we have a function or a subroutine, it’s straightforward to implement it as a custom operator. Tip Don’t confuse the custom operator syntax, .op. with the logical literal constants .true. or .false.. The latter […]

Operator precedence

An important aspect of operators, and a common source of bugs for novice programmers, is operator precedence. In other words, which operation gets to go first, and which last? Fortran has a few simple rules for arithmetic operator precedence: Comparison operators don’t suffer from precedence ambiguity because they operate on numeric or character values, and […]

Fortran’s built-in operators

Fortran comes with a number of built-in operators. Each falls into one of four categories, depending on what type of result they produce: There’s also the special assignment operator =. What’s special about it is that rather than returning a value of some data type, it acts to store the result on the right side into […]

Things to do with operators

There are four main aspects to Fortran operators: In this chapter, we’ll cover the built-in operators (item 1) in more detail, and we’ll learn how to express a function or subroutine as a custom operator (item 2). We’ve used built-in operators extensively since chapter 2 throughout the book but haven’t given much attention to what […]

What’s an operator?

An operator is a special creature in most programming languages. All expressions are made of some combination of literal constants, variables, function calls, and, you guessed it, operators. Like in math, operators are used to combine values (numeric or otherwise), to compute a new value. In fact, we’ve been working with operators since chapter 2 without giving […]

Scroll to top