Great Deal! Get Instant $10 FREE in Account on First Order + 10% Cashback on Every Order Order Now

Your task for this assignment is to implement a small number of mathematical functions (e* and /x), mimicking , and using them to compute the fundamental constants e and 7. You will also need to write...

1 answer below »
Your task for this assignment is to implement a small number of mathematical functions (e* and /x), mimicking
math.h>, and using them to compute the fundamental constants e and 7. You will also need to write a dedicated
test harness comparing your implemented functions with that of the math li
ary's, then analyzing and presenting
your findings in a writeup. The test harness should be a program named mathlib-test. The interface for you
math li
ary will be given in mathlib.h. You may not modify this file. The following sections will describe the
functions that you need to write, and the files that should contain the functions.
You are strictly fo
idden to use any functions from in your own math li
ary. You are also fo
idden
to write a factorial () function.
Each of the functions you will write must halt computation using an € = 10~'4, which will be defined inmathlib.h.
For example, consider approximating the value of e. For sufficiently large k, |x¥| < k!. As seen in Figure 1, x* dom-
inates
iefly, but is quickly overwhelmed by k!, making the ratio rapidly approach zero.
6.1 e.c
This file should contain two functions: e() and e_terms(). The former function will approximate the value of e
using the Taylor series presented in §3 and track the number of computed terms by means of a static variable
local to the file. The latter function will simply return the number of computed terms.
6.2 madhava.c
This file should contain two functions: pi_madhava() and pi_nadhava_terms (). The former function will ap-
proximate the value of 7 using the Madhava series presented in §4.2 and track the number of computed terms with
astatic variable, exactly like in e. c. The latter function will simply return the number of computed terms.
6.3 euler.c
This file should contain two functions: pi_euler() and pi_euler_terns(). The former function will approxi-
mate the value of x using the formula derived from Euler's solution to the Basel problem, as described in §4.4. It
should also track the number of computed terms. The latter function will simply return the number of computed
terms.
6.4
p.c
‘This file should contain two functions: pi_
p() and pi_
p_terms (). The former function will approximate the
value of 7 using the Bailey-Borwein-Plouffe formula presented in §4.5 and track the number of computed terms.
The latter function will simply return the number of computed terms.
6.5 viete.c
This file should contain two functions: pi_viete () and pi_viete_factors(). The former function will approx-
imate the value of 7 using Viete's formula as presented in §4.6 and track the number of computed factors. The
latter function will simply return the number of computed factors.
6.6 newton.c
This file should contain two functions: sqrt_newton() and sqrt_newton_iters(). The former function will
approximate the the square root of the argument passed to it using the Newton-Raphson method presented in §5.
This function should also track the number of iterations taken, which the latter function will return.
6.7 mathlib-test.c
This file will contain the main test harness for your implemented math li
ary. It should support the following
command-line options:
+ -a:Runs all tests.
* -e: Runs e approximation test.
-b: Runs Bailey-Borwein-Plouffe 7 approximation test.
-m: Runs Madhava x approximation test.
-r: Runs Euler sequence 7 approximation test.
-v: Runs Viéte zr approximation test.
-n: Runs Newton-Raphson square root approximation tests.
+ -s: Enable printing of statistics to see computed terms and factors for each tested function.
« -h: Display a help message detailing program usage.
The expected output for each of the e or 7 tests should resemble the following:
./mathlib-test -e -b -v
XXXXXXXXXX, M_E = XXXXXXXXXX, diff = XXXXXXXXXX
XXXXXXXXXX, M_PI = XXXXXXXXXX, diff = XXXXXXXXXX
= XXXXXXXXXX, M_PI = XXXXXXXXXX, diff = XXXXXXXXXX

Note that the newline should occur after the printed difference. You can refer to the reference program in the
esources repostiroy for the example output. With the statistics option enabled, the output should resemble the
following:
$ ./mathlib-test -e -b -v -s
e() = XXXXXXXXXX, M_E = XXXXXXXXXX, diff = XXXXXXXXXX
e terms = 18
pi_
p() = XXXXXXXXXX, M_PI = XXXXXXXXXX, diff = XXXXXXXXXX
pi_
p() terms = 11
pi_viete() = XXXXXXXXXX, M_PI = XXXXXXXXXX, diff = XXXXXXXXXX
RLS px}

