Вы находитесь на странице: 1из 25

Rep of real num

The real nos are shorted with a decimal precision(on mantissa) and the decimal
exponent range

9.90625_10=1001.11101_2

In general x=+- r*10^n with a real non-zero no with a range number in the range
1/10<=r<1

X==+-q*2^m with q a no in the range ½<=q<1

The mantissa of a binary no is rep by general formula (0.a_-1a_-2….a_-n)_2 = a_-


1*2^-1+…+a_-n*2^-n

Therefore the num x is rep as x = (-1)^s *mantissa*2^(exponent)

Where, s is the sign but, the exponent gives the available range.

With a single-precision word, 32 bits, 8 bits would be reserved for the exponent, 1
bit for the sign and 23 for the mantissa

C++: float size_of_fossile; size_of_fossil will have significant digit gvn by


½^(23)=10(-17). The range of the exponent goes from 2^(-128)=2.9*10^(-39) to 2^(127)
= 3.4*10^38 to 2^127 = 3.4*10^38. Where 128 stems from the fact that 8 bits are
reserved for the exponent.

In such notation, (1.a_(-1)a_(-2)……a_(-23))_2 = 1*2^0+a_(-1)*2^(-1)+…..a(-n)*2^(-23)

EX: consider 32 bit binary number (10111110111101000000000000000000)_2

 First 1 ----sign@ the first bit Is reserved for the sign


 1 is for negative sign
 m is given by the next 8 binary number (01111101 resulting in 125 in the
decimal system)
 2^8 – 1 = 255 numbers in the interval -128<=m<=127
 Final exponent is the 125-127 = -2 resulting in 2^(-2)

