Appligent Labs

Fixed Point Math

Written by Mark Gavin | Dec 22, 2009 5:00:00 AM

by Mark Gavin

In the past year I have taught an Acrobat SDK training course a few times.  During these courses; I noticed many programmers; especially younger programmers, have not been exposed to Fixed Point Mathematics.  When working with the Acrobat SDK; Fixed Point Math is very important; because, many of the SDK calls expect the caller to be passing numerical information using Fixed Point data types like ASFixed, ASFixedPoint, ASFixedRect, ASFixedQuad and ASFixedMatrix.

The Acrobat SDK uses the ASFixed data type to represent rational numbers.  An ASFixed can not be directly substituted for any of the standard C numerical data types; for example, integer, float or double.

An ASFixed is a 32 bit data type where the first 16 bits represents the significand portion of the number; and, the second 16 bits represents the fractional portion of the number. Note: this is true on big-endian systems and reversed on little-endian systems.

Working with Fixed Point Numbers

Addition, Subtraction and negation can be done normally on fixed point numbers.  However, multiplication and division must be performed using the Acrobat SDK calls; ASFixedMul and ASFixedDiv if your want to get the correct results.

Following is a list of some of the fixed point constants included in the Acrobat SDK as found in ASExpT.h.  The numbers are in hexadecimal.

0x 0000 0000 fixedZero

0x 0000 2000 fixedEighth

0x 0000 4000 fixedQuarter

0x 0000 8000 fixedHalf

0x 0000 AAAA fixedTwoThirds

0x 0001 0000 fixedOne

0x 0002 0000 fixedTwo

0x 0003 0000 fixedThree

0x 0010 0000 fixedSixteen

0x 0020 0000 fixedThirtyTwo

If at all possible; use the fixed point constants.

Correct use of fixedTwo constant

theWidthASFixed =  ASFixedDiv( theLengthASFixed, fixedThree ) ;

InCorrect use of integer 3

theWidthASFixed =  ASFixedDiv( theLengthASFixed, 3 ) ;

Conversions

The Acrobat SDK contains several functions to convert between ASFixed and other data types.  For example:

ASFixedToFloat, FloatToASFixed, ASFixedRoundToInt32, ASInt32ToFixed, etc.  In addition, there are to and from C strings; ASCStringToFixed and ASFixedToCString.

Conversions when using constants are not necessary.  If a fixed point constant does not exist in ASExpT.h; constant values can be added together.  For example; fixedFourteen does not exist; but, you can easily do the following: fixedTwelve + fixedTwo

Matrix Manipulation

There are also several functions available for matrix manipulation.  They include; ASFixedMatrixConcat, ASFixedMatrixInvert, ASFixedMatrixTransform and ASFixedMatrixTransformRect.