You will specially test your sqrt_newton () function in the range [0, 10) in steps of 0.1. Again, you can refer to
the reference program in the resources repository for the example output. Any double that is printed should use
the following printf () format specifier:
161f\n", pi

7 Command-line Options
Afew dud universes can really clutter up your basement.

—Neal Stephenson, In the Beginning... Was the Command Line
Your test harness will determine which of your implemented functions to run through the use of command-line
option. In most C programs, the main () function has two parameters: int argc and char **argv. Acommand,
such as ./exec argl arg? is split into an a
ay of strings refe
ed as arguments. The parameter argv is this
a
ay of strings. The parameter argc serves as the argument counter, which is the number of arguments that were
supplied. Try the following code, and make sure that you understand it.
Printing out supplied command-line arguments.
#include ETS TYCEY aero [ETT oan
for ( i=0; i prints ("argvl4al = %s\n", i, argv[i);
i
Et

A command-line option is an argument, usually prefixed with a hyphen, that modifies the behavior of a com-
mand or program. They are typically parsed using the getopt () function.
Instead, use the getopt () function. Command-line options must be defined in order fo
getopt () to parse them. These options are defined in a string, where each character in the string co
esponds to
an option character that can be specified on the command-line. Upon running the executable, getopt () should
e used to scan through the command-line arguments, checking for option characters in a loop.
Parsing options using getopt ().
#include [EENSRT ORR EIT
OPTIONS "pi:"
EYEENS [STL LETTS
BT
while ((opt = getopt (argc, argv, OPTIONS))
PES AE TO Ie
case 'p':
printf ("-p option.\n");
eak;
case 'i':
printf ("-i option: %s is parameter.\n", optarg);
EI
pba
}
Ft

This example program supports two command-line options, -p and -i. Note that the option character ‘i’ in
the defined option string OPTIONS has a trailing colon. The colon signifies, when the -i option is enabled on the
command-line, that getopt () should search for a option argument following it. An e
or is thrown by getopt ()
if an argument for a option, or flag, requiring one is not supplied.
8 Deliverables
You will need to turn in the following source code and header files:
1.
p. c: This contains the implementation of the Bailey-Borwein-Plouffe formula to approximate 7 and the
function to return the number of computed terms.
2. e.c: This contains the implementation of the Taylor series to approximate Euler's number e and the function
to return the number of computed terms.
3. euler.c: This contains the implementation of Euler's solution used to approximate 7 and the function to
eturn the number of computed terms.
4. madhava.c: This contains the implementation of the Madhava series to approximate 7 and the function to
eturn the number of computed terms.
5. mathlib-test.c: This contains the main () function which tests each of your math li
ary functions.
6. mathlib.h: This contains the interface for your math li
ary.
7. newton. c: This contains the implementation of the square root approximation using Newton's method and
the function to return the number of computed iterations.
8. viete.c: This contains the implementation of Viéte's formula to approximate 7 and the function to return
the number of computed factors.
You may have other source and header files, but do not make things over complicated.
You will also need to turn in the following:

Writing Protion
Writing Portion
Description 1
Describe how to use your program and Makefile. It should also list and explain any command-line options that you
program accepts. Any false positives re- ported by scan-build should be documented and explained here as well.
Note down any known bugs or e
ors in this file as well for the graders.
Description 2
describe your design and design process for your program with enough detail such that a sufficiently knowledgeable
programmer would be able to replicate your implementation. This does not mean copying your entire program in
ve
atim. You should instead describe how your program works with supporting pseudocode.
Description 3
Graphs displaying the difference between the values reported by your implemented functions and that of the math
li
ary’s. Analysis and explanations for any discrepancies and findings that you glean from your testing.
Answered 3 days After Jan 26, 2023

Solution

Aditi answered on Jan 28 2023
49 Votes
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here