Inserting the sign and the mantissa yields -2^(-2)(1*2^0+1*2^(-1)+…1*2^(-5) = -


(0.476525)_(10)
A float number x, will be represented as fl(x) = x(1+- E_x), with x is the exact
number and the error E_x<=E_m , where E_m is the precision assignment

A number like 1/10 has exact binary representation with single onn double
precision since the mantissa 1.( a_(-1)a_(-2)……a_(-23))_2 is always truncated at
some stage n due to its limited number of bits, there is only limited number of
real biary number 1

 The spacing between every real binary number is given by the chosen
machine precision. For 32 bit words ~ E-m ~ 10^(-7) and for double precision
64 bits we have E_m~10^(-16) or in terms of binary bases as 2^(-23) and 2(-
52) for single and double precision

EXAMPLE:::

PROGRAM SH

IMPLICIT NONE

REAL(KIND = 8)::r

REAL(KIND = 8)::s

WRITE(*,*) 'INPUT A NUM'

READ(*,*) r

s = SIN (r)

WRITE(*,*) 'HELLO WORLD! SINE OF' , r, '=', s

END PROGRAM SH

 The first statement must be a program statement, the last statement must
have corresponding end program statement
 Name of the variables must be between 1 and 31 alphanumeric characters
of which the first must be a letter and last must not be a underscore
 Asterisks (*,*) following WRITE represents the default form at for output
 READ (*,*) means that the program is expecting a line input
 IMPLICIT REAL *8(a-h, o-z), meaning all the variables beginning with any
of the above letters are be default floating numbers. This makes it hard to
spot erantual errors due to miss speling of variables name with IMPLICIT
NONE one has to declare all variables and therefore detect possible errors
already while compiling

Programming examples on loss of precision and round off errors

Algorithms for e^(-x)

Consider three possible algorithms for computing e^(-x)

1. Simplify cofing e^(-x) = Sum(0 to infinity) (-1)^xx^n/n!


2. Or to eploy a recrussion relation for e^(-x) = Sum(0 to infinity) s_n = Sum(0
to infinity) (-1)^n x^n/n! using s_n = -s_(n-1)x/n
3. Or to first to calculate exp x = Sum(0 to infinity) s_n === inverse e^(-x) =
1/exp x , e^(-x) = Sum(0 to infinity) (-1)^n x^n/n! for x-values ranging
from 0 to 100 in steps of 10
Truncation error = 1.0 E-10

 For low values of x, the agreement is good


 For x = 70--- overflow problems, NaN @171! Is beyond the limit set for
double precision
 Computer sets 171! –t0- 0

To control the overflow problem, recurrence formula for the terms in the sum
can be used exp(-x) = Sum(0 to infinity) s_n = Sum(0 to infinity) (-1)^n x^n/n!

S_n = -s_(n-1) x/n

Fortran

REAL(KIND =8)

 FORTRAN does not allow floating numbers as loops variables


 DO WHILE
EXIT statement
 IF construct allows the execution of a sequence of statements(ablock) to
depend on a conduction
 IF…THEN……………………..
END IF
 Subprograms are called from main program or other subprograms. TYPE
factorial (int); if void factorial(int); then equivalent to FORTRAN
subroutine
 Subroutines are used if we have more than one return value
 INTENT(IN) --- the dummy argument cannot be changed within the
subprogram
 INTENT(OUT) – the dummy argument cannot be changed within the
subprogram until it is given with the intent of passing a value back to the
calling program
 INTENT(INOUT) – the dummy argument has an initial value which is
changed and passed back to the calling program

FORTRAN

 The MODULE declaration in FORTRAN allows one place to place


functions like the one which calculates factorial
 MODULE helps in such a way that if one which if one wishes to change the
declaration in one part of the program
 Global variable declaration
 The variable truncation is accessible to all functions which have USE
constants declaration
 The ‘USE’ declaration has to come before any variable decleration and
IMPLICIT NONE statement

Ex: calculate s1 = \sum_1^N (1/n) and s2 = \sum_N^1 (1/n), both using single
precision and double precision separately for N = 1000000 & N = 10^8 and
compare the results. Interpret the results

Standard algorithm for the standard deviation

For N data points, 1-D array x(i), I = 1 to N, the mean, \tilda x =


frac{\sum_1^N x(i)}{N} and standard deviation \sigma = sqrt[ \frac{\sum_1^N
x(i)^2 - \tilda x \sum_1^N x(i)}{N-1}]. Let x(i) = i+10^5, N = 127, the second
algorithm for calculating standard deviation, \sigma = \sqrt[\frac{\sum_1^N
(x_i - \tilda x)^2}{N-1}]

ARITHMETIC OPERATORS RELATIONAL OPERATORS


-Subtraction >greatertha .GT.
+ addition < less than .GE.
/ division >= greater than equal to
% MOD modulus division <= less than equatl to
--decrement == equal
++increment !=notequal

LOGICAL OPERATORS

C++ effect FORTRAN

0 false value .FALSE.

1 true value .TRUE.

!x logical negation .NOT.x

X&&y logical AND x. AND .y

X||y logical inclusive OR x. OR. Y

BITWISE OPERATOR

~i bitwise complement NOT(i)

I&j bitwise and IAND(I,j)

I^j bitwise exclusive or IEOR(I,j)

I|j bitwise inclusive or IOR(i,j)

I<<j bitwise shift left ISHIFT(I,j)

i>>n bitwise shifr right ISHIFT(i, nj)

c++ specific expression


Exp Mea
a+=b
a-=b
a*=b
a/=b
a%= b
a<<=b A = a<<b;
a>>=b
A&=b
A|=b
A^=b

POINTERS AND ARRAY IN C++

POINTERS AND ARRAY ARE IMPORTANT ELEMENTS IN C++

Int name Defines an integer variable called name. it is given an address


in memory where we can store an integer name
&name Address of a specific place in memory where the integer name
is stored. Placing the operator & infront of a variable yields
its address in memory
Int* pointer Defines an integer pointer and recives a location in memory
for this specific variables. The content of this location is
viewed as the address of onother place memory where we
have stored on integer

In c++ int* pointer;


In c int *pointer;

LEGAL C++ EXPRESSIONS

Name = 0 x 56; //name gets the hexadecimal value hex 56

Pointer = &name //pointer points to name

Printf(“address of name = %p”,pointer); //writes out the address of


name
Printf(“address of name = %d”,pointer); // writes value of name

#include<stdio.h>

#include<iostream>

using namespace std;

main()

int var;

int *pointer;

pointer = &var;

var = 421;

printf("adress of the integer var: %p\n",&var);

printf("value of the int : %d\n", var);

printf("val of int point: %p\n", pointer);

printf("val which pointer is pointing at: %d\n", *pointer);

printf("add of pointer var : %p\n",&pointer);

Line 4: defines an integer var

5: defines an intger pointer – reserves space in memory

7: the content of the address of pointer is the address of variable

8: the val of var is 481

9: writes the address of var in hexadecimal notation for pointers %p

10: writes the val of var in decimal notation %d


To study the link between array and pointers

Int matr[2] represents matr[0] and matr [1]

Matr is a pointer to matr[0]

Matr +1 is a pointer to matr[1]

Macros in c++

In c, global constants or functions the Define statement

#define ONE 1

main()

typedef char n_name;

typedef unsigned int word;

typedef char *test;

typedef char field[50];

n_name mychar, anotherchar, *ptc1;

word myword;

test ptc2;

field name;

Typedef existing- type new_type_namr;

Where existing_type is a c++ fundamentals or compound type and


new_type_name is the namw of the new type
C marcos;

#define MIN(a,b) (((a)<(b))?(a): (b))

#define MAX(a,b) (((a)<(b))?(a): (b))

#define ABS(a) (((a)<0)?(a)(a))

#define EVEN(a) ((a)%2==0?1:0)

#define TOASCII(a) ((a)&OX7f)

Numerical differentiation and integration

Numerical differentiation

F_1 (x) = \frac{f(x+h)-f(x)}{h} +o(h)

F_2 (x) = \frac{ f(x) - f(x+h) }{h} +o(h)


Central difference:

F(x=x_0+-h) = f(x_0)+-hf’(x_0)+h^2f’’(x_0)/2 +- h^3f’’’(x_0)/6+o(h)

F(+h) = f_0 +- hf’ + h^2f’’/2+-h^3f’’’/6 +o(h^4)

F’3 = (fh- f-h)/2h – h^2f’’’/6 + o(h^3)

Second derivative:

Fh-2f_0=f-h = h^2f’’ + o(h^2)

F’’ = (fh-2f_0+f-h)/h^2 + o(h^2)

F+-2h = f_0 +-2hf’+2^2f’’+-4h^3f’’’/3+o(h^4)

Using (1) and (2), 8fh, 8f-h

==f_5c = \frac{f2h-8fh+fh-f2h}{12h} + o(^4)

Dominating error of o(h^4). Useful in rept 4^th order polynomial

Formulas for first derivatives

(f_h – f-h)/2h = f’’_0 + \sum{I to inf} \frac{f_0^(2j+1) h^2j}{2j+1!}

Frac{fh-2fo+fh}{h^2} = f’’_0 + 2\sum{1 to inf} \frac{f_0 ^(2j+2)}{2j+2} h^2j ~ o(h^2j)

A_h f_h + a_0f_0 + a_hf_h = [a_h +a_0 +a_h0]f_0 + [a_h –a_-h]hf_0 + [a_-h + a_h]
\fraac{h^2 f’’_0}{2} + \sum{3 t o inf } \frac{f_0 (j) h(j)}{j!} [(-1)(j) a_-h + a_h]

To determine f_0’ , a_-h + a_0 ++a_h = 0

-a_-h+a_h = 1/h

A_-h +a_h = 0
== a_-h = -a_h = -1/2h & a_0 = 0

Yielding \frac{f_n – f_h}{2h} = f_0’ + \sum{j = 1 to inf} \frac{f_0 (2j+1)}{2j+1 !} h^2j

To determine f_0’’ , we require in the last eqn that

C:

 Reading to a file
 Writing a file call by reference
 Call by value
 Dynamic (reference) memeory allocation

using namespace std;

#include<iostream>

#include<math.h>

// begin main function

void func(int, int*);

int main(int argc, char *argv[])

int a;

int *b;

a=10;
b=new int[10];

for(int i =0; i<10; i++)

b[i] = i;

func(a,b);

for(int i =0; i<10; i++)

printf("%p\n\n",b[i]);

return 0;

//end main

//definition of the function func

void func(int x, int *y)

x += 7;

*y += 10;

y[6] += 10;

return;

}//end function

 Line 5 and 6 : decleration of two variables a, b. the compiler reserves two


