Scratch lets you store and do math with imaginary numbers using lists and variables

Scratch does not have a built-in imaginary number type, but you can represent them using two separate values: one for the real part and one for the imaginary part. An imaginary number like 3 + 4i gets stored as two pieces of data — the real part (3) and the imaginary coefficient (4). Once you set up this structure, you can add, subtract, multiply, and divide imaginary numbers using the same math blocks you already use for regular numbers.

The simplest way is to use two variables for each imaginary number. If you are working with one imaginary number, create a variable called real_part and another called imaginary_part. If you need to work with multiple imaginary numbers at once, use lists instead — one list to hold all the real parts and another to hold all the imaginary parts, with matching index positions.

Key Takeaways

  • Represent an imaginary number as two separate values: the real part and the imaginary coefficient, stored in variables or lists.
  • Adding two imaginary numbers means adding their real parts together and their imaginary parts together separately.
  • Multiplying imaginary numbers requires the formula (a + bi)(c + di) = (ac − bd) + (ad + bc)i, which you build using Scratch's math blocks.
  • Use custom blocks to avoid repeating the same imaginary number operations over and over in your project.
  • Test your blocks by working through examples on paper first, then checking that Scratch produces the same answer.

Setting up variables to hold the real and imaginary parts

Start by creating two variables in the Variables category. Click "Make a Variable" and name the first one real_part. Click "Make a Variable" again and name the second one imaginary_part. These will hold the two pieces of a single imaginary number.

To represent the number 3 + 4i, you would set real_part to 3 and imaginary_part to 4. The imaginary unit i is understood — you do not store it as a separate value. If you need to work with negative imaginary parts, like 2 − 5i, you set real_part to 2 and imaginary_part to −5.

If your project needs to track many imaginary numbers at once, use lists instead. Create one list called real_parts and another called imaginary_parts. Store the real part of the first number at index 1 of real_parts and its imaginary part at index 1 of imaginary_parts. The second number goes at index 2 of each list, and so on. This keeps all your data organized and makes it easier to loop through multiple numbers.

Adding and subtracting imaginary numbers

Addition is the simplest operation. To add two imaginary numbers, add their real parts together and add their imaginary parts together separately. If you are adding (3 + 4i) + (2 + 5i), the result is (3 + 2) + (4 + 5)i = 5 + 9i.

In Scratch, create a custom block called add imaginary that takes four inputs: real1, imag1, real2, and imag2. Inside the block, set real_part to (real1 + real2) and set imaginary_part to (imag1 + imag2). When you call this block with the numbers you want to add, the result will be stored in your two variables.

Subtraction works the same way. Create a custom block called subtract imaginary with the same four inputs. Set real_part to (real1 − real2) and set imaginary_part to (imag1 − imag2). To subtract (2 + 5i) from (3 + 4i), you get (3 − 2) + (4 − 5)i = 1 − i, so real_part becomes 1 and imaginary_part becomes −1.

Multiplying imaginary numbers using the FOIL formula

Multiplication is more involved because you have to use the distributive property. The formula is (a + bi)(c + di) = (ac − bd) + (ad + bc)i. The key is that i × i = −1, so when you multiply the imaginary parts together, you get a negative real number.

Create a custom block called multiply imaginary with four inputs: real1, imag1, real2, and imag2. Inside the block, set real_part to ((real1 × real2) − (imag1 × imag2)). Set imaginary_part to ((real1 × imag2) + (imag1 × real2)). When you call this block, Scratch will calculate the product and store it in your two variables.

Test this with a straightforward example: (2 + 3i) × (1 + 2i). By hand, you get (2 × 1 − 3 × 2) + (2 × 2 + 3 × 1)i = (2 − 6) + (4 + 3)i = −4 + 7i. In your block, real_part should be −4 and imaginary_part should be 7. Run it in Scratch and verify the answer matches.

Dividing imaginary numbers using the conjugate

