UNIHIVE

Study Hub UNIPORT ENG 213.1

ENG 213.1 · Computer Programming for Engineers

Arithmetic expressions and intrinsic functions

Operators and their order, integer division, mixed-mode arithmetic, and Fortran's built-in maths functions.

Our notes

2 min read

Arithmetic operators

OperatorMeaningExample
**powerx**2
*multiplya * b
/dividea / b
+adda + b
−subtracta - b

Order of evaluation

  1. Brackets first.
  2. Powers, evaluated right to left: 2 to the power 3 to the power 2 is 2⁹ = 512, not 8² = 64.
  3. * and /, left to right.
  4. + and −, left to right.

Example: 2 + 3 * 4 = 14; (2 + 3) * 4 = 20.

Integer division

When both operands are integers the result is an integer, with the fraction cut off:

  • 7 / 2 = 3; 7.0 / 2 = 3.5; 1 / 3 = 0.
  • mod(7, 2) = 1 (remainder).

This is a famous source of bugs: (1/2) * g * t * t always gives 0, because 1/2 is 0. Write 0.5 * g * t * t.

Mixed-mode expressions

If an integer and a real meet in one operation, the integer is converted to real first: 3 * 2.5 = 7.5. Convert explicitly with real(n) and int(x) (int cuts off), nint(x) (rounds).

Common intrinsic functions

FunctionMeaning
sqrt(x)square root
abs(x)absolute value
exp(x), log(x), log10(x)e^x, natural log, log base 10
sin(x), cos(x), tan(x)x in radians
asin, acos, atan, atan2(y, x)inverse trig, result in radians
max(a, b, ...), min(a, b, ...)largest, smallest
mod(a, b)remainder
real(n), int(x), nint(x)type conversion

Writing formulas in Fortran

FormulaFortran
v = √(2gh)v = sqrt(2.0 * g * h)
Re = ρvD/μre = rho * v * d / mu
σ = Mc/Isigma = m * c / i_beam
A = πd²/4a = pi * d**2 / 4.0
P = V²/Rp = v**2 / r

Worked example: free fall

With g = 9.81 m/s² and t = 3 s, d = 0.5 × g × t² = 0.5 × 9.81 × 9 = 44.145 m. Written with (1/2) in front, the program prints 0.0 because 1/2 is integer division.

Worked example: angle in degrees

To find sin 30°: sin(30.0 * pi / 180.0) = 0.5. sin(30.0) alone gives −0.988 (30 radians).

Exams usually ask

  • Evaluate Fortran expressions step by step, including integer division.
  • Write engineering formulas as Fortran statements.

Common mistake

Passing degrees to sin and cos. Fortran's trigonometric functions use radians.

Written by the UniHive team in our own words. Spotted a mistake? Tell us on WhatsApp.

Read

Watch

We link to free material and never copy it. Every link was opened by our team before it went up; video data is a rough guide at 360p.

All ENG 213.1 topics

Report

Sign in to report this. It is free.

Is this your work (a lecturer, author or publisher)? Ask us to take it down.