TweetFollow Us on Twitter

Fixed Math
Volume Number:9
Issue Number:10
Column Tag:C++ Workshop

Related Info: Binary-Dec. Pack.

Fast Fixed-point Math Made Easy

Using C++ to abstract data in an intuitive way

By Brion Sarachan, Albany, New York

Note: Source code files accompanying article are located on MacTech CD-ROM or source code disks.

With the recent release of Symantec C++, a high-quality, low-cost C++ development environment is now available to Macintosh programmers. If used properly, C++ can provide a quantum leap over the representation power of languages like C and Pascal. We often hear about the use of C++ to build object-oriented systems. Even more fundamentally, however, C++ is an excellent language for data abstraction. Using C++, new data types can be defined that can then be used as conveniently as the built-in types like integers, characters, and floating-point numbers.

As an example of C++’s ability to support data abstractions, this article will describe a simple C++ class for fixed-point numbers. Arithmetic operations on fixed-point numbers are much faster than the corresponding operations on floating-point numbers, at the expense of some loss in precision and a smaller range of allowable values. Many computer graphics and animation applications that require high-performance use fixed-point numbers.

Fixed-point numbers are not built into C++. The Mac Toolbox provides its own implementation of fixed-point numbers. However, using the Mac’s fixed-point numbers can be rather awkward for a couple of reasons:

• Arithmetic operators like * and / cannot be used. Instead, calls must be made to Toolbox routines such as FixMul() and FixDiv(). Therefore, code that uses fixed-point numbers must be written differently than other arithmetic expressions.

• ANSI library functions such as sqrt() or printf() cannot use fixed-point numbers directly. A fixed-point number must be converted to a double by using functions like Fix2X() before using the library functions. This is even more complicated because the manner in which fixed-point numbers are converted to doubles may differ depending on the type of floating-point representation being used by the processor or, if there is one, by the floating-point coprocessor.

C++ enables us to solve these problems by allowing us to define a new fixed-point number class. This new class uses the Mac Toolbox internally, but allows the programmer to declare fixed-point numbers and use them in expressions just as easily as built-in types like ints or doubles.

The Macintosh Fixed Data Type and its Operations

The Macintosh Fixed data type is described in Inside Macintosh. A Fixed type is actually a long integer:

typedef long Fixed;

A Fixed number therefore consists of 32 bits: a sign bit, a 15-bit integer part, and a 16-bit fractional part. The value of a Fixed number therefore falls in the range of about + and -32,768.

Fixed numbers can be added and subtracted using the usual + and - operators. However, the * and / operators will not work correctly for multiplication and division. Instead, the Toolbox provides the routines FixMul() and FixDiv(). The Toolbox provides several other routines for conversion to and from other data types and for some math functions. The following Toolbox routines are used in the implementation of the C++ fixed-point number class:

• FixMul() - multiplies two fixed-point numbers

• FixDiv() - divides two fixed-point numbers

• FracSin() - returns the sine of a fixed-point number as a Fract

• FracCos() - returns the cosine of a fixed-point number as a Fract

• Frac2Fix() - converts a Fract to a fixed-point number

• Long2Fix() - converts a long integer to a fixed-point number

• X2Fix() - converts an extended-format floating-point number to a fixed-point number

• Fix2X() - converts a fixed-point number to an extended-format floating-point number

Some of these routines use something called a Fract. A Fract is an alternative fixed-point type provided by the Toolbox. Like a Fixed number, a Fract also consists of 32 bits. However, for the case of a Fract, there is only one integer bit and a 30-bit fractional part.

Normally, one would use fixed-point numbers by declaring Fixed variables and calling the routines listed above directly. Using our new C++ class, we will be able to use floating-point numbers much more easily, while still getting the benefit of fast calculations.

C++ Features that Support Data Abstraction

C++ has several features that make it a good language for data abstraction. These include:

• function overloading

• operator overloading

• user-defined type conversions

These features will be briefly described here, and they are discussed in depth in many reference books on C++. (See the References section at the end of this article.)

Function overloading allows multiple functions having the same name to be defined. As long as each instance of the function has a different argument type list, the compiler is able to determine which instance is being called. Function overloading allows us, for example, to define a sin() function, separate from the sin() function found in the C library, that is specialized for our new fixed-point number class.

Operator overloading allows us to define the meaning of different types of operators as they are applied to user-defined classes. This will allow us, for example, to define the * and / operators for multiplication and division on our new fixed-point number class.

User-defined type conversions are a subtle but very powerful feature of C++. In C (or C++), we can write:

double x = 3;

The compiler automatically converts the integer value of 3 to the double-precision floating-point value of 3.0, which is represented internally in a completely different format. Automatic type conversions save us a lot of work, and eliminate a lot of errors. For example, a library function to calculate a square root may be defined for doubles:

double sqrt(double x);

However, we are generally free to call the sqrt() function with any type of number, such as an integer. The compiler automatically performs any necessary type conversions:

