Basic Mathematics for Programming
Every line of code you write involves mathematics, whether you realise it or not. When you split a dataset into training and test sets, you are doing division. When you normalise pixel values from 0-255 to 0-1, you are doing division and subtraction. When you calculate a loss function, you are combining multiplication, subtraction and summation. This lesson covers the foundational arithmetic operations you need to be fluent in before tackling AI mathematics.
Operator Precedence: PEMDAS/BODMAS
When an expression contains multiple operators, the order of evaluation matters. Python follows the standard mathematical convention known as PEMDAS (or BODMAS in South Africa):
- P/B -- Parentheses / Brackets (evaluated first)
- E/O -- Exponents / Orders (powers, roots)
- MD/DM -- Multiplication and Division (left to right)
- AS/AS -- Addition and Subtraction (left to right)
Consider this expression:
result = 3 + 4 * 2 ** 2 - 1
Step by step:
- Exponent first:
2 ** 2 = 4 - Multiplication next:
4 * 4 = 16 - Addition:
3 + 16 = 19 - Subtraction:
19 - 1 = 18
The answer is 18, not 27 or any other value you might get by reading left to right. When in doubt, use parentheses to make your intention explicit:
result = 3 + (4 * (2 ** 2)) - 1 # Same result, but much clearer
In AI code, explicit parentheses are not just good practice -- they prevent subtle bugs in loss functions and gradient calculations.
Integer Division vs Float Division
Python has two division operators and confusing them is one of the most common sources of bugs in data processing code.
Float division (/) always returns a decimal result:
10 / 3 # 3.3333333333333335
10 / 2 # 5.0 (still a float, even though the result is whole)
Integer (floor) division (//) truncates towards negative infinity:
10 // 3 # 3
7 // 2 # 3
-7 // 2 # -4 (not -3! It rounds towards negative infinity)
This matters in AI because when you split a dataset of 1000 samples into batches of 32, you get 1000 // 32 = 31 full batches with 1000 % 32 = 8 samples left over. If you use regular division, you get 31.25 -- which is not a valid number of batches.
The Modulus Operator
The modulus operator (%) returns the remainder after division:
10 % 3 # 1 (because 10 = 3*3 + 1)
15 % 5 # 0 (15 divides evenly by 5)
7 % 2 # 1 (odd numbers always give remainder 1 when divided by 2)
Common uses in programming and AI:
- Checking even/odd:
if n % 2 == 0means n is even - Cycling through indices:
index % lengthwraps around (useful in circular buffers and ring attention) - Batch processing:
remaining = total_samples % batch_size - Logging intervals:
if epoch % 10 == 0: print(loss)logs every 10th epoch
Type Mixing: Integers and Floats
When you combine an integer and a float in an operation, Python automatically converts the result to a float:
5 + 2.0 # 7.0 (float)
3 * 1.5 # 4.5 (float)
10 - 0.0 # 10.0 (float)
type(5 + 2.0) # <class 'float'>
This automatic promotion is called type coercion. It matters in AI because NumPy and TensorFlow are strict about data types. If you accidentally mix int32 and float64 arrays, you might get unexpected type promotions that consume more memory or cause type errors.
import numpy as np
a = np.array([1, 2, 3]) # int64 by default
b = np.array([1.0, 2.0, 3.0]) # float64 by default
c = a + b # float64 -- the int array was promoted
Be explicit about your types when performance matters:
a = np.array([1, 2, 3], dtype=np.float32) # Use float32 for ML
Why This Matters for AI
These basic operations appear everywhere in machine learning:
- Learning rate updates:
weight = weight - learning_rate * gradient(subtraction, multiplication) - Batch normalisation:
normalised = (x - mean) / std(subtraction, division) - Cost functions:
mse = sum((predicted - actual) ** 2) / n(subtraction, exponent, summation, division) - Accuracy calculation:
accuracy = correct // total-- wait, this is wrong! You need float division:accuracy = correct / total
That last example shows exactly why understanding the difference between // and / matters. Using integer division for accuracy would give you 0 whenever correct < total, which is most of the time during training.
Order of Operations in Real AI Code
Here is a real formula from machine learning -- the sigmoid function:
sigmoid = 1 / (1 + math.exp(-x))
The parentheses are critical. Without them:
# WRONG: 1 / 1 + math.exp(-x) = 1.0 + math.exp(-x)
# Division happens before addition without parentheses
Every formula you encounter in AI -- gradient descent, backpropagation, attention scores -- relies on correct operator precedence. Build the habit now of using parentheses to make complex expressions unambiguous.
Summary
- Python follows PEMDAS/BODMAS for operator precedence
- Use
/for float division and//for integer (floor) division - The modulus operator
%returns remainders and is essential for batch processing - Mixing int and float types produces float results automatically
- Always use parentheses in complex expressions to avoid subtle bugs
- These operations are the atoms that make up every AI algorithm