Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSC 107 - Programming for Science Lecture 7: Math Functions.

Similar presentations


Presentation on theme: "CSC 107 - Programming for Science Lecture 7: Math Functions."— Presentation transcript:

1 CSC 107 - Programming for Science Lecture 7: Math Functions

2 Today’s Goal At the end of today’s lecture, you should have a better understanding of functions  These are important in real programs  Includes way to convert decimals to integers

3 Functions C includes many pre-defined functions  Function just means a routine that does some useful work  #include statement allow us to use functions Differs from what function means in math  printf & scanf do not return values  Functions discussed today do, however

4 Mathematical Functions To use, at start of program must add #include All of these functions will return a value  Will not change value of any argument  Can assign result to a variable  Can use result in an equation  Can ignore result, but why call function?

5 Using These Functions double fabs(double x)  Returns absolute value of a number double d1 = fabs(-1); double d2 = fabs(-56.54); double d3 = fabs(d1); double d4 = fabs(d1 + d2 * d3); double d5 = d4 * fabs(d2); d2 = 46 * fabs(d1); d5 = fabs(fabs(d3));

6 Other Functions Some other good, useful functions that return data of type double  sqrt(x)returns square root of x  pow(x,y)returns x y  exp(x)returns e x  log(x)returns ln x; natural logarithm of x  log10(x)returns log 10 x double includes value “NaN” that returns when result is undefined, as in sqrt(-1)

7 Trigonometric Functions Angle measurements are in radians -- program must do any conversions  sin(x)returns sine of x  cos(x)returns cosine of x  tan(x)returns tangent of x  asin(x)return arcsine of x; x must be in the range [-1, 1] & result is in the range [-  /2,  /2]  acos(x)return arccosine of x; x must be in the range [-1, 1] & result is in the range [-  /2,  /2]  atan(x)return arctangent of x; result is in the range [-  /2,  /2]

8 Rounding Functions Additional functions allow us to convert decimal numbers into integers floor(x) returns x rounded to nearest integer toward -  (negative infinity)  floor(2.01) returns 2  floor(78.999999) returns 78  floor(-0.0001) returns -1  floor(-65.561) returns -66

9 Rounding Functions Additional functions allow us to convert decimal numbers into integers ceil(x) returns x rounded to nearest integer toward  (positive infinity)  ceil(2.01) returns 3  ceil(78.999999) returns 79  ceil(-0.0001) returns 0  ceil(-65.561) returns -65

10 Normal Rounding C does not define function to perform typical rounding operation  floor(x + 0.5) works with numbers > - 0.5  ceil(x + 0.5) works with numbers < -0.5 We will revisit this problem later…

11 Your Turn Get into groups and complete daily activity

12 For Next Lecture Read through Section 2.6 of book  Do not need to understand all the details  But important knowing what is not understood Start homework assignment for week 3  Covers material from this week’s lectures


Download ppt "CSC 107 - Programming for Science Lecture 7: Math Functions."

Similar presentations


Ads by Google