float y = sqrt(3);

The example above performs two automatic conversions: The int 3 is converted to a double before calling sqrt(), and then the result of sqrt() is converted to a float. The effect is the same as if we had written:

float y = (float) sqrt( (double) 3 );

But certainly the simpler form of this expression is easier to write, easier to read, and less subject to errors.

C++ allows us to define the valid set of type conversions that the compiler may apply to the new classes that we write. So, in other words, we can tell the compiler how to convert instances of our new fixed-point number class to doubles. We can then apply the sqrt() function to a fixed-point number as easily as any other type of number, and leave the details of the type conversion to the compiler!

Implementation of the C++ Fixed-Point Number Class

Let’s take a look at how our new fixed-point number class is implemented. In order to understand these examples, the reader should know something about C programming. If the reader also has some knowledge of C++, all the better!

Our new C++ class is called simply fix, and recall that the design goal is for fix to be as easily usable as built-in types like float or double. The declaration of fix is structured like this:

/* 1 */

class fix {
 // declarations of friend functions
 Fixed n;
 static fix result;
 // declarations of private member functions
 // declarations of public member functions

The fix class has one data member, n. This member is a number whose data type is the Macintosh Toolbox type Fixed, which was described above. The fix class also has a static data member, result, which is shared among all members of the fix class. It was found during development that using this static data member to hold the results of calculations gave better performance than the alternative of declaring local variables within member functions.

The power of the new class comes from its friend and member functions. Examples of these will be described in detail below.

Arithmetic operators

Friend functions are functions that, although external to the class, may access the class’s private data members, which in this case is the Fixed data member, n. One friend function overloads the + operator for addition. This friend function is declared as:

/* 2 */

friend inline fix operator+(const fix, const fix);

and implemented as:

/* 3 */

inline fix operator+(const fix a, const fix b)
 fix::result.n = a.n + b.n;
 return fix::result;

The inline specifier means that the compiler will substitute the code for the function in place, rather than actually generating a stack frame when the function is called. This speeds execution at the expense of program size.

This friend function allows the + operator to be used for the addition of two numbers of type fix. (The minus operator for subtraction is defined similarly.)

From what we have seen so far, there is not yet much benefit to the fix class; the Toolbox Fixed data type also supports the + and - operators. However, let’s take a look at another friend function:

/* 4 */

inline fix operator*(const fix a, const fix b)
 fix::result.n = FixMul(a.n,b.n);
 return fix::result;

Now we begin to see some benefit of C++ data abstraction! This function allows the familiar * operator to be used for multiplication rather than the Toolbox FixMul() function. This seems much more natural. (Division is similarly defined.) Also, friend functions of this form play an important role in user-defined data conversions. Either of the function arguments is subject to implicit, user-defined type conversions. This allows, for example, a fix number to multiply a double.

This function also demonstrates another benefit of C++ classes: that of encapsulation. The function provides a wrapper over fixed-point number multiplication, so that whenever fix numbers are multiplied, the rangecheck() function will be called automatically to ensure that the result is valid.

sin() and cos() functions

Friend functions are also defined for sin() and cos() of fix numbers. For example:

/* 5 */

fix sin(const fix a)
 fix result;
 Fract temp = FracSin(a.n);
 result.n = Frac2Fix(temp);
 return result;

The calls to the Toolbox functions FracSin() and Frac2Fix() are hidden within the implementation of the friendlier sin() function.

Why is this function defined as a friend function rather than a member function? If it were a member function, it would be called as:


However, as a friend function, it is instead called as:


This matches the normal usage of sin() in the standard ANSI library.

I/O functions

C++ provides the iostream library which may be used in place of the standard C stdio library. Many beginning C++ programmers wonder why an alternative I/O library is provided, and are reluctant to use it since they are already expert in using the stdio library. The benefit of the C++ iostream library is illustrated in the following example.

Suppose we want to print a fix number using stdio. Before doing so, we need to convert the fix to a printable type. (We will assume for now that we may cast a fix to a double. Soon we will take a look at how such user-defined conversions are specified.)

printf("here is the number:  %f \n", (double)my_fix);

Converting the fix to a double before printing is at odds with the notion that user-defined types (such as fix) are supposed to behave analogously to the built-in types (like double). The problem is that stdio is not extensible! We cannot add new data types to the stdio mechanism cleanly. New types must be converted to a built-in type before printing.

In contrast, here is how we print the same thing using the iostream library:

cout << "here is the number:  " << my_fix << endl;

Note that there is no explicit type conversion specified. We would write essentially the same line of code whether we wanted to print a fix, int, or double variable. As we see, the iostream library can be cleanly extended for new data types.

The following friend function implements the above capability by overloading the << operator:

/* 6 */

ostream& operator<<(ostream& os, const fix& f)
 os << (double) f;
 return os;

The explicit conversion from fix to double is now specified once and for all within the implementation of this method.

The assignment operator and copy constructor

The compiler automatically generates a default assignment operator and copy constructor for each user-defined class. Therefore, the following operations are provided automatically by the compiler:

/* 7 */

fix my_fix;
fix your_fix = my_fix;
fix her_fix(my_fix);

Here, your_fix is assigned to have the same value as my_fix, and her_fix is initialized as being a copy of (having the same value as) my_fix. The default assignment operator and copy constructor perform a memberwise copy of all the data members of the class. For the fix class, the value of the fixed-point number is copied.

Other member functions

A few other member functions are provided, including overloaded operators for increment (++) and decrement (--), as well as the +=, -=, *=, and /= operators. The implementation of these is fairly straightforward.

The last, and most complex, topic in the implementation of the fix class, is that of user-defined type conversions.

User Defined Type Conversions

In the last section, we had assumed that the compiler knew how to convert fix numbers to and from built-in types such as doubles:

double my_double = (fix) my_fix;  // explicit conversion
double another_double = my_fix;   // implicit conversion

These type conversions are specified using C++ member functions. In this section we will examine the details of how this is done.

Whenever an instance of a user-defined class is created, a special function called a constructor is called. A class may have multiple constructors having different argument lists. For example, the fix class has several constructors, one of which is:

fix(const long l) { n = Long2Fix(l); };

This constructor allows a fix to be created from a long integer. The implementation of this constructor calls the Toolbox function Long2Fix() in order to convert the integer to a fixed-point value. The constructor uses the long data type rather than int because the compiler by default casts literal constants to the largest applicable type, so any integer literal has an implicit type of long. Also, the compiler knows how to convert any of the build-in integer types, including int and short, to long.

This constructor allows as to declare fix variables such as:

fix x(36); // initializes the fixed-point number to 36

Less obvious, however, is that this constructor tells the compiler how to implicitly convert between integers and fixed-point numbers. Having defined this constructor, it is valid to write:

int my_int = 38;
fix my_fix = my_int; // implicit conversion

Of course, now that we have the ability to convert integers to fixed-point numbers, it would also be desirable to convert floating-point numbers to fixed-point numbers. The implementation of this constructor is similar:

fix(const long double d) { n = X2Fix(d); };

Now that we can convert numbers of different types (integer and floating-point) to numbers of our new fixed-point class, how do we define the inverse conversion? In this case, we overload the fix cast operator:

/* 8*/

fix::operator long double()
 long double result = Fix2X(n);
 return (result);

The implementation of this member function is quite analogous to the implementation of the constructor from floating-point numbers, except that the conversion is performed in the reverse direction.

The overloaded cast operator allows us to write the following for an explicit type conversion.

/* 9 */

double d;
fix    f(3.14);
d = (double) f;

Even more importantly, however, the overloaded cast operator tells the compiler how to convert a fix number to a double implicitly.

At this point, we may wonder whether it is valid to write the following:

/* 10 */

fix my_fix, your_fix;
my_fix   = some_value;
your_fix = another_value;
if (my_fix > your_fix)   // Can we compare two fix numbers?
 do something...

The answer is: yes! Because we have told the compiler how to implicitly convert fixed-point numbers to floating-point numbers, the compiler actually performs the comparison as:

if ( (long double) my_fix > (long double) your_fix )

The compiler implicitly converts the fixed-point numbers to floating-point numbers in order to use the comparison (>) operator.

Using the Fixed-Point Number Class

We’ve already looked at several examples involving the fix class. Let’s study a couple of others in terms of the individual steps -- some explicit and some implicit -- performed by the compiler.

First, let’s consider:

/* 11 */

const double pi = 3.14159;
fix a(0.75);
cout << "sine of 135 degrees is " << sin(a*pi) << endl;

This calculation is performed by executing these steps:

• The overloaded * operator for fixed-point numbers computes a*pi.

• The overloaded sin() function for fixed-point numbers computes the sine of a*pi.

• The overloaded << operator writes the sine value to the output stream.

In contrast, let’s consider this example:

cout << "tangent of 135 degrees is " << tan(a*pi) << endl;

In this case, the calculation consists of these steps:

• The overloaded * operator for fixed numbers computes a*pi.

• The overloaded cast operator implicitly converts a*pi (a fix) to a long double.

• The long double result above is converted implicitly to a double.

• The standard ANSI library tan() function receives the double result from the last step.

• The << operator writes the tangent value to the output stream.

In the first case above, there is a sin() function specialized for fixed-point numbers, so the entire calculation of sin(a*pi) is performed in the domain of fixed-point numbers. In contrast, we have not defined a tan() function for fixed-point numbers. Therefore, the calculation of tan(a*pi) requires an implicit type conversion so that the tangent is actually computed in the domain of floating-point numbers. To the programmer using the fix class, the difference is transparent; fixed-point numbers can be used just like any other numbers. The compiler takes care of the details.

A Benchmark to Measure Performance

In this section we will look at a small sample program that compares the performance of fixed-point and floating-point numbers. To get a fair test, we want to perform the same set of calculations using each type of number. To do this, we will use another C++ feature: templates.

A C++ matrix template

Templates allow functions or classes to be written once for multiple data types. For example, our benchmark program will use a simple template class for matrices. This template class definition defines a simple 4x4 matrix of any type of number:

/* 12 */

template <class Number>
class matrix {
 Number m[4][4];
 friend ostream& 
 operator<<(ostream&, const matrix<Number>&);
 inline Number& operator()(const int,const int);
 matrix<Number> operator*(const matrix<Number>);

This is not an exhaustive definition for a matrix class by any means. If we were building a matrix class as part of a general-purpose class library, there are many other matrix functions that we would probably want to provide. However, this definition will suffice for the purpose of benchmarking the fix class.

As we can see, the definition of the matrix class includes:

• a 4x4 matrix of numbers

• an overloaded << operator for writing the matrix to an output stream

• a constructor for initializing a matrix

• an overloaded () operator that is used for indexing the elements of the matrix

• an overloaded * operator for multiplying matrices

Let’s take a look at the implementation of the * (matrix multiplication) operator:

/* 13 */

template <class Number>
matrix<Number>::operator*(const matrix<Number> mat)
 matrix<Number> result;
 int i,j,k;
       result[i][j] += m[i][k] * mat.m[k][j];
 return result;

This template function implements the traditional algorithm for multiplying two 4x4 matrices. Note that the multiplication of pairs of numbers is performed using the * operator. Of course this is the usual way of multiplying built-in number types like ints and doubles. Because we also defined the fix class to use the * operator for multiplication, this function can multiply matrices of fix numbers, as well.

Matrices for different types of numbers can now be declared as:

/* 14 */

matrix<double> double_matrix;
matrix<fix>    fix_matrix;

The benchmark() function

The benchmark() function is itself defined as a template, so that it can be applied to a matrix of any type of numbers. The benchmark() function:

• sets the input matrix to be an identity matrix,

• creates a three dimensional rotation matrix corresponding to a 2 degree rotation about the Z axis,

• premultiplies the input matrix 1000 times by the rotation matrix, and counts the number of seconds that this takes, and finally

• prints the resulting matrix (resulting from the 1000 multiplications).

Matrix operations like those listed above, and their use for three-dimensional computer graphics, are described in many standard textbooks, such as those listed in the References section.

It’s interesting to look at the line of code within the benchmark() function that performs the matrix multiplication:

m = rotation * m;

Because we overloaded the * operator for the matrix class to perform matrix multiplication, this line of code can be written in an extremely simple fashion! At this level of detail we do not have to be concerned with the fact that multiplication of two 4x4 matrices actually involves 64 multiplications of numbers, 48 additions, and so forth. The matrix C++ class is a high-level abstract data type that hides the details of the low-level matrix operations.

The main() routine calls the benchmark() function for both a matrix of fix numbers and a matrix of doubles. The results are summarized in the next section.

Benchmark Results

After the initial identity matrix:

10 0 0
01 0 0
00 1 0
00 0 1

was premultiplied 1000 times by the rotation matrix:

cos(Π/90) -sin(Π/90) 0 0

sin(Π/90) cos(Π/90) 0 0

0 0 1 0

0 0 0 1

the result, using fix numbers, was the matrix:

-0.93808 0.338715 0 0

-0.338715 -0.93808 0 0

0 0 1 0

0 0 0 1

And using doubles, the result was:

-0.939703 0.341992 0 0

-0.341992 -0.939703 0 0

0 0 1 0

0 0 0 1

It can be seen that the difference in precision between fix numbers and doubles in this case is less than 1% for 1000 calculations. This difference is certainly tolerable for many types of graphics applications where the numbers correspond to screen positions or orientations of geometric objects. For these cases, an infinitesimal difference in some quantity will most likely not even be noticeable.

The benchmark was run on a Powerbook 145 running at 16 MHz. The 1000 matrix multiplications took about 4 seconds for fix numbers and about 13 seconds for doubles. Therefore, the fix numbers executed about 3 times faster!

We might wonder whether a significant portion of the time for the fix number calculations was due to overhead introduced by the fix number class, as compared to the time required by the fixed-point math routines themselves. To find out, the profiler was used to take statistics during the 1000 matrix multiplications for fix numbers. The results were:

Function Min Max Avg % Entries

matrix<fix>::operator * 0 3 0 28 1000

operator * 0 1 0 33 64000

fix::operator += 0 1 0 16 64000

fix::rangecheck 0 1 0 8 64000

operator + 0 1 0 11 64000

matrix<fix>::__PT6matrix3fix 0 1 0 0 1000

fix::fix 0 1 0 0 1000

fix::fix 0 1 0 2 16000

As we see, 33% of the execution time was due to fixed-point number multiplication (performed in the operator *). 28% was due to the matrix multiplication routine, consisting of the nested for loops and assignment statements. Only 11% of the execution time was due to the addition of fixed-point numbers, although a surprising 16% of time was due to the += operator. We also note that 8% was due to the rangecheck() function. We could of course abstain from calling this function at the expense of some safety. It certainly seems as though the bulk of the execution time was due to the mathematical operations themselves, rather than overhead introduced by the class definition.


We have looked in detail at the design of a new C++ class that allows fixed-point numbers to be used as easily as floating-point numbers, with about a 3 times improvement in performance. The loss in precision was found to be less than 1%, even after a large number of calculations. The values of the fix numbers span the range of about + and - 32,768. The fix class should only be used for applications where values will remain within this range.

Modular design and consistent interfaces are extremely important for well-designed C++ classes. The fix class, although simple, was designed using an extremely powerful idea. By providing the same standard interface as the built-in number types, fix numbers can be used interchangeably with floats or doubles.

We could, for example, develop an application using doubles, and then easily “plug in” fix numbers for increased performance, simply by changing the types of variables. Conversely, if we develop an application using fix numbers, and then find that we need to use doubles after all, we could just “plug in” doubles.

The implementation of the fix class could be easily extended in a modular way. For example, if we want, in the future, to provide a fast square root function optimized for fixed-point numbers, we could easily “plug in” such a function. In the interim, fixed-point square roots are easily calculated by the compiler by implicitly converting fix numbers to doubles, and then calling the standard C sqrt() function. A programmer can use the fix class effectively without being concerned with these details.

Looking to the Future

There are many other examples where the power of C++ could greatly simplify Macintosh programming. Large, commercial class libraries such as the THINK Class Library (TCL), MacApp, and, soon, Bedrock, provide us with general-purpose application frameworks. However, we can still improve the design of our own specialized applications by developing appropriate, application-specific class definitions that work along with the larger application frameworks.

For example, suppose we wanted to create a class library to support a variety of three-dimensional graphics and animation applications. Using the modest foundation developed in this article, we could:

• extend the simple, initial matrix class introduced here to include other matrix functions,

• derive specialized types of matrices for geometric transformations, perspective projections, and so forth,

• design additional classes for such things as points, lines, and planes, using the fast fix class for calculations, and even

• derive a new type of TCL pane class that uses transformations, projections, and so forth, to support three-dimensional geometry.

The only limit is our imagination!



The Symantec C++ User’s Guide lists many of the good references on C++. Two others will be mentioned here.

The following book was just recently published and contains a good introduction to C++, the Symantec programming environment, and the THINK Class Library:

Rhodes and McKeehan, Symantec C++ Programming for the Macintosh, Brady, 1993.

The following book has a detailed discussion of the C++ data abstraction capabilities discussed in this article.

K.E. Gorlen, et. al., Data Abstraction and Object-Oriented Programming in C++, Wiley, 1990.

[And don’t forget Dave Mark’s Learn C++ on the Macintosh published by Addison-Wesley. - Ed.]

Macintosh Toolbox

The Fixed data type and its operations are documented in Volumes I and IV of Inside Macintosh.

Graphics and Animation

There are many standard textbooks that cover the graphics and animation topics mentioned in this article. Two of the best are:

Foley, et. al., Computer Graphics, Principles and Practice, 2nd ed., Addison-Wesley, 1990.

Rogers, Mathematical Elements for Computer Graphics, 2nd ed., McGraw-Hill, 1990.

Listing: fix.h
* fix.h
* fixed-point number class interface

#pragma once

#include <FixMath.h>
#include <iostream.h>
#include <assert.h>

const Fixed fixed_zero = Long2Fix((long) 0);

class fix {
// arithmetic operators
 friend inline fix operator+(const fix, const fix);
 friend inline fix operator-(const fix, const fix);
 friend inline fix operator*(const fix, const fix);
 friend inline fix operator/(const fix, const fix);
// I/O operators
 friend ostream& operator<<(ostream&, const fix&);
 friend istream& operator>>(istream&, fix&);
// transcendental functions that are optimized 
// for fixed-point numbers
 friend fix sin(const fix);
 friend fix cos(const fix);
// data value
 Fixed n;
// static data member for computation results
 static fix result;
// private range check member function
 inline void rangecheck();
// constructors
 fix() { n = fixed_zero; };
 fix(const long l) { n = Long2Fix(l); };
 fix(const long double d) { n = X2Fix(d); };
// incremant and decrement operators
 fix operator++()   { n += Long2Fix(1); return *this; }; 
// prefix
 fix operator--()    { n -= Long2Fix(1); return *this; };
 fix operator++(int) { n += Long2Fix(1); return *this; }; 
// postfix
 fix operator--(int) { n -= Long2Fix(1); return *this; };
// cast operators
 operator long double();
// other operators...
 fix operator+=(const fix f) 
 { *this = *this + f; return *this; }; 
 fix operator-=(const fix f) 
 { *this = *this - f; return *this; }; 
 fix operator*=(const fix f) 
 { *this = *this * f; return *this; }; 
 fix operator/=(const fix f) 
 { *this = *this / f; return *this; }; 
inline void fix::rangecheck()
 assert((n != 0x7FFFFFFF) && (n != 0x80000000));

inline fix operator+(const fix a, const fix b)
 fix::result.n = a.n + b.n;
 return fix::result;
inline fix operator-(const fix a, const fix b)
 fix::result.n = a.n - b.n;
 return fix::result;
inline fix operator*(const fix a, const fix b)
 fix::result.n = FixMul(a.n,b.n);
 return fix::result;
inline fix operator/(const fix a, const fix b)
 fix::result.n = FixDiv(a.n,b.n);
 return fix::result;
Listing: fix.cp
* fix.cp
* fixed-point number class implementation

#include "fix.h"
#include <math.h>
#include <SANE.h>

fix fix::result; // initialize static data member

fix::operator long double()
 long double result = Fix2X(n);
 return (result);

ostream& operator<<(ostream& os, const fix& f)
 os << (double) f;
 return os;

istream& operator>>(istream& is, fix& f)
 double temp;
 is >> temp;
 // Since this routine reads a floating-point number, 
 // and then converts it, errors are handled in the same 
 // way as for reading floating-point numbers.  The 
 // iostream library includes functions for testing 
 // and reseting the error state.
 if (is.good())
 f = (fix) temp;
 return is;

fix sin(const fix a)
 fix result;
 Fract temp = FracSin(a.n);
 result.n = Frac2Fix(temp);
 return result;

fix cos(const fix a)
 fix result;
 Fract temp = FracCos(a.n);
 result.n = Frac2Fix(temp);
 return result;
Listing: matrix.h
* matrix.h
* matrix template class interface

#pragma once

#include <iostream.h>
#include <assert.h>

template <class Number>
class matrix {
 Number m[4][4];
 friend ostream& operator<<(ostream&, const matrix<Number>&);
 inline Number& operator()(const int,const int); 
// for matrix indexing 
 matrix<Number> operator*(const matrix<Number>);

template <class Number>
inline Number& matrix<Number>::operator()(const int row, 
 const int col)
 assert(row >=0 && row <= 3 && col >=0 && col <= 3);
 return m[row][col];
Listing: matrix.cp
* matrix.cp
* matrix template class implementations

#include "matrix.h"
#include <iomanip.h>

template <class Number>
 int i,j;
 Number zero(0); // initialize any type of Number to zero
 m[i][j] = zero;

template <class Number>
matrix<Number> matrix<Number>::operator*(
 const matrix<Number> mat)
 matrix<Number> result;
 int i,j,k;
 result.m[i][j] += m[i][k] * mat.m[k][j];
 return result;

template <class Number>
ostream& operator<<(ostream& os, const matrix<Number>& mat)
 int i,j;
 os << setw(10) << mat(i,j);
 os << endl;
 return os;
Listing: matrix<double>.cp
* matrix<double>.cp
* pragmas to create public double matrix

#include "matrix.cp"
#pragma template matrix<double>
#pragma template_access public
Listing: matrix<fix>.cp
* matrix<fix>.cp
* pragmas to create public fix matrix

#include "fix.h"
#include "matrix.cp"
#pragma template matrix<fix>
#pragma template_access public
Listing: benchmark.cp
* benchmark.cp
* benchmark template function and main routine

#include "fix.h"
#include "matrix.h"
#include <math.h>
#include <iostream.h>
#include <console.h>
#include <profile.h>