locations in memory. The size of location depends on the type of variable.
 Line 7: the value of a a is now 10
 Line 8: memory to solve 10 integers is reserved. The address to the location
sis stored in b. the address of element no: 6 is gvn by (b+6)
 Like 10: all the elements of b are given values b[0]=0; b 1 = 1; b 9= 9

Line 12: the main () function calls the function func() and progresses counter
transfers to the first statement in func()

Line 16: the variable x, y are local variable in func(). They have the value
x=10, y= address of the first element in b in the main () program

Line 18: the local variable x stored in the stack memory is changed to 17.
Nothing happens with the value a in main.

Line 19: the value of y is the address and the symbol *y stands for the
position in memory which has this address. The value is the location is now
increased by 10. This means that the value of b 10 in the main program () is
equal to 10. Thus func() has modified value in main ()

Line 20: this statement has the same effect as line 9 except that in modifies
element b 6 in main () by adding value of 10 to what was there originally.

Line 21: the program counter returns to main (), the next expression after
func(a, b); all data on the stack associated with func() are destroyed

The value of a is transferred to func() and stored in a new memory location called x. Any modification
of x in func() doesn't effect the value 'a' in main (). This is called transfer of data by.
The next argument in func() is an address which is transferred to func (). This address can be used to
modify the corresponding value in main (). In the programming language c it is expressed as a
modification of the value which y points to, namely the first element of b. This is called transfer of
data by reference & is a method to transfer data back to the calling function in this case main ().
Whereas in C++

