Skip to main content
Engineering LibreTexts

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}}\)

    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.


    This page titled 5.2: Arithmetic Operations is shared under a CC BY-NC-SA 3.0 license and was authored, remixed, and/or curated by Ed Jorgensen via source content that was edited to the style and standards of the LibreTexts platform; a detailed edit history is available upon request.

    • Was this article helpful?