typestar

Complex numbers in Fortran

complex is a built-in numeric type: rotate, measure, and conjugate without a library.

program complex_numbers
    implicit none
    ! complex is a built-in numeric type, not a library import.
    complex :: z = (3.0, 4.0)
    complex :: w

    w = z * (0.0, 1.0)
    print *, "z:         ", z
    print *, "rotated:   ", w
    print *, "magnitude: ", abs(z)
    print *, "parts:     ", real(z), aimag(z)
    print *, "conjugate: ", conjg(z)
end program complex_numbers

How it works

  1. complex :: z = (3.0, 4.0) is a literal, not a constructor call.
  2. Multiplying by (0.0, 1.0) rotates z a quarter turn.
  3. abs, real, aimag, and conjg are ordinary intrinsics on it.

Keywords and builtins used here

The run, in numbers

Lines
13
Characters to type
342
Tokens
73
Three-star pace
65 tpm

At the three-star pace of 65 tokens a minute, this run takes about 67 seconds.

Type this snippet

Step 3 of 3 in Types & memory, step 21 of 27 in Language basics.

← Previous Next →