Int n;
N=8;
Func(n);
.....
Void func(int & I)
{
I = 10; // n is changed
...
}
• copying large objects such as matrices & vectors to a function slows down execution, hence, it is
better to use call by reference.

In FORTRAN we use flags


INTEGER INTENT (IN) :: I ---- the local function cannot change the value I
INTEGER, INTENT (OUT) :: I allows the local function to change the variable i

Initializations and main program

Every program we have to define the functions employed

 Declare the function in beginning (OR)


 Include these functions and their statements before the main program
(i.e.) the main program appears at end
 In case of large projects include functions names at header

/* PROGRAM TO COMPUTE THE SECOND DERIVATIVE OF EXP(X)

THREE CALLING FUNCTION ARE INCLUDED IN THIS VERSION.

IN ONE FUNCTION WE NEED IN THE DATA FOR SCRREEN

WHILW THE LAST FUNCTION PRINTS OUT DATA TO SCREEN

*/

using namespace std;

#include<iostream>

#include<stdio.h>

#include<math.h>

#include<iomanip>

#include<fstream>

void initialize(double *, double *, int *);

void second_derivative (int, double, double, double *, double *);

void output(double *, double *, double, int);

int main()

//DECLERATION OF VARIABLES

int number_of_steps;

double x, initial_step;
double *h_step, *computed_derivative;

//read input data from screen

initialize (&initial_step,&x,&number_of_steps);

//allocate space in memory for the one dim array

// hstep and computed derivative

h_step=new double[number_of_steps];

//compute the second derivative of the exp(x)

second_derivative(number_of_steps, x, initial_step, h_step, computed_derivative);

//then we print the results to the file

output(h_step, computed_derivative, x, number_of_steps);

//free memory

delete[] h_step;

delete[] computed_derivative;

return 0;

Three functions

 Initialize
 Second_derivative
 Output
The arguments in void second derivative(int number_of_steps, double x,
double *h_step, double *computed derivative): indicate double *h_step,
double *computed_derivative; are pointers and that the address of the
first elements are passed.
 Dynamic allocation of memory through the new function
H_step = new double[number_of_steps];
Computed_derivative = new[number_of_steps];
 Freeing memory – delete[] h_step; delete[] computed_derivative;

The function initialize::

The function given below receives the addresses of 3 variables


//read in from screen the initial step, number of step and val x

void initialize (double *initial_step, double *x, int *number_of_steps)

printf("read in form screen initial step, x and number of steps\n");


scanf("%lf %lf %d",initial_step, x, number_of_steps);

return;

}//end of function initialize

