Operators
Contents
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
andsur_name
to be your forname and surname respectively, - Assign
full_name
to be the concatenation offor_name
andsur_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 |
---|---|
|
returns the remainder of the division i/j |
|
returns the real version of the integer argument (casts integer to real) |
|
returns real kind k of integer |
|
absolute value of i |
|
returns the larger of i and j (can take a variable number of arguments) |
|
returns the smaller of i and j (can take a variable number of arguments) |
Function |
Description |
---|---|
|
returns integer part of floating-point number (cast real to integer) |
|
casts real to double precision (equivalent to real(a,k) with appropriate k) |
|
casts (truncates) double precision to single precision |
|
absolute value |
|
returns the smallest integer greater than or equal to its argument |
|
returns the largest integer less than or equal to its argument |
|
returns the maximum of the list of arguments |
|
returns the minimum of the list of arguments |
|
returns the remainder of the division a/b |
|
exponential (base e) of a |
|
natural logarithm (base e) of a |
|
common logarithm (base 10) of a |
|
square root |
|
sine |
|
cosine |
|
tangent |
|
arcsine |
|
arccos |
|
arctangent |
|
returns the principle value of the arctangent for the complex number (b,a) |
|
hyperbolic sine (not defined for complex argument) |
|
hyperbolic cosine (not defined for complex argument) |
|
hyperbolic tangent (not defined for complex argument) |
|
converts a real to a complex number with zero imaginary part |
|
converts a real to a complex number a1+ia2 |
|
imaginary part of complex number z |
|
real part of complex number z (casts complex to real) |
|
conjugate of complex number z |
Function |
Description |
---|---|
|
length |
|
length of c if it were trimmed |
|
returns .true. if s1 follows or is equal to s2 in lexical order, otherwise returns .false. |
|
returns .true. if s1 follows s2 in lexical order |
|
returns .true. if s2 follows or is equal to s1 in lexical order |
|
returns .true. if s2 follows s1 in lexical order |
|
returns string with leading blanks removed and the same number of trailing blanks added |
|
returns string with trailing blanks removed and the same number of leading blanks added |
|
concatenates string s to itself n times. Return variable should be declared large enough. |
|
returns the integer starting position of string c within string s or zero if it is not found |
|
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
Solution to Exercise 3
program ex3
implicit none
character(len=5) :: a = 'Space'
character(len=19) :: b = 'the final frontier!'
character(len=24) :: c
c = a // ' ' // b
print *, c
end program ex3
Solution to Exercise 3
program ex3
implicit none
character(len=5) :: a = 'Space'
! length determined on assignment
character(*), parameter :: b = 'the final frontier!'
! allocate length on allocation
character(len=:), allocatable :: c
character(len=20) :: space = 'Space'
c = a // ' ' // b
print *, c
! or
print *, trim(space) // ' ' // b
end program ex3
Exercise 4
Write a program that:
Adds two integers together
Adds an integer to a real number, casting the integer appropriately
Solution to Exercise 4
program ex4
implicit none
integer :: i = 1
integer :: j = 2
integer :: k
real :: b = 4.0
k = i + j
print *, k
c = real(i) + b ! Try to cast from low --> high pres
print *, c
end program ex4
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
Solution to Exercise 5
program ex5
implicit none
complex :: b = ( 1, 2 )
complex :: c = ( 1, 3 )
integer :: br, bi
complex :: d
br = real(b)
bi = aimag(b)
d = b + c
print *, br
print *, bi
print *, d
print *, b * conjg(c)
end program ex5