ENG 213.1 · Computer Programming for Engineers
Our notes
2 min readArithmetic operators
| Operator | Meaning | Example |
|---|---|---|
| ** | power | x**2 |
| * | multiply | a * b |
| / | divide | a / b |
| + | add | a + b |
| − | subtract | a - b |
Order of evaluation
- Brackets first.
- Powers, evaluated right to left: 2 to the power 3 to the power 2 is 2⁹ = 512, not 8² = 64.
- * and /, left to right.
- + 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
| Function | Meaning |
|---|---|
| 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
| Formula | Fortran |
|---|---|
| v = √(2gh) | v = sqrt(2.0 * g * h) |
| Re = ρvD/μ | re = rho * v * d / mu |
| σ = Mc/I | sigma = m * c / i_beam |
| A = πd²/4 | a = pi * d**2 / 4.0 |
| P = V²/R | p = 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
-
Order of operations
(opens en.wikipedia.org in a new tab)
Wikipedia, Order of operations, CC BY-SA 4.0 · licence
-
Expression (computer science)
(opens en.wikipedia.org in a new tab)
Wikipedia, Expression (computer science), CC BY-SA 4.0 · licence
Watch
-
Best pick Ep 4: A Scientific Calculator at Native Speed | Arithmetic and Intrinsic Functions
15 min · ≈ 68 MB · about ₦38
(opens youtube.com in a new tab)
-
Operators, Functions and Parameters in FORTRAN | Tutorial - 6
7 min · ≈ 32 MB · about ₦18
(opens youtube.com in a new tab)
-
Simple arithmetic using Fortran: Addition,Substraction,Multiplication,Division,Mod,Floor,Ceiling
9 min · ≈ 41 MB · about ₦23
(opens youtube.com in a new tab)
-
[Fortran Tuto 3] Simple Math and Real Variables
12 min · ≈ 54 MB · about ₦30
(opens youtube.com in a new tab)
-
Symbols for the basic arithmetic operations of FORTRAN
2 min · ≈ 9 MB · about ₦5
(opens youtube.com in a new tab)
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.