The function second_derivative::

 The loop helps in calculating the second derivative


 Steps stored in array h_step, derivatives in computed_derivative
 //this function computes the second
 void second_derivative(int number_of_steps, double x, double initial_step, double *h_step, double
*computed_derivative)
 {
 int counter;
 double h;
 //calculate the step size
 //initialize the derivative and iteration counter
 h = initial_step;
 for(counter=0; counter<number_of_steps; counter++)
 {
 //setup array with derivatives and step sizes
 h_step[counter]=h;
 computed_derivative[counter] = (exp(x+h)-2.*exp(x)+exp(x-h))/(h*h);
 h = h*0.5;
 }
 return;
 }//end of second function derivative

The output function::


//function to write out the final results

void output(double *h_step, double *computed_derivative, double x, int number_of_steps)

int i;

ofile<<"RESULTS: "<<endl;

ofile<<setiosflags(ios::showpoint | ios::uppercase);

for(i=0; i<number_of_steps; i++)

ofile<<setw(15)<<setprecision(8)<<log10(h_step[i]);

ofile<<setw(15)<<setprecision(8)<<

log10(fabs(computed_derivative[i]-exp(x))/exp(x))<<endl;

}//end of function output

An alternative program for output is given by c++ program. It needs the input
and output (files) names through screen
#include<stdio.h>

#include<stdlib.h>

int col;

int main(int argc, char *argv[])

FILE *inn, *out;

int c;

if(argc<=3)

printf("argc = %d\n", argc);

printf("you have to read in :\n");

printf("in file and out file\n");

// exit(1);

inn=fopen(argv[1],"r"); //returns pointe to file

// inn=fopen("inn.xls","r");

if(inn==NULL)

printf("cant find the file %s\n", argv[1]);

// exit(1);

out=fopen(argv[2],"w");//returns a pointer to a output file

// out=fopen("out.xls","w");

if(out==NULL)//cant find the out file

printf("cant find the output file %s\n", argv[2]);

// exit(1);

fclose(inn);

fclose(out);

return 0;

}
The interesting features are

LINE PROGRAM COMMENTS


5 The function main() takes three arguments, given by argc. The
variable argv points to the following: the name of program, the
first and second arguments, in this case the file names to read
from screen
7 C++ has a type called FILE. The pointer inn and out point to
specific files. They must be type FILE
10 The command line has to contain 2 file names as parameters
13-17 The input file has to exit, else the pointer returns NULL. It has
only read permission
18-22 This applies for the output file as well, but now with write
permission only
23-24 Both files are closed before main program ends.

The following gives the second dderivative of exp(x), c++ options for readily
and writing files
/* PROGRAM TO COMPUTE THE SECOND DERIVATIVE OF EXP(X)

IN THIS VERSION WE USE C++ OPTIONS TO READING AND

WRITING FILES AND DATA. THE REST OF CODE IS AS IN

PROGRAM/CHAPTER3/PROGRAM1.CPP.

THREE CALLING FUNCTION ARE INCLUDED IN THIS VERSION.

IN ONE FUNCTION WE READ IN DATA FFROM SCREEN

THE NEXT FUNCTION COMPUTES THE SECOND DERIVATIVE

*/

/*e^x = \frac{f(h)-2f(0)+f(-h)}{h^2}}

= \frac{exp(h)-2exp(0)+fexp(-h)}{h^2}}

(e^x)''= \frac{exp(x+h)-2exp(x)+fexp(x-h)}{h^2}}

*/
using namespace std;

#include<iostream>

//#include<stdio>

#include<math.h>

#include<iomanip>

#include<fstream>

void initialize(double *, double *, int *);

void second_derivative (int, double, double, double *, double *);

void output(double *, double *, double, int);

ofstream ofile;

int main(int argc, char *argv[])

//DECLERATION OF VARIABLES

char *outfilename;

int number_of_steps;

double x, initial_step;

double *h_step, *computed_derivative;

//read in output file, abort if there are too few command line arguments

if(argc<=1)

cout<<"bad usage: "<<argv[0]<<"read also output file on same line"<<endl;

exit(1);

else

outfilename=argv[1];

ofile.open(outfilename);

//read input data from screen

initialize (&initial_step,&x,&number_of_steps);

//allocate space in memory for the one dim array

// hstep and computed derivative

h_step=new double[number_of_steps];
//compute the second derivative of the exp(x)

second_derivative(number_of_steps, x, initial_step, h_step, computed_derivative);

//then we print the results to the file

