Operators#

INTRO HERE?
Something order of operations?

Mathematical Operators#

Operator

Comment

+

addition

-

subtraction

/

division

*

multiplication

**

exponentiation

Note

Dividing an array elementwise by a real is more expensive than multiplying by the inverse of the real which is computed beforehand. Arrays will be introduced in section !!.

Logical Operators#

Listed in order of precedence the logical operators are:

.not.
.and.
.or.
.eqv.
.neqv.

Relational Operators#

== ! .eq.
/= ! .ne.
<  ! .lt.
>  ! .gt.
<= ! .le.
>= ! .ge.

Characters#

Characters can be concatenated using //, eg. a // b. A slice of a character variable can be accessed using word(i:j), which includes the charaters from index i up to and including index j.

Interactive

Use the interactive cell below to try concatenating two strings.

  • Assign the variables for_name and sur_name to be your forname and surname respectively,
  • Assign full_name to be the concatenation of for_name and sur_name.
  • Ensure there is a space inbetween them!
character(len=10) :: for_name
character(len=10) :: sur_name
character(len=10) :: full_name


for_name  = 
sur_name  = 
full_name = 

print *, full_name

Intrinsic Functions#

Fortran has built-in or intrinsic functions. Examples of these functions which oeprate on integers, real, and character types are contained in the tables below. There are many more intrinsics than those listed below, see the Fortran wiki page for more.

Function

Description

mod( i, j )

returns the remainder of the division i/j

real(i)

returns the real version of the integer argument (casts integer to real)

real( i, k )

returns real kind k of integer

abs(i)

absolute value of i

max( i, j )

returns the larger of i and j (can take a variable number of arguments)

min( i, j )

returns the smaller of i and j (can take a variable number of arguments)

Function

Description

int(a)

returns integer part of floating-point number (cast real to integer)

dble(a)

casts real to double precision (equivalent to real(a,k) with appropriate k)

real(d)

casts (truncates) double precision to single precision

abs(a)

absolute value

ceiling(a)

returns the smallest integer greater than or equal to its argument

floor(a)

returns the largest integer less than or equal to its argument

max( a, b [,c...] )

returns the maximum of the list of arguments

min( a, b [,c...] )

returns the minimum of the list of arguments

mod( a, b )

returns the remainder of the division a/b

exp(a)

exponential (base e) of a

log(a)

natural logarithm (base e) of a

log10(a)

common logarithm (base 10) of a

sqrt(a)

square root

sin(a)

sine

cos(a)

cosine

tan(a)

tangent

asin(a)

arcsine

acos(a)

arccos

atan(a)

arctangent

atan2( a, b )

returns the principle value of the arctangent for the complex number (b,a)

sinh(a)

hyperbolic sine (not defined for complex argument)

cosh(a)

hyperbolic cosine (not defined for complex argument)

tanh(a)

hyperbolic tangent (not defined for complex argument)

cmplx(a)

converts a real to a complex number with zero imaginary part

cmplx( a1, a2 )

converts a real to a complex number a1+ia2

aimag(z)

imaginary part of complex number z

real(z)

real part of complex number z (casts complex to real)

conjg(z)

conjugate of complex number z

Function

Description

len(c)

length

len_trim(c)

length of c if it were trimmed

lge( s1, s2 )

returns .true. if s1 follows or is equal to s2 in lexical order, otherwise returns .false.

lgt( s1, s2 )

returns .true. if s1 follows s2 in lexical order

lle( s1, s2 )

returns .true. if s2 follows or is equal to s1 in lexical order

llt( s1, s2 )

returns .true. if s2 follows s1 in lexical order

adjustl(s)

returns string with leading blanks removed and the same number of trailing blanks added

adjustr(s)

returns string with trailing blanks removed and the same number of leading blanks added

repeat( s, n )

concatenates string s to itself n times. Return variable should be declared large enough.

scan( s, c )

returns the integer starting position of string c within string s or zero if it is not found

trim(c)

trim trailing blanks from c (returns another character string)

Summary#

Exercises#

Exercise 3

Write a program that declares three character variables.

  • Initialize two of them to any desired constant strings, you should have already done this in Exercise 2

  • Concatenate those two and store the result into the third

  • Print the result

Exercise 4

Write a program that:

  • Adds two integers together

  • Adds an integer to a real number, casting the integer appropriately

Exercise 5

Write a program that declares some real and complex variables.

  • Initialize the complex variables

  • Store the real part of one complex variable into a real variable and its imaginary part into another real variable

  • Add two complex variables and store the result into another

  • Multiply one complex variable by the conjugate of another, print this value