ICS 103: Computer Programming in C
Handout-05
Topic: Simple
Standard Mathematical Library Functions
Instructor:
M. Waheed Aslam.
Objective:
·
To know about standard Mathematical library Functions and
their use with examples.
Used to implement additional operators
·
We learned how
to form arithmetic expressions in C using some unary and binary operations
·
However, with
these operators alone we cannot write the full range of expressions that we can
do in mathematics.
·
For
example, we do not know yet
how to represent expressions like |a+b|, , in C language
·
To write such
mathematical formulas and other mathematical functions like sin, cos, tan, square root, square, power, absolute value, etc., in C language, the library header
file called math.h should be included in the beginning of the program along with the stdio.h file as follows:
#include <stdio.h>
#include <math.h>
·
For sin, cos,
tan the degrees should be converted into radians. To convert degrees to radians multiply the
degrees with (22/7)/180 or (3.14/180) where 3.14 is the value of PI.
For example:
sin300
is written as sin(30*3.14/180), a5 is written as pow(a, 5).
·
To write the
expression x = sin450 + cos900
+ log106 + b4 in C language, we write:
x = sin(45*3.14/180) +
cos(90*3.14/180) + log10(6) + pow(b,4)
The following Example Shows explanation and use of some important
standard Math library Functions:
#include<stdio.h> // input
output standard functions are defined in this
#include<math.h> // all standard
math functions are defined
#include<stdlib.h> // here used only
for rand() function
void main()
{int ab,random; // variable
declarations
double
cel,cosine,floatabs,flo,logbase_e,logbase_10,power,expo,sq_root;
ab=abs(-8); // gives absolute
value of integer argument
cel=ceil(45.0001); // gives ceil
value of double argument
cosine=cos(30*3.14159/180); //angle must be
converted into radians first
expo=exp(1.0); // gives e to
the power argument value
floatabs=fabs(-8.432); // gives absolute value of float argument
flo=floor(45.99356); // it truncates
decimal digits
logbase_e=log(2.71828); //calculates natural log of argument
(i.e base e)
logbase_10=log10(100); // calculates log of argument at base 10
power=pow(0.16,0.5); // calculates power i.e., pow(base, power) format
random=rand(); // generates random integer between 0 and
the value
// associated with RAND_MAX, a constant macro defined in
<stdlib.h>
sq_root=sqrt(2.25); // calculates square root of argument
printf("The absolute
value of -8 is : %d\n", ab);
printf("The ceil
value of 45.0001 is : %lf\n", cel);
printf("The cos value
of degree 30 is : %lf\n", cosine);
printf("The
exponential value of 1 is : %lf\n",
expo);
printf("The absolute
value of double type argument -8.432 is: %lf\n", floatabs);
printf("The
floor value of 45.99356 is: %lf\n", flo);
printf("The natural
log value of 2.71829 is:
%lf\n", logbase_e);
printf("The log base
10 for value 100 is : %lf\n", logbase_10);
printf("The value of pow(0.16,0.5) is : %lf\n", power);
printf("A random
integer between 0 and RAND_MAX is :
%d\n", random);
printf("RAND_MAX
= : %d\n", RAND_MAX);
printf("The value of sqrt(2.25) is : %lf\n", sq_root);
} // end of main
Output: Left as an exercise
Homework
Determine the
value of the following C expression:
a) - (2 * (-3/(double) (4%10))) – (-6+4)
b) ceil(-7.2) * pow (4.0,2.0)