next up previous
Next: Recommended Reading Up: Math Functions & Macros Previous: Common Math Functions (defined

Math Macros

C does not provide an exponeniation operator. Nor are there functions for simple operations such as taking the absolute value of a number of arbitrary type, determining the sign of a number, etc. The following are simple macros for these types of operations. They should be used with care however because they are inefficient (complex arguments may be evaluated more than once) and can have unexpected side effects.


#define SQ(x) ((x)*(x)) /* square of x */
#define ABS(x) ((x) < 0 ? -(x) : (x)) /* absolute value of x */
#define SGN(x) ((x) < 0 ? -1 : (x) > 0 ? 1 : 0) /* sign of x */
#define MIN(x,y) ((x) < (y) ? (x) : (y)) /* minimum of x and y */
#define MIN(x,y) ((x) > (y) ? (x) : (y)) /* maximum of x and y */


Note also that there is no built-in C support for complex numbers. The best strategy in this case is to define a structure that consists of the real and imaginary parts of the number, then write simple functions to perform basic math on these structures.


next up previous
Next: Recommended Reading Up: Math Functions & Macros Previous: Common Math Functions (defined
Derek C. Richardson 2005-02-03