Complex Number Calculator
Build an app that performs arithmetic on complex numbers — addition, subtraction, multiplication, division — and converts between rectangular (a+bi) and polar (r∠θ) forms.
🎯 Learning Goals
- ▹ Master the algebra of the Imaginary Unit 'i'
- ▹ Convert between Rectangular and Polar coordinate systems
- ▹ Calculate Modulus (magnitude) and Argument (angle)
- ▹ Implement trigonometry-based coordinate transformations
🌎 Why This Matters
Complex numbers are essential for modern electronics. Everything from your smartphone's wireless communication to the power lines on your street uses complex numbers for calculations. They allow engineers to describe 'waves' and 'oscillations' easily.
📖Understanding Complex Numbers
Theory MasterclassA complex number has two parts: a real part and an imaginary part. Written as: z = a + bi, where 'a' is real, 'b' is imaginary, and i = √(-1). Example: z₁ = 3 + 4i means a=3, b=4. Operations: Addition: (a+bi) + (c+di) = (a+c) + (b+d)i Subtraction: (a+bi) - (c+di) = (a-c) + (b-d)i Multiplication: (a+bi)(c+di) = (ac-bd) + (ad+bc)i Division: (a+bi)/(c+di) = [(ac+bd) + (bc-ad)i] / (c²+d²) Modulus: |z| = √(a² + b²) — the "distance" from origin Argument: arg(z) = arctan(b/a) — the angle from the positive x-axis Polar Form: z = r(cosθ + i·sinθ) = r∠θ where r = |z| (modulus) and θ = arg(z) (argument) Converting: Rectangular → Polar: r = √(a²+b²), θ = arctan(b/a) Polar → Rectangular: a = r·cosθ, b = r·sinθ
Mathematical Foundation
🎨Part A — Designer View (UI Design)
Open MIT App Inventor → Switch to Designer view. Follow each step below to build the interface.
Set up the screen
Title: "Complex Number Calculator". Dark theme.
Create Complex Number z₁ input
Label: "Complex Number z₁" HorizontalArrangement: TextBox RealA (Hint: "Real a"), Label " + ", TextBox ImagB (Hint: "Imag b"), Label "i" NumbersOnly = true for both TextBoxes.
Create Complex Number z₂ input
Label: "Complex Number z₂" Same layout: TextBox RealC, TextBox ImagD.
Add arithmetic operation buttons
Row 1: "z₁ + z₂", "z₁ - z₂" Row 2: "z₁ × z₂", "z₁ ÷ z₂" Use color coding: green for add, blue for subtract, orange for multiply, yellow for divide.
Add analysis buttons
Row 3: "Modulus z₁", "Arg z₁" Row 4: "To Polar", "To Rectangular" Row 5: "Conjugate z₁"
Create result display
ResultLabel for showing the answer. FormatLabel for showing both rectangular and polar forms side by side.
🧩Part B — Blocks View (Logic & Calculation)
Switch to Blocks view. Now add the logic that makes your app actually work.
Read complex number inputs
Create procedure "readInputs": set a = number(RealA.Text) set b = number(ImagB.Text) set c = number(RealC.Text) set d = number(ImagD.Text)
Build Addition
When AddButton.Click: realResult = a + c imagResult = b + d Display formatted: formatComplex(realResult, imagResult) Create a "formatComplex" procedure: if imagResult >= 0: return realResult + " + " + imagResult + "i" else: return realResult + " - " + abs(imagResult) + "i"
Build Subtraction
When SubButton.Click: realResult = a - c imagResult = b - d Display using formatComplex.
Build Multiplication
When MulButton.Click: Use the formula: (a+bi)(c+di) = (ac-bd) + (ad+bc)i realResult = (a × c) - (b × d) imagResult = (a × d) + (b × c) Display result.
Build Division
When DivButton.Click: 1. Calculate denominator: denom = c² + d² 2. Check if denom = 0 → "Cannot divide by zero!" 3. realResult = (a×c + b×d) / denom 4. imagResult = (b×c - a×d) / denom 5. Display result.
Calculate Modulus
When ModulusButton.Click: mod = sqrt(a² + b²) Display: "|z₁| = " + mod
Calculate Argument & Polar conversion
When ArgButton.Click: theta = atan2(b, a) — use atan2 block from Math Convert radians to degrees: degrees = theta × 180 / π Display: "arg(z₁) = " + degrees + "°" When ToPolarButton.Click: r = sqrt(a² + b²) theta = atan2(b, a) × 180 / π Display: "z₁ = " + r + "∠" + theta + "°"
Calculate Conjugate
When ConjugateButton.Click: The conjugate of a + bi is a - bi (just negate the imaginary part) Display: formatComplex(a, -b)
🧪Testing Your App
- ✓(3+4i) + (1+2i) = 4+6i
- ✓(3+4i) × (1+2i) = (3-8) + (6+4)i = -5+10i
- ✓|3+4i| = 5 (classic 3-4-5)
- ✓(1+i) ÷ (1-i) = i (pure imaginary result)
- ✓Polar form of 1+i = √2 ∠ 45°
🚀Bonus Challenges
Extra credit — impress your instructor
- ★Plot complex numbers on an Argand diagram using Canvas
- ★Add powers: z^n using De Moivre's theorem
- ★Add nth roots of a complex number
- ★Show the multiplication geometrically (rotation and scaling)