Scratch does not have a built-in imaginary number type, so you create them using two variables or a list to store the real and imaginary parts separately

An imaginary number is a number multiplied by i, where i is defined as the square root of −1. In Scratch, you cannot type i directly into a block. Instead, you store the real part and the imaginary part as separate values — for example, the number 3 + 4i becomes a real part of 3 and an imaginary part of 4. When you need to perform math with imaginary numbers, you write custom blocks that do the arithmetic on both parts at once.

The most straightforward approach is to use two variables for each imaginary number: one for the real component and one for the imaginary component. If you are working with many imaginary numbers at once, you can also use lists to store pairs of values. Either way, the logic is the same — you perform operations on the real and imaginary parts according to the rules of complex arithmetic.

Key Takeaways

  • Create an imaginary number by storing its real part in one variable and its imaginary part in another, since Scratch has no native imaginary type.
  • Addition and subtraction of imaginary numbers work by adding or subtracting the real parts together and the imaginary parts together.
  • Multiplication of imaginary numbers requires the formula (a + bi)(c + di) = (ac − bd) + (ad + bc)i, which you can build into a custom block.
  • You can use lists to store multiple imaginary numbers as pairs of values, making it easier to work with collections of complex numbers.

Setting up variables for a single imaginary number

Start by creating two variables in Scratch. Click the Variables category in the blocks palette, then click "Make a Variable". Name the first one real part and the second one imaginary part. These will hold the two components of your imaginary number.

To set the imaginary number 3 + 4i, use two "set" blocks: set real part to 3, and set imaginary part to 4. If you want to display the number on the screen, you can use a "say" block that combines text with both variables, like "say (join (real part) (join " + " (join (imaginary part) "i")))". This shows the number in the format 3 + 4i.

Adding and subtracting imaginary numbers

To add two imaginary numbers, add their real parts together and their imaginary parts together. If you have the first number stored in real part 1 and imaginary part 1, and the second in real part 2 and imaginary part 2, create a custom block called "add imaginary numbers". Inside the block, set a result variable for the real part to (real part 1) + (real part 2), and set the imaginary result to (imaginary part 1) + (imaginary part 2).

Subtraction works the same way, except you subtract instead of add. Create a custom block called "subtract imaginary numbers" and set the real result to (real part 1) − (real part 2), and the imaginary result to (imaginary part 1) − (imaginary part 2). Both operations are straightforward because the real and imaginary parts do not mix during addition or subtraction.

Multiplying imaginary numbers

Multiplication is more complex because the imaginary unit i has a special property: i × i = −1. When you multiply (a + bi) × (c + di), you get (ac − bd) + (ad + bc)i. The real part of the result is (ac − bd), and the imaginary part is (ad + bc).

Create a custom block called "multiply imaginary numbers" with four inputs: a, b, c, and d. Inside, set a variable result real to (a × c) − (b × d), and set result imaginary to (a × d) + (b × c). When you call this block with your two imaginary numbers, it returns the correct product. For example, multiplying (2 + 3i) × (1 + 2i) gives (−4) + 7i.

Using lists to store multiple imaginary numbers

If you need to work with many imaginary numbers, storing each one in separate variables becomes unwieldy. Instead, create a list called imaginary numbers. Each imaginary number takes up two slots in the list: the first slot holds the real part, and the second holds the imaginary part.

To add the imaginary number 3 + 4i to the list, use "add (3) to (imaginary numbers)" and then "add (4) to (imaginary numbers)". To retrieve the first imaginary number, read item 1 (the real part) and item 2 (the imaginary part). You can loop through the list in steps of 2 to process all the numbers. This approach scales better when you have 10 or 100 imaginary numbers instead of just two.

Building a custom block for division

Division of imaginary numbers uses the formula (a + bi) ÷ (c + di) = ((ac + bd) + (bc − ad)i) ÷ (c² + d²). The denominator c² + d² is called the modulus squared, and you divide both the real and imaginary parts by it.

Create a custom block called "divide imaginary numbers" with four inputs: a, b, c, and d. First, calculate the denominator as (c × c) + (d × d). Then set result real to ((a × c) + (b × d)) ÷ denominator, and set result imaginary to ((b × c) − (a × d)) ÷ denominator. This block handles the full division operation in one call.

Displaying imaginary numbers in different formats

Scratch can show imaginary numbers in several ways depending on what your project needs. The standard format is 3 + 4i, but you might also want to show it as a coordinate pair (3, 4) or in polar form with a magnitude and angle.

To show the standard format, join the real part, the string " + ", and the imaginary part with "i" at the end. If the imaginary part is negative, you may want to handle the sign separately so it displays as 3 − 4i instead of 3 + −4i. For coordinate format, use parentheses and a comma. For polar form, calculate the magnitude as the square root of (real² + imaginary²) and the angle using the arctangent of (imaginary ÷ real).

Frequently Asked Questions

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

No. Scratch treats all numbers as real numbers. You must manually split imaginary numbers into real and imaginary parts and store them in separate variables or list slots. This is a limitation of Scratch's design, not a bug.

What happens if I multiply i by i in my custom block?

When you multiply (0 + 1i) × (0 + 1i) using the formula (ac − bd) + (ad + bc)i, you get (0 − 1) + (0 + 0)i = −1 + 0i, which is correct. The formula automatically handles the fact that i² = −1.

How do I find the absolute value of an imaginary number?

The absolute value (or magnitude) of a + bi is the square root of (a² + b²). In Scratch, use a "square root" block with (a × a) + (b × b) as the input. For example, the magnitude of 3 + 4i is the square root of (9 + 16) = 5.

Can I use imaginary numbers in a game or animation?

Yes. Imaginary numbers are useful for rotating objects, creating wave patterns, or simulating physics. Store the position or velocity as an imaginary number, perform the math you need, and then extract the real and imaginary parts to set the sprite's x and y coordinates.