Logo

Programming-Idioms

  • Perl
  • Pascal
  • Ruby

Idiom #77 Complex number

Declare a complex x and initialize it with value (3i - 2). Then multiply it by i.

The complex x is moved on the 2D plane when multiplied by i
x = 3i - 2
x *= 1i
use Math::Complex;
my $x = 3*i - 2;
$x *= i;

Simple version: clear, but not efficient. It starts with i, then uses a multiplication and a subtraction to build the complex number.
use Math::Complex;
my $x = cplx(-2, 3);
$x *= i;

This version is efficient (fast)
uses ucomplex;
var
  C: Complex;
begin
  C := 3*i - 2;
end.
with Ada.Numerics.Complex_Types;
declare
   use Ada.Numerics.Complex_Types;
   X : Complex := (Re => -2.0, Im => 3.0);
begin
   X := X * i;
end;

New implementation...
< >
deleplace