output(h_step, computed_derivative, x, number_of_steps);

//free memory

delete[] h_step;

delete[] computed_derivative;

//close output file

ofile.close();

return 0;

//read in from screen the initial step, number of step and val x

void initialize (double *initial_step, double *x, int *number_of_steps)

printf("read in form screen initial step, x and number of steps\n");

scanf("%lf %lf %d",initial_step, x, number_of_steps);

return;

}//end of function initialize

//this function computes the second

void second_derivative(int number_of_steps, double x, double initial_step, double *h_step, double *computed_derivative)

int counter;

double h;

//calculate the step size

//initialize the derivative and iteration counter

h = initial_step;

for(counter=0; counter<number_of_steps; counter++)

//setup array with derivatives and step sizes

h_step[counter]=h;

computed_derivative[counter] = (exp(x+h)-2.*exp(x)+exp(x-h))/(h*h);

h = h*0.5;
}

return;

}//end of second function derivative

//function to write out the final results

void output(double *h_step, double *computed_derivative, double x, int number_of_steps)

int i;

ofile<<"RESULTS: "<<endl;

ofile<<setiosflags(ios::showpoint | ios::uppercase);

for(i=0; i<number_of_steps; i++)

ofile<<setw(15)<<setprecision(8)<<log10(h_step[i]);

ofile<<setw(15)<<setprecision(8)<<log10(fabs(computed_derivative[i]-exp(x))/exp(x))<<endl;

return;

}//end of function output

#include<fstream> --- library function to be included

Ofstream file --- writing open and declare files

Ifstream file --- reading, opening files

#include<iomanip>

Setw(15) --- reserves an output of 15 spaces

Setprecision(8) --- 8 leading digits

Classes in c++
In fortran a vector start with 1, but it can be changed to start from zero or
negative numbers.

Precision decleration in fortran is

REAL(KIND=8)::VECTOR(-10,10)

REAL(KIND=8)::VECTOR(0:10)

IN C,

For(i=0;i<n;i++)

A[i] = b[i] + c[i];

C++ = define a new data types, tailorized to some particular problem

 Overloading a operator
 Set a data types.

Flow is it done using classes & templates

Functions and classes declerations are fundamental concepts with c++

Functions are abstractors which encapsulate an algorithm or parts of it &


perdorm specific tasks in a program

Classes are abstractios which encapsulate data and operators on these data.
They can be vary complex data structures and the class can contain particular
functions . which operate on these data,. Classes allow for a higher level of
abstractions in computing elements: classes data member and procedures are
classes are class members functions.

The ‘complex ‘ class

// program to calculate addition and


//multiplication of two complex numbers

using namespace std;

#include<iostream>

#include<cmath>

#include<complex>

int main()

complex<double> x(6.1,8.2), y(0.5,1.3);

cout<< x + y << x*y << endl;

return 0;

#ifdefine complex_H

#define complex_H

//various include statements and definitions

#include<iostream>//standard ANSI C++ include

#include<new>

#include ...

class complex

....

definition of variables and their character


};

//declerations of a various functions used by the class

....

#endif

Complex.h

class Complex

private:

double re, im;

public:

Complex();Complex c;

Complex(double re, double im = 0.0); //definition of a complex variable

Complex(const Complex& c); // equate two complex vatiables same as


previous

~ Complex(){ }//destructor
double Re() const; //double realpart = a.Re()

double Im()const; //double imagpart = a.im()

double abs() const; // double m=a.abs; //modulus

friend Complex operator+(const Complex& a, const Complex& b);

friend Complex operator-(const Complex& a, const Complex& b);

friend Complex operator*(const Complex& a, const Complex& b);

friend Complex operator/(const Complex& a, const Complex& b);

};

#include "Complex.h"

//... other include and declerations

int main()

Complex a(1.0,1.3); // we declare a complex variable

Complex b(3.0), c(5.0, -2.3); //we declare complex variable base

Complex d=b;

cout<<"d="<<d<<",a=" <<a<< , ",b=" <<b<< endl;

d=a*c+b/a; //we add , mul, div two complex num

cout<<"Re(d)="<<d.Re()<<",Im(d)="<<d.Im()<<endl; //write the real and imag


parts

Вам также может понравиться