C Course Homework


Homework 2 - Squareroot: Scientific Programming

Program Background

We will write a program to find the squareroot of a real number. Real numbers on a machine cannot be exactly represented so we must compute an approximation.

We will use two different techniques and compare them for purposes of accuracy and efficiency. The first technique is using an existing C library function to compute the squareroot of a number. The second technique is using an algorithm called a Taylor approximation to compute the squareroot.

Using an existing library function:


      double sqrt(double); /* found in math.h; use flag -lm with gcc */

This function is found in math.h, and in order to use it, you have to #include this library and compile your code using the "-lm" flag with gcc. The function can be used as follows to print the squareroot of a number:


      printf("\nsqrt(%lf)  = %lf", x, sqrt(x));

Using Taylor approximation:

The Taylor approximation is:

You must program the Taylor approximation and print its answer as well as its difference from the library function answer. However, this approximation only works when 0 < x < 2. To make it work for larger values of x, we can use the relation . This lets us use a square of number k to reduce X until it is in the radius of convergence 0 < x < 2. We will discuss this in lecture.


Program Description

Write a program using scanf() to get a next value of a double and find its squareroot with both the library function and the Taylor series method. Compare the two answers printing each and the difference. If the squareroot is outside the radius of convergence use the above identity to reduce to an appropriate value. The program should terminate when it is given a number less than or equal to 0.0.

Therefore, the program should have the structure:


#include <math.h>
#include <stdio.h>
int main(void)
{ ....
   loop until sentinel value less than or equal to 0 {
      prompt user for next value
      read in value x
      ...use sqrt(x) ...
      ...in radius of convergence okay
         use Taylor series approximation
         otherwise reduce
   }
   return 0;
}

Grading

Style -- 3 points total


Correctness -- 6 points total


Overall appearance
-- 1 point total

This is subjective. Points awarded for neatness, or maybe a unique approach to finding the taylor squareroot of a number.


Deductions

Deduct 3 points if the program does not compile. (use "gcc -lm")


Hints

  • Use -lm flag to link in math library.
    For example, if my program is called "sqrt.c", type

     gcc sqrt.c -lm 
  • To compute the squareroot of a value using the Taylor formula, use a loop to reduce the input value until it is less than 2.0. You will have to choose some constant, k, and you will have to keep track of a correction factor (or somehow compute it using the number of times the loop was executed). Here's an example of how this might work:
          Let's say initx = x = 40, and we define k = 2.
    
          loop                    correction        mathematical
          number   x      k       factor            equivalent
          ---------------------------------------------------------------------
          init.   40      2       k^0=1       root(initx) = root(x)
          1       10      2       k^1=2                   = root(k*k*x)
                                                          = k * root(x)
          2       2.5     2       k^2=4                   = k * root(k*k*x)
                                                          = k * k * root(x)
          3       0.625   2       k^3=8                   = k * k * root(k*k*x)
                                                          = k * k * k * root(x)
    
          Note that when I say root(x), I am referring to the mathematical
          operation of taking the square root of a number.  I am not referring
          to the built-in C function "sqrt()".
         
    Now x is small enough to use the Taylor Formula, and to get the squareroot of the inputValue, we have to compensate by k^3 (or our correction factor).

    FAQ


    Star Solutions for Squareroot Program