Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

Complex Arithmetic: In the Intersection of C and C++


Listing 2: Test program

Listing 2: Test program

// Test program for math-complex.h.  This file can be
// compiled by either C99 or C++.

#include <stdio.h>
#include "math-complex.h"

static const long double pi =
  3.141592653589793238462643383279502884197L;

int main()
{
    // The three complex types store two components of the
    // underlying floating point type.
    printf("sizeof(float)=%2d        sizeof(float_complex)=%2d\n",
      (int) sizeof(float), (int) sizeof(float_complex));
    printf("sizeof(double)=%2d       sizeof(double_complex)=%2d\n",
      (int) sizeof(double), (int) sizeof(double_complex));
    printf("sizeof(long double)=%2d  sizeof(long_double_complex)=%2d\n",
      (int) sizeof(long double), (int) sizeof(long_double_complex));

    double_complex x, y, z;

    x = double_complex(1,2);
    y = double_complex(2,2);

    // You may mix complex and the corresponding real type
    z = x + y + 10.0;
    printf("(1+2i)+(2+2i)+10 = %g+%gi\n", real(z), imag(z));

    // But different complex types must be cast to a
    // common type or C++ rejects the program.
    long_double_complex w = long_double_complex(2,3);
    x = (double_complex) w * double_complex(5, 6);
    printf("(2+3i) * (5+6i) = %g+%gi\n", real(x), imag(x));
    x /= double_complex(5, 6);
    printf("(-8+27i) / (5+6i) = %g+%gi\n", real(x), imag(x));

    // e raised to the imaginary pi power is -1
    w = exp(long_double_complex(0., pi));
    printf("exp(pi*i) = %Lf+%Lfi\n", real(w), imag(w));

    // log of -1 is imaginary pi
    w = log(long_double_complex(-1, 0));
    printf("complex log(-1) = %Lf+%Lfi   (difference from pi=%Lg)\n",
      real(w), imag(w), imag(w) - pi);

    // absolute value of a complex number is a real number:
    // the square root of the sum of the squares of the real
    // and imaginary parts
    printf("abs(float_complex(3,4)) = %f\n", abs(float_complex(3,4)));

    // You may raise a real number to a complex power
    x = pow(exp(1.0), double_complex(0., pi));
    printf("e to the imaginary pi power = %f+%fi\n",
      real(x), imag(x));

    // Or a complex number to a real power
    x = pow(double_complex(0, 1), 2.0);
    printf("i squared = %f+%fi\n",
      real(x), imag(x));

    // Or a complex number to a complex power
    x = pow(double_complex(1, 1), double_complex(1, 1));
    printf("(1+1i) to the power (1+1i) = %f+%fi\n",
      real(x), imag(x));

    // Finally, square roots of negative numbers...
    x = sqrt(double_complex(-1.0, 0));
    printf("square root of -1 = %g+%gi\n",
      real(x), imag(x));

    // arg() returns the angle 
    long double ld = arg(long_double_complex(1., 1.));
    printf("arg(1+1i) = %Lf   (difference from pi/4=%Lg)\n", 
      ld, pi/4 - ld);

    // the conjugate of a complex number is the number with the
    // imaginary part's sign reversed.
    x = conj(double_complex(2., 3.));
    printf("conj(2+3i)) = %g+%gi\n",
      real(x), imag(x));

    // The complex trigonometric and hyperbolic functions
    x = double_complex(1,1);
    y = cos(x);
    printf("cos((1+1i)) = %f+%fi\n",
      real(y), imag(y));

    y = sin(x);
    printf("sin((1+1i)) = %f+%fi\n",
      real(y), imag(y));

    y = tan(x);
    printf("tan((1+1i)) = %f+%fi\n",
      real(y), imag(y));

    y = cosh(x);
    printf("cosh((1+1i)) = %f+%fi\n",
      real(y), imag(y));

    y = sinh(x);
    printf("sinh((1+1i)) = %f+%fi\n",
      real(y), imag(y));

    y = tanh(x);
    printf("tanh((1+1i)) = %f+%fi\n",
      real(y), imag(y));

    return 0;
}


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.