math library functions 02/12/15. objective use functions from the math.h library to do special...
TRANSCRIPT
Math Library Functions
02/12/15
Objective
• Use functions from the math.h library to do special calculations.
Side of Bandana Problem
I am manufacturing bandanas. Given the amount of cloth I can afford for each bandana, I want to know the measure of the side of that square. Write a program to find out the measure of the side of the square bandana.
Algorithm
1. Input a
2.
3. Output s
s=√a
Program
bandana.c
Built-in Functions• Call Syntax:
fname(arg1, arg2,… argn)• Example:
s = sqrt(area);• Include #include <math.h>• Must use -lm option when compiling
gcc -lm prog.c
Some Functions in cmath
sqrt(x)
arg - doubleresult - double
pow(x,y)
arg - doubleresult - double
fabs(x)
arg - doubleresult - double
√ x
x y
( x )
Some Functions in cmath
ceil(x) Rounds up arg - doubleresult - double
floor(x) Rounds down arg - doubleresult - double
Absolute Value of Integers
abs( ) arg - intresult - int
|x|
More Functions in cmath
log(x) Natural log of x arg - doubleresult - double
exp(x) e to the power x arg - doubleresult - double
•Examples
log(88.5) --> 4.483
exp(4.483) --> 88.5
Example
• Write a program to find the 4th root of a number input by the user.
• fourthRoot.c
Find Age Difference
Write a program to find the difference in age between two quarterbacks on a team.
Algorithm
1. Input qb1 and qb2
2. difference = |qb1 – qb2|
3. Output difference
Write the program today in lab. Started in qb.c