 // Change the 0 below to a 1 to use the profiler.
 // Also turn on the following C++ compiler options:
 //    "Generate profiler calls"
 //    "Always generate stack frames"
 //    "Use function call for inlines"
const int use_profiler = 0;

const double pi = 3.14159;
const int num_iterations = 1000;

template <class Number>
unsigned long benchmark(matrix<Number> m,const int use_prof=0)
 unsigned long time1, time2, total;

 // Make the matrix an identity matrix, and print.
 m(0,0) = m(1,1) = m(2,2) = m(3,3) = 1;
 cout << "identity matrix:" << endl << m;
 // Create a rotation matrix, and print.
 matrix<Number> rotation;
 rotation(0,0) = rotation(1,1) = cos(pi/90);  
 // 2 degree rotation
 rotation(0,1) = rotation(1,0) = sin(pi/90);
 rotation(0,1) *= -1;
 rotation(2,2) = rotation(3,3) = 1;
 cout << "rotation matrix:" << endl << rotation;
 // Premultiply the matrix by the rotation 
 // matrix 1000 times.
 if (use_prof) InitProfile(200,200);
 for (int i=0; i<num_iterations; i++)
 m = rotation * m;
 if (use_prof) DumpProfile();

 // Print results.
 cout << "result of " << num_iterations << 
 " multiplications:" << endl << m;
 total = time2-time1;
 cout << "duration: " << total << " seconds" << endl;
 return total;

void main(int argc, char *argv[])
 unsigned long double_time, fix_time;
 double   ratio;

