Assignment 9 Floating Point Arithmetic

Assigned: 3 April 2001
Due: 17 April 2001

The purpose of this assignment is to give you additional assembly language programming experience and some exposure to floating point arithmetic.

The function "float exp(float x)" rasies e (the base of the natural logarithms) to the x power. Computers evaluate this function using a series. The series for exp is particularly simple. It is just

1 + x + x*x/2 + x*x*x/6 + x*x*x*x/24 + x*x*x*x*x/120 ... or

sum over i of x^i / i!  (x to the ith power divided by i factorial)

An unlikely C implementation  might look like

main()
{
    constant int N = 30; // or whatever appropriate number of terms
    float x = 1; // the power of e
    float terms[N];	// store the terms so we can add both ways
    float num = 1;	// numerator
    float den = 1;	// denominator
    for(int i=0; i<N; i++) {
	terms[i] = num / den;	// compute this term in the series
	num = num * x;		// bump the power of x
	den = den * (i+1);	// bump the factorial
    }
    
    float forward_sum = 0;
    for(int i=0; i<N; i++) {
	forward_sum = forward_sum + terms[i];
    }
    print_float(forward_sum);
    float reverse_sum = 0;
    for(int i=N-1; i>=0; i--) {
	reverse_sum = reverse_sum + terms[i];
    }
    print_float(reverse_sum);
    float difference = forward_sum - reverse_sum;
    print_float(difference);
}

Your assignment is to

  1. Write the above code (or equivalent) in MIPS assembly language and run it on the SPIM simulator.
  2. Compare the values you get to known values for e to the x. What does your program compute for x=1? For x=0? For x = -1? For x = 10?
  3. Explain why forward_sum and reverse_sum are different. Which do you think is most accurate? Why?

As usual email your program and the answers to the questions above to chunfa@email.unc.edu and put HW9 in the subject line.