Concepts / Using the Complex Number Class

Using the Complex Number Class

Reading the converted form tells you which function runs and which values it receives. Whenever an operand is a complex number, translate the operator into its operator function call before you try to reason about the result.

  • Programming

Guessing What an Operator Really Does

When you write an expression such as c!=b or 1/a, it looks exactly like the expressions you would write for ordinary numbers. The symbols seem to be built-in instructions that the language already knows how to carry out. When one of the operands is a complex number, that impression stops being reliable. The compiler does not treat the symbol as a simple built-in instruction. It treats the operator as a request to call a function. This article shows how that translation works and why reading the translated form is the key to reasoning about complex number expressions.

What do you think happens?

What do you think happens when the compiler sees 1/a, where a is a complex value?

Reveal answer
Answer:

The compiler rewrites 1/a as operator/(complex{1},a), a call to a function named operator/ whose first argument is complex{1}.

The operator symbol becomes a function name, the operands become the arguments in their original order, and the plain integer 1 is written as a complex value before the call is made.

Tracing c!=b Into a Function Call

Take the source's first example, c!=b. When c or b is a complex number, the compiler does not read != as an ordinary comparison instruction. It reads it as a request for the operator!= function, and it passes the two operands to that function. The expression is therefore equivalent to operator!=(c,b). The name of the function is the operator with the word operator placed in front of it. The operands keep the same order they had in the original expression, so c is still the first argument and b is still the second.

Before
c != b

An operator symbol sits between the two operands.

After
operator!=(c, b)

A named function receives both operands as arguments, in the same order.

c != boperator expressioncleft operandoperator!=(c, b)function callcfirst argumentbright operandbsecond argumentrewritten assame positionsame position
What does the compiler replace the operator expression with, and where does each operand go in the function call?

The Rule for Converting Operators

The rule has three parts. First, the operator symbol becomes the name of a function by placing the word operator in front of it, so != becomes operator!= and / becomes operator/. Second, the operands become the arguments of that function, enclosed in parentheses and separated by commas. Third, the operands keep their original order. The converted form is an ordinary function call, so once you can read it, you can see exactly which values the operation receives and in what sequence. This matters because some operators depend on which operand is on the left and which is on the right. Division is the clearest case.

Key point

Reading the converted form tells you which function runs and which values it receives. Whenever an operand is a complex number, translate the operator into its operator function call before you try to reason about the result.

1left operand, becomescomplex{1}operator/function name/operator symbol, becomesoperator/complex{1}first argumentaright operand, kept as isasecond argumentnameconvertedpassed on
Which parts of the original expression map to which parts of the operator function call, and what is the role of each part?

Where the Plain Integer 1 Goes

The second source example, 1/a, shows a step that is easy to miss. The rule gives operator/(complex{1},a). The function is operator/, and its first argument is complex{1}, not the bare 1. The left operand was itself converted into a complex value before the call was made. So the plain integer 1 does not reach the division function as a plain integer. It arrives as a complex object, and that object is what the division function receives first. The right operand a is passed through as the second argument.

1plain integer on the leftcomplex{1}converted complex valueoperator/division functionresultreturned by the callacomplex operand on therightconvertedfirst argumentsecond argumentcall returns
Why does the literal 1 turn into a complex object, and how does that object flow into the division function as its first argument?

Converting a Comparison Expression

The same pattern applies to comparison operators, not just arithmetic ones. Consider a generated example with two complex variables named x and y, written as x!=y. The compiler reads the != symbol as a request for the operator!= function and passes the operands in their original order. The expression therefore corresponds to operator!=(x,y). The left operand x remains the first argument, so the order of the variables in the original expression determines the order of the call.

Worked example

Rewriting x!=y as a function call

Two complex variables x and y appear in the expression x!=y. Rewrite the expression in its operator function form.

  1. 1
    Identify the operator symbol

    The symbol in the expression is !=, which the compiler treats as a request for a function call because an operand is complex.

  2. 2
    Name the function

    Place the word operator in front of the symbol to get operator!=.

  3. 3
    Collect the operands in order

    The left operand is x and the right operand is y, so they are passed in that order.

  4. 4
    Write the call

    Combine the name and the arguments in parentheses with a comma between them to get operator!=(x,y).

Result

x!=y corresponds to operator!=(x,y). Swapping the variables to y!=x would instead correspond to operator!=(y,x).

Mistakes Learners Make When Reading Operators

  • Common mistake

    Reading the operator as an ordinary built-in instruction

    What it looks like

    Seeing c!=b and assuming it is a simple comparison performed directly on the symbol.

    Why it fails

    When an operand is a complex number, the compiler treats the operator as a request for a function call.

    Do this instead

    Translate the expression into its operator function form, such as operator!=(c,b), before reasoning about it.

  • Common mistake

    Assuming operand order does not matter

    What it looks like

    Treating 1/a and a/1 as the same expression with the same arguments.

    Why it fails

    The operands are passed in their original order, and division depends on which value is first.

    Do this instead

    Write out the argument list for each expression separately and check which value is in the first position.

  • Common mistake

    Assuming the plain integer stays an integer in the call

    What it looks like

    Reading 1/a as operator/(1,a) with a plain integer as the first argument.

    Why it fails

    The source gives the converted form as operator/(complex{1},a), so the left operand is a complex value.

    Do this instead

    Write the literal 1 as complex{1} when you rewrite the expression.

  • Common mistake

    Forgetting the word operator in the function name when rewriting the expression

1/aoriginal expressionoperator/(complex{1},a)first argument is theliteral 1a/1original expressionoperator/(a, ...)first argument is aconvertedconverted
How do the two expressions 1/a and a/1 differ once they are converted, and why does swapping operands change the result?

Practice Translating Operator Expressions

EASY

Given two complex variables p and q, rewrite the expression p!=q in its operator function form. Then rewrite the expression q!=p and explain how the two calls differ.

Hints
  • Start by replacing the symbol != with the function name operator!=.
  • Keep the operands in the order they appear in the original expression.
  • Check whether p or q is the first argument in each call.
MEDIUM

An expression divides a plain integer 1 by a complex value a, and a second expression divides a complex value a by the plain integer 1. Write the operator function form of each, showing where complex{1} appears, and explain why the results depend on operand order.

Hints
  • The source gives operator/(complex{1},a) for 1/a.
  • Operands keep their original order in the call.
  • Think about which argument the division function receives first in each case.

Key Takeaways

  • When an operand is a complex number, the compiler treats an operator symbol as a request for a function call rather than a simple built-in instruction.
  • An operator such as != becomes a function named by placing the word operator in front of it, so c!=b means operator!=(c,b).
  • Operands keep their original order in the call, which matters for operators such as division.
  • The plain integer 1 appears as complex{1} in the converted call, as in operator/(complex{1},a) for 1/a.
  • Rewriting an expression in its converted form shows exactly which function runs and which values it receives.

Related concepts

More concepts starting with U