Nov 27, 2005 - For testing was used bash commands like this one: a=1000;time for i in 'seq ... Speed of other functions
Iterative methods documentaion of 2nd IZP project
November 27, 2005
Author: Jakub Zverina,
[email protected] Faculty of Information Technology Brno University of Technology
Contents 1
Requirements 1.1 Overview . . . . . . . . . 1.2 Specificaton . . . . . . . . 1.3 User manual . . . . . . . . 1.3.1 Synopsis . . . . . 1.3.2 Description . . . . 1.3.3 Options . . . . . . 1.3.4 Examples . . . . . 1.4 Performance . . . . . . . . 1.4.1 Testing conditions 1.4.2 Testing methods . 1.4.3 Testing results . . 1.5 Problem analysis . . . . . 1.5.1 sin(x) . . . . . . . 1.5.2 ln(x) . . . . . . . . 1.5.3 exp(x) . . . . . . . 1.5.4 sqrt(x) . . . . . . . 1.5.5 Common functions
. . . . . . . . . . . . . . . . .
2 2 2 2 2 3 3 3 3 3 3 3 4 4 4 4 4 4
2
Design 2.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.2 Runtime structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
5 5 5
3
Testing 3.1 Strategy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3.2 Test results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
6 6 6
4
Reflection 4.1 Evaluation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4.2 Lessons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4.3 Known bugs and limitations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
6 6 6 6
5
Appendix 5.1 Formats . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5.2 Test cases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
6 6 7
. . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . .
1
. . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . .
1
Requirements
1.1
Overview
This program has been made as project into Introduction to Programming Systems course. It provides functions for basic mathematical operation. The functions are sin(), ln(), exp() and sqrt() which performs computation of sine, natural logarithm, the power of Euler’s constant and square root of given number. Maximal error, function and input number are given as program arguments as described below (chapter 1.3).
1.2
Specificaton
The program have to compute following functions implemented by basic mathematical operations (+,-,*,/) with accuracy specified by number representing maximal error. The list of functions is: √ • x • sin(x) • ex • ln(x) Calculation is realised in real numbers [1], represented in C programming language by type double (we’re not using complex numbers). Determine domain of these functions in relation to used data type. Count of input data lines (numbers) must not be limited. Functions from standard math library such as sqrt(), sin(), exp(), log(), etc. must not be used except special cases used for parsing of input (such as trunc() or constants NAN and INFINITY). Common mathematical constants can be used: const double IZP E = 2.7182818284590452354; // e const double IZP PI = 3.14159265358979323846; // pi const double IZP 2PI = 6.28318530717958647692; // 2*pi const double IZP PI 2 = 1.57079632679489661923; // pi/2 const double IZP PI 4 = 0.78539816339744830962; // pi/4 Behavior of the program is modified by command line arguments, data are read from stdin in formats as described in chapter 5.1.
1.3 1.3.1
User manual Synopsis
proj2 option error
2
1.3.2
Description
Option specifies which of implemented function will being used. Error is nubmer representing maximal acceptable error from exact result. Compiling of program: gcc -std=c99 -Wall -pedantic -g -lm proj2.c -o proj2 System was tested only on GNU/Linux and i386 platform. 1.3.3
Options
-sin this function performs calculation of sin(x) -exp this performs calculation of ex -ln this performs calculation of ln(x) √ -sqrt this performs calculation of x
1.3.4
Examples
file is forwarded into stdin of program: proj2 -sin 0.001 < data.txt program is used as filter: cat data.txt | proj2 -cos 1e-3 data are written directly into stdin: proj2 -ln 1e-3
1.4 1.4.1
Performance Testing conditions
Performance was tested on dual Intel(R) Pentium(R) 4 CPU 2.60GHz with 512kB cache. While performance has being tested there were 33 user logged in. Average load in last 5 minutes was 0,18. 1.4.2
Testing methods
Performance was meassured by various unix tools, especially bash, bc and time. For testing was used bash commands like this one: a=1000;time for i in ‘seq 1 $a‘; do echo "scale=5;$i/$a*3.14" | bc ; done | ./proj2 -sin 0.0001 >/dev/null result for sin(), 1000 numbers from 0 to 3.14:
1.4.3
Testing results
• accuracy:0.1 - sys 1.936s • accuracy:0.0001 - sys 1.904s • accuracy:0.000001 - sys 1.832s 3
• accuracy:0.00000001 - sys 1.912s Input numbers are optimized by program using periodicity of sin(), so testing of numbers in range 1 - 100 was practically the same as above. Speed of other functions was very similar so it is not necessary to include result even for them.
1.5 1.5.1
Problem analysis sin(x)
The implementation of sin(x) [2] function uses for optimalization periodicity of sine. Value of function is then calculated as sum of Taylor’s series. [3] Periodicity and optimization is defined by angle = generalAngle − 2π · int( Calculation is performed by sin x =
∞ X 0
1.5.2
generalAngle ) 2π
(−1)n−1 2n−1 x (2n − 1)!
(1)
(2)
ln(x)
Natural logarithm [4] performs computation with use of Continued fraction representations [5] 1+x 2x x2 4x2 ln( )= ... 1−x 1− 3 − 5 − 1.5.3
(3)
exp(x)
The power of Euler’s constant [7], which is base of natural logarithm is defined as limit 1 e = x→∞ lim (1 + )x x 1.5.4
(4)
sqrt(x)
√ Square root [6] of number is a number r such that r2 = x. The function x is therefore the inverse function of f (x) = x2 for x ≥ 0. For calculation of value of this function is used Newton’s iteration 1 n xk+1 = (xk + ) 2 kx
(5)
with x0 = 1 1.5.5
Common functions
For these function in connection with the specification which says not to use standard math library, few functions are implemented for ancillary uses. These are pretty simple so it’s not important to mention their algorithms. It is abs(x) which calculates absolute value of number, the nth power of number, where n is integer and factorial. 4
STDIN
STDOUT ln()
lnR()
main() ARGS
exp()
expR()
sin() sqrt()
absolute()
powerInt()
fact()
sqrtR() getToInterval()
sinR()
Figure 1: Dataflow diagram
2 2.1
Design Overview
Program is small so it is one file with few functions. Only external libraries it uses are standard C libraries and these are stdio.h, stdlib.h, string.h and for two constants, mentioned at chaper 1.2, math.h. Functions are using recursive alhorithms which was for these purposes the most ideal, fast and simple to implement.
2.2
Runtime structure
As specification declares most of data types are 32bit signed C type double. Data flow diagram on figure 1 represents how functions are used.
5
3 3.1
Testing Strategy
Testing was based on specifications (chapter 1.2). For testing wasa choosed overall strategy called Black box. This part due to size of program didn’t met any effectual problems. Test was performed by bash script with wide range of allowed values. Neither problems are while compilation of program which takes no more than 0.030ms of system time.
3.2
Test results
While test results seems to be good, there should be expected few faults when handling errorneous input data such as input numers out of range of double type or results which may exceed this 32bit type.
4 4.1
Reflection Evaluation
Design of function structure is not ideal and should be much improved. There are few useless functions which could be replaced by one function – e.g. sin() and sinR() etc. No other major issues are needed to be solved and program generally cover most of it’s specification.
4.2
Lessons
While making this program and this documentation, I earned some experiences in programming in C language mostly the language basics. While parsing parameters I have learnd basic string handling (such as comparing of strings) and use of recursive algorithms. Documentation is written in english because of bad support of UTF-8 on system which it was made on. While making this domcumentation I have learned common technologies that should be used for this purpose such as LATEX for document creating and dia for making of diagrams.
4.3
Known bugs and limitations
As mentioned in chapter 4.1 there are known few limitations. Handling of these “errors” is not subject of this project and objective of it, which is to earn experiences in C language and basic algorithms, was reached sucessfuly.
5 5.1
Appendix Formats
Behavior of program is specified in chapter 1.3. Now it is time to specify data format which is expected to be on STDIN. Numbers (arguments of function which will be performed) are read from STDIN and each of them have to be divided by whitespace characters (spaces, tabs or linefeeds). Parsing of arguments is terminated by EOF constant. 6
Results are printed into STDOUT and format of them is 10 decimal numbers multiplied by nth power to the 10. E.g: 1.0000000000e+12 3.1622776601e+02 1.4035668847e+00
5.2
Test cases
While testing the software, two major types of test were used. One was performance test and other was stress test for testing all wide range of allowed values including stress conditions. Results are presented in chapter 3 and 1.4.
References [1] Eric W. Weisstein. ”Real Number.” From MathWorld–A Wolfram Web Resource. http:// mathworld.wolfram.com/RealNumber.html [2] Eric W. Weisstein. ”Real Number.” From MathWorld–A Wolfram Web Resource. http:// mathworld.wolfram.com/Sine.html [3] Eric W. Weisstein. ”Taylor Series.” From MathWorld–A Wolfram Web Resource. http:// mathworld.wolfram.com/TaylorSeries.html [4] Eric W. Weisstein. ”Natural Logarithm.” From MathWorld–A Wolfram Web Resource. http: //mathworld.wolfram.com/NaturalLogarithm.html [5] Eric W. Weisstein. ”Continued Fraction.” From MathWorld–A Wolfram Web Resource. http: //mathworld.wolfram.com/ContinuedFraction.html [6] Eric W. Weisstein. ”Square Root.” From MathWorld–A Wolfram Web Resource. http:// mathworld.wolfram.com/SquareRoot.html [7] Eric W. Weisstein. ”e.” From MathWorld–A Wolfram Web Resource. http://mathworld. wolfram.com/e.html
7