math library functions 02/12/15. objective use functions from the math.h library to do special...

13
Math Library Functions 02/12/15

Upload: johnathan-allen

Post on 13-Dec-2015

213 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Math Library Functions 02/12/15. Objective Use functions from the math.h library to do special calculations

Math Library Functions

02/12/15

Page 2: Math Library Functions 02/12/15. Objective Use functions from the math.h library to do special calculations

Objective

• Use functions from the math.h library to do special calculations.

Page 3: 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.

Page 4: Math Library Functions 02/12/15. Objective Use functions from the math.h library to do special calculations

Algorithm

1. Input a

2.

3. Output s

s=√a

Page 5: Math Library Functions 02/12/15. Objective Use functions from the math.h library to do special calculations

Program

bandana.c

Page 6: Math Library Functions 02/12/15. Objective Use functions from the math.h library to do special calculations

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

Page 7: Math Library Functions 02/12/15. Objective Use functions from the math.h library to do special calculations

Some Functions in cmath

sqrt(x)

arg - doubleresult - double

pow(x,y)

arg - doubleresult - double

fabs(x)

arg - doubleresult - double

√ x

x y

( x )

Page 8: Math Library Functions 02/12/15. Objective Use functions from the math.h library to do special calculations

Some Functions in cmath

ceil(x) Rounds up arg - doubleresult - double

floor(x) Rounds down arg - doubleresult - double

Page 9: Math Library Functions 02/12/15. Objective Use functions from the math.h library to do special calculations

Absolute Value of Integers

abs( ) arg - intresult - int

|x|

Page 10: Math Library Functions 02/12/15. Objective Use functions from the math.h library to do special calculations

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

Page 11: Math Library Functions 02/12/15. Objective Use functions from the math.h library to do special calculations

Example

• Write a program to find the 4th root of a number input by the user.

• fourthRoot.c

Page 12: Math Library Functions 02/12/15. Objective Use functions from the math.h library to do special calculations

Find Age Difference

Write a program to find the difference in age between two quarterbacks on a team.

Page 13: Math Library Functions 02/12/15. Objective Use functions from the math.h library to do special calculations

Algorithm

1. Input qb1 and qb2

2. difference = |qb1 – qb2|

3. Output difference

Write the program today in lab. Started in qb.c