Академический Документы
Профессиональный Документы
Культура Документы
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
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
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
READ(*,*) r
s = SIN (r)
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
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!
Fortran
REAL(KIND =8)
FORTRAN
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
LOGICAL OPERATORS
BITWISE OPERATOR
#include<stdio.h>
#include<iostream>
main()
int var;
int *pointer;
pointer = &var;
var = 421;
Macros in c++
#define ONE 1
main()
word myword;
test ptc2;
field name;
Numerical differentiation
Second derivative:
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]
-a_-h+a_h = 1/h
A_-h +a_h = 0
== a_-h = -a_h = -1/2h & a_0 = 0
C:
Reading to a file
Writing a file call by reference
Call by value
Dynamic (reference) memeory allocation
#include<iostream>
#include<math.h>
int a;
int *b;
a=10;
b=new int[10];
b[i] = i;
func(a,b);
printf("%p\n\n",b[i]);
return 0;
//end main
x += 7;
*y += 10;
y[6] += 10;
return;
}//end function
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.
*/
#include<iostream>
#include<stdio.h>
#include<math.h>
#include<iomanip>
#include<fstream>
int main()
//DECLERATION OF VARIABLES
int number_of_steps;
double x, initial_step;
double *h_step, *computed_derivative;
initialize (&initial_step,&x,&number_of_steps);
h_step=new double[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;
return;
int i;
ofile<<"RESULTS: "<<endl;
ofile<<setiosflags(ios::showpoint | ios::uppercase);
ofile<<setw(15)<<setprecision(8)<<log10(h_step[i]);
ofile<<setw(15)<<setprecision(8)<<
log10(fabs(computed_derivative[i]-exp(x))/exp(x))<<endl;
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 c;
if(argc<=3)
// exit(1);
// inn=fopen("inn.xls","r");
if(inn==NULL)
// exit(1);
// out=fopen("out.xls","w");
// exit(1);
fclose(inn);
fclose(out);
return 0;
}
The interesting features are
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)
PROGRAM/CHAPTER3/PROGRAM1.CPP.
*/
/*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>
ofstream ofile;
//DECLERATION OF VARIABLES
char *outfilename;
int number_of_steps;
double x, initial_step;
//read in output file, abort if there are too few command line arguments
if(argc<=1)
exit(1);
else
outfilename=argv[1];
ofile.open(outfilename);
initialize (&initial_step,&x,&number_of_steps);
h_step=new double[number_of_steps];
//compute the second derivative of the exp(x)
//free memory
delete[] h_step;
delete[] computed_derivative;
ofile.close();
return 0;
//read in from screen the initial step, number of step and val x
return;
void second_derivative(int number_of_steps, double x, double initial_step, double *h_step, double *computed_derivative)
int counter;
double h;
h = initial_step;
h_step[counter]=h;
computed_derivative[counter] = (exp(x+h)-2.*exp(x)+exp(x-h))/(h*h);
h = h*0.5;
}
return;
int i;
ofile<<"RESULTS: "<<endl;
ofile<<setiosflags(ios::showpoint | ios::uppercase);
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;
#include<iomanip>
Classes in c++
In fortran a vector start with 1, but it can be changed to start from zero or
negative numbers.
REAL(KIND=8)::VECTOR(-10,10)
REAL(KIND=8)::VECTOR(0:10)
IN C,
For(i=0;i<n;i++)
Overloading a operator
Set a data types.
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.
#include<iostream>
#include<cmath>
#include<complex>
int main()
return 0;
#ifdefine complex_H
#define complex_H
#include<new>
#include ...
class complex
....
....
#endif
Complex.h
class Complex
private:
public:
Complex();Complex c;
~ Complex(){ }//destructor
double Re() const; //double realpart = a.Re()
};
#include "Complex.h"
int main()
Complex d=b;