Division requires multiplying both the numerator and denominator by the conjugate of the denominator. The conjugate of (c + di) is (c − di). The formula is (a + bi) ÷ (c + di) = ((a + bi)(c − di)) ÷ ((c + di)(c − di)). The denominator becomes c² + d², a real number.

Create a custom block called divide imaginary with four inputs: real1, imag1, real2, and imag2. First, calculate the denominator: set a variable denom to (real2² + imag2²). Then calculate the numerator using the multiplication formula with the conjugate: the real part is ((real1 × real2) + (imag1 × imag2)) and the imaginary part is ((imag1 × real2) − (real1 × imag2)). Finally, set real_part to (numerator_real ÷ denom) and imaginary_part to (numerator_imag ÷ denom).

Example: (4 + 2i) ÷ (1 + i). The denominator is 1² + 1² = 2. The numerator is (4 + 2i)(1 − i) = (4 − (−2)) + (−4 + 2)i = 6 − 2i. So the result is (6 − 2i) ÷ 2 = 3 − i. In your block, real_part should be 3 and imaginary_part should be −1.

Using custom blocks to avoid repeating code

Once you have built blocks for addition, subtraction, multiplication, and division, you can reuse them throughout your project without writing the math again. This saves time and makes your code easier to read.

If you find yourself doing the same sequence of operations repeatedly — for example, adding two numbers and then multiplying the result by a third — create a new custom block that calls your existing blocks in order. Name it something descriptive like add then multiply and pass in all the numbers you need. Inside, call your add imaginary block first, then call your multiply imaginary block using the result.

This approach also makes debugging easier. If your answer is wrong, you know the error is in one of your four basic operation blocks, not scattered across your whole project. Test each block in isolation before combining them.

Displaying imaginary numbers on the stage

To show your result to the user, use a say block that combines the real and imaginary parts into a readable string. Use the join block to concatenate text and variables. A straightforward format is to say something like "3 + 4i" or "2 − 5i".

Create a custom block called display imaginary that takes real_part and imaginary_part as inputs. Inside, use a say block with a join that puts together the real part, a plus or minus sign (depending on whether the imaginary part is positive or negative), the absolute value of the imaginary part, and the letter i.

If you want to handle the sign automatically, use an if block: if imaginary_part is greater than or equal to 0, join the real part, " + ", the imaginary part, and "i". Otherwise, join the real part, " − ", the absolute value of the imaginary part, and "i". This way, −5 displays as "− 5i" instead of "+ −5i".

Frequently Asked Questions

What is the imaginary unit i?

The imaginary unit i is defined as the square root of −1. In math, i² = −1. Imaginary numbers are written as a real number plus a multiple of i, like 3 + 4i. Scratch does not have i built in, so you store the coefficient (4) separately and remember that it is multiplied by i.

Can I use Scratch's built-in number type for imaginary numbers?

No. Scratch's numbers are real numbers only. You have to create your own representation using two variables or two lists. This is a common approach in many programming languages that do not have imaginary numbers built in.

Why does multiplying imaginary numbers use that formula?

When you multiply (a + bi)(c + di), you distribute each term: ac + adi + bci + bdi². Since i² = −1, the bdi² term becomes −bd. Collecting real and imaginary parts gives (ac − bd) + (ad + bc)i. This is why the formula works.

What happens if the imaginary part is zero?

If imaginary_part is 0, your number is just a real number. All the blocks still work — you are just doing regular arithmetic. For example, (3 + 0i) + (2 + 0i) = 5 + 0i, which is the same as 3 + 2 = 5.

How do I check if my multiplication block is correct?

Work through an example by hand first, then run it in Scratch and compare. Try (1 + i) × (1 + i), which should give 1 + 2i − 1 = 2i (real part 0, imaginary part 2). Or try (2 + 3i) × (1 − i), which should give 2 − 2i + 3i − 3i² = 2 + i + 3 = 5 + i. If your block matches these answers, it is working.