5.2: Arithmetic Operations
- Page ID
- 54256
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)
\( \newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\)
( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\)
\( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)
\( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\)
\( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)
\( \newcommand{\Span}{\mathrm{span}}\)
\( \newcommand{\id}{\mathrm{id}}\)
\( \newcommand{\Span}{\mathrm{span}}\)
\( \newcommand{\kernel}{\mathrm{null}\,}\)
\( \newcommand{\range}{\mathrm{range}\,}\)
\( \newcommand{\RealPart}{\mathrm{Re}}\)
\( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)
\( \newcommand{\Argument}{\mathrm{Arg}}\)
\( \newcommand{\norm}[1]{\| #1 \|}\)
\( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)
\( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\AA}{\unicode[.8,0]{x212B}}\)
\( \newcommand{\vectorA}[1]{\vec{#1}} % arrow\)
\( \newcommand{\vectorAt}[1]{\vec{\text{#1}}} % arrow\)
\( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vectorC}[1]{\textbf{#1}} \)
\( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)
\( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)
\( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)
\(\newcommand{\avec}{\mathbf a}\) \(\newcommand{\bvec}{\mathbf b}\) \(\newcommand{\cvec}{\mathbf c}\) \(\newcommand{\dvec}{\mathbf d}\) \(\newcommand{\dtil}{\widetilde{\mathbf d}}\) \(\newcommand{\evec}{\mathbf e}\) \(\newcommand{\fvec}{\mathbf f}\) \(\newcommand{\nvec}{\mathbf n}\) \(\newcommand{\pvec}{\mathbf p}\) \(\newcommand{\qvec}{\mathbf q}\) \(\newcommand{\svec}{\mathbf s}\) \(\newcommand{\tvec}{\mathbf t}\) \(\newcommand{\uvec}{\mathbf u}\) \(\newcommand{\vvec}{\mathbf v}\) \(\newcommand{\wvec}{\mathbf w}\) \(\newcommand{\xvec}{\mathbf x}\) \(\newcommand{\yvec}{\mathbf y}\) \(\newcommand{\zvec}{\mathbf z}\) \(\newcommand{\rvec}{\mathbf r}\) \(\newcommand{\mvec}{\mathbf m}\) \(\newcommand{\zerovec}{\mathbf 0}\) \(\newcommand{\onevec}{\mathbf 1}\) \(\newcommand{\real}{\mathbb R}\) \(\newcommand{\twovec}[2]{\left[\begin{array}{r}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\ctwovec}[2]{\left[\begin{array}{c}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\threevec}[3]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\cthreevec}[3]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\fourvec}[4]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\cfourvec}[4]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\fivevec}[5]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\cfivevec}[5]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\mattwo}[4]{\left[\begin{array}{rr}#1 \amp #2 \\ #3 \amp #4 \\ \end{array}\right]}\) \(\newcommand{\laspan}[1]{\text{Span}\{#1\}}\) \(\newcommand{\bcal}{\cal B}\) \(\newcommand{\ccal}{\cal C}\) \(\newcommand{\scal}{\cal S}\) \(\newcommand{\wcal}{\cal W}\) \(\newcommand{\ecal}{\cal E}\) \(\newcommand{\coords}[2]{\left\{#1\right\}_{#2}}\) \(\newcommand{\gray}[1]{\color{gray}{#1}}\) \(\newcommand{\lgray}[1]{\color{lightgray}{#1}}\) \(\newcommand{\rank}{\operatorname{rank}}\) \(\newcommand{\row}{\text{Row}}\) \(\newcommand{\col}{\text{Col}}\) \(\renewcommand{\row}{\text{Row}}\) \(\newcommand{\nul}{\text{Nul}}\) \(\newcommand{\var}{\text{Var}}\) \(\newcommand{\corr}{\text{corr}}\) \(\newcommand{\len}[1]{\left|#1\right|}\) \(\newcommand{\bbar}{\overline{\bvec}}\) \(\newcommand{\bhat}{\widehat{\bvec}}\) \(\newcommand{\bperp}{\bvec^\perp}\) \(\newcommand{\xhat}{\widehat{\xvec}}\) \(\newcommand{\vhat}{\widehat{\vvec}}\) \(\newcommand{\uhat}{\widehat{\uvec}}\) \(\newcommand{\what}{\widehat{\wvec}}\) \(\newcommand{\Sighat}{\widehat{\Sigma}}\) \(\newcommand{\lt}{<}\) \(\newcommand{\gt}{>}\) \(\newcommand{\amp}{&}\) \(\definecolor{fillinmathshade}{gray}{0.9}\)This section summarizes the basic arithmetic operations.
Assignments
In programming, assignment is the term for setting a variable equal to some value. Assignment is performed with an equal (=) sign. The general form is:
variable = expression
The expression may be a literal, variable, an arithmetic formula, or combination of each. Only one assignment to a single variable can be made per line.
For example, to declare the variable answer1
as a real value,
real :: answer1
and to set it equal to 2.71828183, it would be:
answer1 = 2.71828183
The value for answer1
can be changed as often as needed. However, it can only hold one value at a time.
Addition
The Fortran addition operation is specified with a plus sign (+). For example, to declare the variables, mysum
, number1
, number2
, and number3
,
integer :: mysum, number1=4, number2=5, number3=3
and calculate the sum,
mysum = number1 + number2
which will set the variable mysum
to 9 in this example. The data types of the variables, integer in this example, should be the same. Multiple variables can be added on one line. The line can also include literal values. For example,
mysum = number1 + number2 + number3 + 2
which will set the variable mysum
variable to 14. Additionally, it will write over the previous value of 9.
Subtraction
The Fortran subtraction operation is specified with a minus sign (-). For example, to declare the variables, ans
, value1
, value2
, and value3
,
real :: ans, value1=4.5, value2=2.5, value3=1.0
and calculate the difference,
ans = value1 – value2
which will set the variable ans
to 2.0. The data types of the variables, real in this example, should be the same. Multiple variables can be subtracted on one line. The line can also include literal values. For example,
ans = value1 - value2 – value3
which will set the variable ans
to 1.0. Additionally, it will over-write the previous value of 2.0.
Multiplication
The Fortran multiplication operation is specified with an asterisk (*). For example, to declare the variables, ans
, value1
, value2
, and value3
,
real :: ans, value1=4.5, value2=2.0, value3=1.5
and calculate the product,
ans = value1 * value2
which will set the variable ans
to 9.0. The data types of the variables, real in this example, should be the same. Multiple variables can be multiplied on one line. The line can also include literal values. For example,
ans = value1 * value2 * 2.0 * value3
which will set the variable ans
to 27.0. Additionally, it will over-write the previous value of 9.0.
Division
The Fortran division operation is specified with a slash symbol (/). For example, to declare the variables, ans
, value1
, value2
, and value3
,
real :: ans, value1=10.0, value2=2.5, value3=2.0
and calculate the quotient,
ans = value1 / value2
which will set the variable ans
to 4.0. The data types of the variables, real in this example, should be the same. Multiple variables can be divided on one line.
For example,
ans = value1 / value2 / value3
which will set the variable ans
to 2.0. Additionally, it will over-write the previous value of 4.0.
Exponentiation
Exponentiation means “raise to the power of”. For example, 2 to the power of 3, or \(2^3\) is (2 * 2 * 2) which is 8. The Fortran exponentiation operation is specified with a double asterisks (**
).
For example, to declare the variables, ans
and value1
,
real :: ans, value1=2.0
and calculate the exponentiation,
ans = value1 ** 3
which will set the variable ans
to 8.0. When using exponentiation, pay close attention to the data types. For example, raising an integer variable to the power 0.5 would produce a truncated integer result.