The Math Library: 1 Basic Math Functions

Up: GEOS SDK TechDocs | Up | Down | Prev: The Math Library | Next: 2 Conversions to Other Types

The Math Library includes many routines and structures that make the manipulation of FP numbers possible. Most of these are transparent to a C programmer.

C includes the following floating point types: float , double , and long double . A floating point number in GEOS is of type FloatNum , which uses the IEEE 80 bit standard (equivalent to a long double) to represent floating point values. FloatNum has overloaded the float, double, long double, and all basic math functions.

A FloatNum consists of a one bit sign, a 15 bit exponent, and a 64 bit mantissa. The maximum exponent allowed is 7FFEh. An exponent of 7FFFh (FP_NAN) signals an underflow or overflow. You may check the value of this exponent using the macro FLOAT_EXPONENT to extract the exponent of a FloatNum .

Code Display D-1 Extracting an Exponent

FloatNum myNum;
if (FLOAT_EXPONENT(&myNum) == FP_NAN)
{
     return(ERROR);
}

If you use any floats, doubles, or long doubles in your application, GEOS will convert these types into FloatNum values automatically and call the appropriate GEOS functions to manipulate the numbers. As this is the case, it is usually easier to declare any floating point variables in your application as FloatNum types (or long doubles). This cuts down on conversion time.

The Math Library provides all the familiar mathematical functions to manipulate these GEOS FP numbers (addition, multiplication, computation of logarithms, etc.). In C, most of these functions will automatically be called when their corresponding C operation involving FP numbers take place. (For example, when using the `+' binary operator to add two FP numbers, the Math Library will use the corresponding FloatAdd() routine.)

You may, under special circumstances, wish to use these math routines directly. If you do so, you will need to manipulate the floating point stack manually, pushing numbers on the stack and making sure numbers are in the proper stack location to perform each operation. In most cases, the FP math routines operate on numbers already in place on the FP stack; they take and return no arguments of their own.

Two functions you will need to use if you take this approach are FloatPushNumber() and FloatPopNumber() . FloatPushNumber() takes the address of a variable (of type FloatNum ) to push onto the FP stack; FloatPopNumber() takes the address of a buffer to place an FP number popped off the FP stack. Other routines (for example, FloatAdd() ) can then be called to operate on the FP stack. (See Adding Two FP Numbers for an example using this approach.)

There are many other routines that perform stack manipulation by shifting locations of FP numbers on the stack ( FloatRoll() , FloatDrop() , etc.). These routines are covered in detail in the latter part of this chapter because they are seldom needed in a C applications.


Up: GEOS SDK TechDocs | Up | Down | Prev: The Math Library | Next: 2 Conversions to Other Types