 // The ccommand() function allows results to be 
 // easily saved to a file.
 argc = ccommand(&argv);

 cout << 
 "calling the benchmark function on a double matrix..." 
 << endl;
 matrix<double> double_matrix;
 double_time = benchmark(double_matrix);
 cout << "calling the benchmark function on a fix matrix..." 
 << endl;
 matrix<fix> fix_matrix;
 fix_time = benchmark(fix_matrix,use_profiler);
 if (!use_profiler)
 if (fix_time > 0)
 ratio = (double)double_time/(double)fix_time;     
 cout << "fixed-point calculations were about " 
 << ratio << " times faster!" << endl;
 cout << 
 "fixed-point calculations completed in less than 1 second!" 
 << endl;

Apple Inc.
Microsoft Corpora
Google Inc.

MacTech Search:
Community Search:

Software Updates via MacUpdate

RapidWeaver 6.0 - Create template-based...
RapidWeaver is a next-generation Web design application to help you easily create professional-looking Web sites in minutes. No knowledge of complex code is required, RapidWeaver will take care of... Read more
NTFS 12.0.39 - Provides full read and wr...
Paragon NTFS breaks down the barriers between Windows and OS X. Paragon NTFS effectively solves the communication problems between the Mac system and NTFS, providing full read and write access to... Read more
RestoreMeNot 2.0.3 - Disable window rest...
RestoreMeNot provides a simple way to disable the window restoration for individual applications so that you can fine-tune this behavior to suit your needs. Please note that RestoreMeNot is designed... Read more
Macgo Blu-ray Player - Blu-r...
Macgo Mac Blu-ray Player can bring you the most unforgettable Blu-ray experience on your Mac. Overview Macgo Mac Blu-ray Player can satisfy just about every need you could possibly have in a Blu-ray... Read more
Apple iOS 8.1 - The latest version of Ap...
The latest version of iOS can be downloaded through iTunes. Apple iOS 8 comes with big updates to apps you use every day, like Messages and Photos. A whole new way to share content with your family.... Read more
TechTool Pro 7.0.5 - Hard drive and syst...
TechTool Pro is now 7, and this is the most advanced version of the acclaimed Macintosh troubleshooting utility created in its 20-year history. Micromat has redeveloped TechTool Pro 7 to be fully 64... Read more
PDFKey Pro 4.0.2 - Edit and print passwo...
PDFKey Pro can unlock PDF documents protected for printing and copying when you've forgotten your password. It can now also protect your PDF files with a password to prevent unauthorized access and/... Read more
Yasu 2.9.1 - System maintenance app; per...
Yasu was originally created with System Administrators who service large groups of workstations in mind, Yasu (Yet Another System Utility) was made to do a specific group of maintenance tasks... Read more
Hazel 3.3 - Create rules for organizing...
Hazel is your personal housekeeper, organizing and cleaning folders based on rules you define. Hazel can also manage your trash and uninstall your applications. Organize your files using a... Read more
Autopano Giga 3.7 - Stitch multiple imag...
Autopano Giga allows you to stitch 2, 20, or 2,000 images. Version 3.0 integrates impressive new features that will definitely make you adopt Autopano Pro or Autopano Giga: Choose between 9... Read more

Latest Forum Discussions

See All

Ghost Blade (Games)
Ghost Blade 1.1 Device: iOS Universal Category: Games Price: $4.99, Version: 1.1 (iTunes) Description: Get the most outstanding 3D Action Game from App Store NOW! For those who dare pursue dreams. Masterpiece of Yu Shi Game ---China... | Read more »
Fiete – A Day on a Farm Review
Fiete – A Day on a Farm Review By Amy Solomon on October 21st, 2014 Our Rating: :: A MEMORABLE EXPERIENCEUniversal App - Designed for iPhone and iPad Fiete – A day on a farm in an interactive app for young children full of... | Read more »
Tilt to Live: Gauntlet’s Revenge is Almo...
Tilt to Live: Gauntlet’s Revenge is Almost Here Posted by Jessica Fisher on October 21st, 2014 [ permalink ] One Man Left has announced the official release date of Tilt to Live: Gauntlet’s Re | Read more »
Sago Mini Monsters Celebrates Halloween...
Sago Mini Monsters Celebrates Halloween with Fun Costumes and Special Treats. Posted by Jessica Fisher on October 21st, 2014 [ permal | Read more »
Inferno 2 Review
Inferno 2 Review By Andrew Fisher on October 21st, 2014 Our Rating: :: TWIN STICK GOODNESSUniversal App - Designed for iPhone and iPad With tight controls and awesome, stark visuals, Inferno 2 is loads of fun.   | Read more »
Clips Review
Clips Review By Jennifer Allen on October 21st, 2014 Our Rating: :: CONVENIENT PASTINGUniversal App - Designed for iPhone and iPad Making copying and pasting more powerful than usual, Clips is a great way to move stuff around.   | Read more »
MonSense Review
MonSense Review By Jennifer Allen on October 21st, 2014 Our Rating: :: ORGANIZED FINANCESiPhone App - Designed for the iPhone, compatible with the iPad Organize your finances with the quick and easy to use, MonSense.   | Read more »
This Week at 148Apps: October 13-17, 201...
Expert App Reviewers   So little time and so very many apps. What’s a poor iPhone/iPad lover to do? Fortunately, 148Apps is here to give you the rundown on the latest and greatest releases. And we even have a tremendous back catalog of reviews; just... | Read more »
Angry Birds Transformers Review
Angry Birds Transformers Review By Jennifer Allen on October 20th, 2014 Our Rating: :: TRANSFORMED BIRDSUniversal App - Designed for iPhone and iPad Transformed in a way you wouldn’t expect, Angry Birds Transformers is a quite... | Read more »
GAMEVIL Announces the Upcoming Launch of...
GAMEVIL Announces the Upcoming Launch of Mark of the Dragon Posted by Jessica Fisher on October 20th, 2014 [ permalink ] Mark of the Dragon, by GAMEVIL, put | Read more »

Price Scanner via

Select MacBook Airs $100 off MSRP, free shipp...
B&H Photo has 2014 a couple of MacBook Airs on sale for $100 off MSRP. Shipping is free, and B&H charges NY sales tax only. They also include free copies of Parallels Desktop and LoJack for... Read more
13-inch 2.5GHz MacBook Pro on sale for $100 o...
B&H Photo has the 13″ 2.5GHz MacBook Pro on sale for $999.99 including free shipping plus NY sales tax only. Their price is $100 off MSRP. Read more
Strong iPhone, Mac And App Store Sales Drive...
Apple on Monday announced financial results for its fiscal 2014 fourth quarter ended September 27, 2014. The Company posted quarterly revenue of $42.1 billion and quarterly net profit of $8.5 billion... Read more
Apple Posts How-To For OS X Recovery
OS X 10.7 Lion and later include OS X Recovery. This feature includes all of the tools you need to reinstall OS X, repair your disk, and even restore from a Time Machine backup. OS X Recovery... Read more
Mac OS X Versions (Builds) Supported By Vario...
Apple Support has posted a handy resource explaining which Mac OS X versions (builds) originally shipped with or are available for your computer via retail discs, downloads, or Software Update. Apple... Read more
Deals on 2011 13-inch MacBook Airs, from $649
Daily Steals has the Mid-2011 13″ 1.7GHz i5 MacBook Air (4GB/128GB) available for $699 with a 90 day warranty. The Mid-2011 13″ 1.7GHz i5 MacBook Air (4GB/128GB SSD) is available for $649 at Other... Read more
2013 15-inch 2.0GHz Retina MacBook Pro availa...
B&H Photo has leftover previous-generation 15″ 2.0GHz Retina MacBook Pros now available for $1599 including free shipping plus NY sales tax only. Their price is $400 off original MSRP. B&H... Read more
Updated iPad Prices
We’ve updated our iPad Air Price Tracker and our iPad mini Price Tracker with the latest information on prices and availability from Apple and other resellers, including the new iPad Air 2 and the... Read more
Apple Pay Available to Millions of Visa Cardh...
Visa Inc. brings secure, convenient payments to iPad Air 2 and iPad mini 3as well as iPhone 6 and 6 Plus. Starting October 20th, eligible Visa cardholders in the U.S. will be able to use Apple Pay,... Read more
Textkraft Pocket – the missing TextEdit for i...
infovole GmbH has announced the release and immediate availability of Textkraft Pocket 1.0, a professional text editor and note taking app for Apple’s iPhone. In March 2014 rumors were all about... Read more

Jobs Board

Senior Event Manager, *Apple* Retail Market...
…This senior level position is responsible for leading and imagining the Apple Retail Team's global event strategy. Delivering an overarching brand story; in-store, Read more
*Apple* Solutions Consultant (ASC) - Apple (...
**Job Summary** The ASC is an Apple employee who serves as an Apple brand ambassador and influencer in a Reseller's store. The ASC's role is to grow Apple Read more
Project Manager / Business Analyst, WW *Appl...
…a senior project manager / business analyst to work within our Worldwide Apple Fulfillment Operations and the Business Process Re-engineering team. This role will work Read more
*Apple* Retail - Multiple Positions (US) - A...
Job Description: Sales Specialist - Retail Customer Service and Sales Transform Apple Store visitors into loyal Apple customers. When customers enter the store, Read more
Position Opening at *Apple* - Apple (United...
…customers purchase our products, you're the one who helps them get more out of their new Apple technology. Your day in the Apple Store is filled with a range of Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.