ch 5 - Math manipulations

advertisement
Software Design 2006/7
Chapter 5 - Math Calculations
Handout (July 2006)
Chapter 5 Mathematical Calculations
5.1 Value types
5.1.1 definition of value type
Integer
Floating: floating point number
Floating, double F & Long double F
Double floating
Integer
No of Bites
No. of Bits*
No. of significant
digits
Range
Note
Floating
2
16
-32,768 to + 32,768
4
32
8
64
7
15
-3.4E+38 to +3.4E+38
-1.7E+308 to +1.7E+308
An int beyond this range cannot be
expressed in int but in float
* That is why we need to tell the computer the types of the data by declaration of the type of a variable
in programs.
5.1.2 converting between value types
a. converting by declaration
int i;
float (i)
Note: i is still int while whole part float (i) is float.
b. converting by auto promotion
when an int operand meets a float operand, the compiler will use a pre-defined promotion from integer
to float for the int operand. For example, the expression 1.1 + 2 would be 3.1 but not 3.
5.2 Calculation
Addition
Subtraction
Multiplication
Division
Remainder
Signs
+
*
/
%
Example
a+b
a-b
a*b
a/b
a%b
Note
Quotient is truncated.
a, b, and a%b should be int
5.3 Operator precedence
1. operators * and / have higher precedence than + and -.
2. if two operators have equal precedence, then they obey a left-to-right rule.
For example, 3/4 * 2 means (3/4) *2.
25
Software Design 2006/7
Chapter 5 - Math Calculations
Handout (July 2006)
5.4 Standard mathematical functions
In C++, common math functions are provided in one of libraries – the math library. To use one of the
functions, the include statement must be included at the head of the program:
#include <math.h>
Expressions in C++
cos (x)
sin (x)
tan (x)
fabs(x)
log (x)
log10(x)
sqrt(x)
pow(x,y)
Math meanings
cos (x)
sin (x)
tan (x)
|x|
lnx
log10x
x1/2
xy
Example
Note
in radian
in radian
in radian
fabs(-3)=3
5.5 Worked examples
5.5.1. Conversion of an angle in degree into one in radian
The trigonometric functions are manipulated in radian instead of degree in programs in C++. However
most times the angles encountered are expressed in degree. So the conversion are required before
manipulation. The relationship between degree and radian is given,
x2 (angle in radian) = (3.14 / 180 ) * x1 (angle in degree).
(math02.cpp)
#include <iostream.h>
#include <math.h>
main ()
{
float x1, x2;
cout<<”x1(in degree)=”;
cin>>x1;
x2=3.14/180*x1;
cout<<”\n x2 (in radian)=”<<x2<<endl;
}
5.5.2. Evaluation of the roots of a quadratic
A quadratic ,
ax2 + bx + c = 0
the formula of the roots x1,2 is given,
x1, 2
 b  b 2  4ac

2a
the expression b2 – 4ac is called the discriminant. If b2 – 4ac < 0, then √b2 – 4ac is negative, or an
imaginary value. The values of x1,2 containing the imaginary parts are called the imaginary roots. The
imaginary roots are normally discriminated in primary algebra. Let us write a program fragment to test
whether the quadratic roots are imaginary. If yes, discriminate them; if not, calculate the roots.
26
Software Design 2006/7
Chapter 5 - Math Calculations
Handout (July 2006)
(math01.cpp)
#include <iostream.h>
#include <math.h>
main ()
{
float a, b=12.1, c=3.5, d, x1, x2;
d=b*b - 4.0 * a * c;
cin >> a;
if (d < 0)
cout << "imaginary roots to be discriminated" << endl;
else
{
x1 = (- b + sqrt(d)) / (2.0 * a);
x2 = (- b - sqrt(d)) / (2.0 * a);
cout << "x1 = " << x1 << endl;
cout << "x2 = " << x2 << endl;
}
}
Results displayed
Result 1
a = 10.0
x1 = -0.47841
x2 = -0.73159
Result 2
a =20.0
imaginary roots to be discriminate.
5.6. Exercise
Q1. Write a program that inputs 120.5°on keyboard and converts the angle in degree into one in
radian.
Q2. Write a program that inputs 33.5°on keyboard and calculates the values of sin 33.5°and cos
33.5°.
Q3. Write a program that inputs 133.3°on keyboard and calculates the values of tan 133.3°.
27
Download