## Absolute valueEdit

The absolute value function returns the "size" of a number. The size of a nonnegative number X is always X itself, but if the number is negative it is -X. On implementation need the function would in C and C++ look like:

double dabs (double X) { if (X >= 0) return X; return -X; }

This may pose problems if the argument is int, in which case another specialized function iabs for integers and fabs for single precision float are desired. C, C++ and most other strongly typed programming languages all have this problem, but in C++ and other generic/template providing PL:s using templates is an alternative, such as in C++:

template <class NUM> NUM abs (NUM X) { if (X >= 0) return X; return -X; }

which if to be used somewhere either can be explicitly accessed like:

float K; K = get_my_float_val (something); K = abs<float>(K);

or is implicitly deduced by compiler so that

double D, I, X; I = abs(1); // accesses abs<int> X = abs(1.1); // accesses abs<float> D = abs(D); // accesses abs<double based>

## Minimum functionEdit

C function:

double dmin (double X, double Y) { if (X >= Y) return Y; return X; }

In template/generic languages same technique as for absolute value can be used.

C macro can also be used for typelessness when no template system is available:

#define MIN(X,Y) ((X)>(Y)?(Y):(X))

As usual with C macros beware of side effects in provided arguments, the classical case being an incremental operation:

j = MIN(i++,k);

Which expands to:

j = ((i++)>(k)?(k):(i++));

If `i`

is larger than `k`

, then `i++`

is called twice, not once as would be the naive interpretation of the line:

j = MIN(i++,k);

## Maximum functionEdit

C:

double dmax (double X, double Y) { if (X >= Y) return X; return Y; }

In template/generic languages same technique as for absolute value can be used.

C macro:

#define MIN(X,Y) ((X)>(Y)?(X):(Y))