/* * Complex number in C. * * Example to illustrate how to implement type-safe arithmetic in C. * * Godmar Back * CS 3204 Fall 2006 */ #include typedef struct { double re; double im; } complex_t; static inline complex_t complex_add(complex_t x, complex_t y) { return (complex_t){ x.re + y.re, x.im + y.im }; } static inline double complex_real(complex_t x) { return x.re; } static inline double complex_imaginary(complex_t x) { return x.im; } static inline double complex_abs(complex_t x) { return sqrt(x.re * x.re + x.im * x.im); } // vim: sw=2