Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 TAC2000/2000.7 Protocol Engineering and Application Research Laboratory (PEARL) MATH Functions in C Language.

Similar presentations


Presentation on theme: "1 TAC2000/2000.7 Protocol Engineering and Application Research Laboratory (PEARL) MATH Functions in C Language."— Presentation transcript:

1 1 TAC2000/2000.7 Protocol Engineering and Application Research Laboratory (PEARL) MATH Functions in C Language

2 2 TAC2000/2000.7 Protocol Engineering and Application Research Laboratory (PEARL) Definitions of Functions in Mathematics “A relationship between two variables, typically x and y, is called a function, if there is a rule that assigns to each value of x one and only one value of y.” http://www.themathpage.com/aPreCalc/functions.htm So, for example, if we have a function f(x) = x + 1 then we know that f(-2.5)= -2.5+1= -1.5 f(-2)= -2+1= f(-1)=+1= 0 f(0)= 0+1=+1 f(+1)=+1+1=+2 f(+2)=+2+1=+3 f(+2.5)=+2.5+1=+3.5

3 3 TAC2000/2000.7 Protocol Engineering and Application Research Laboratory (PEARL) Absolute Function  Likewise, if we have an absolute function a(y) = | y | then we know that a(-2.5)=|-2.5|=2.5 a(-2)=|-2|=2 a(-1)=||=1 a(0)=|0|= 0 a(+1)=|+1|=1 a(+2)=|+2|=2 a(+2.5)=|+2.5|=2.5

4 4 TAC2000/2000.7 Protocol Engineering and Application Research Laboratory (PEARL) Math Functions in C Standard Library Function Name Math Name ValueExample abs(x) absolute value |x||x| abs(-1) returns1 fabs(x) absolute value |x||x| fabs(-3.2) returns3.2 pow(x,y) raise to the power xyxy pow(2.0, 3.0) returns8.0 sqrt(x) square rootx 0.5 sqrt(2.0) returns1.414… exp(x) exponentialexex exp(1.0) returns2.718… log(x) natural logarithmln x log(2.718…) returns1.0 log10(x) common logarithmlog x log10(100.0) returns2.0 sin(x) sinesin x sin(3.14…) returns0.0 cos(x) cosinecos x cos(3.14…) returns tan(x) tangenttan x tan(3.14…) returns0.0 ceil(x) ceiling ┌ x ┐ ceil(2.5) returns3.0 floor(x) floor └ x ┘└ x ┘ floor(2.5) returns2.0

5 5 TAC2000/2000.7 Protocol Engineering and Application Research Laboratory (PEARL) Parameter Type  C language has concepts that are analogous to the mathematical domain and range: parameter type and return type.  For a given function in C, the parameter type – which corresponds to the domain in mathematics – is the data type that C expects for a parameter of that function. For example:  the argument type of abs is int ;  the argument type of fabs is float.  Q: What would be the value of abs(-3.5) ?

6 6 TAC2000/2000.7 Protocol Engineering and Application Research Laboratory (PEARL) #include  To utilize these mathematical functions, you must include the header files at the beginning of your program. #include #include using std::cout; using std::endl; int main() { cout << fabs(-3.2) << endl; cout << fabs(-3.2) << endl; cout << pow(2.0, 3.0) << endl; cout << pow(2.0, 3.0) << endl; cout << sqrt(2.0) << endl; cout << sqrt(2.0) << endl; cout << exp(1.0) << endl; cout << exp(1.0) << endl; cout << tan(3.1415926/4) << endl; cout << tan(3.1415926/4) << endl; cout << round(4.5) << endl; cout << round(4.5) << endl; return 0; return 0;} 3.2 8 1.41421 2.71828 1 5

7 7 TAC2000/2000.7 Protocol Engineering and Application Research Laboratory (PEARL) C Math Functions  C numerics library cmath declares a set of functions to compute common mathematical operations and transformations cmath declares a set of functions to compute common mathematical operations and transformations #include before you invoke the following functions. #include before you invoke the following functions.

8 8 TAC2000/2000.7 Protocol Engineering and Application Research Laboratory (PEARL) Trigonometric functions  cos - Compute cosine cos  sin - Compute sine sin  tan - Compute tangent tan  acos - Compute arc cosine acos  asin - Compute arc sine asin  atan - Compute arc tangent atan  atan2 - Compute arc tangent with two parameters atan2  Under a Unix shell prompt, you may type “ man atan2 ” to see the synopsis of the function atan2().

9 9 TAC2000/2000.7 Protocol Engineering and Application Research Laboratory (PEARL) Hyperbolic functions  cosh - Compute hyperbolic cosine cosh  sinh - Compute hyperbolic sine sinh  tanh - Compute hyperbolic tangent tanh

10 10 TAC2000/2000.7 Protocol Engineering and Application Research Laboratory (PEARL) Exponential and logarithmic functions:  exp - Compute exponential function exp  frexp - Get significand and exponent frexp  ldexp - Generate number from significand and exponent ldexp  log - Compute natural logarithm log  log10 - Compute common logarithm log10  modf - Break into fractional and integral parts modf

11 11 TAC2000/2000.7 Protocol Engineering and Application Research Laboratory (PEARL) Power functions  sqrt - Compute square root sqrt  pow - Raise to power pow pow(64.0, 1.0/3.0) pow(64.0, 1.0/3.0)

12 12 TAC2000/2000.7 Protocol Engineering and Application Research Laboratory (PEARL) Rounding, absolute value and remainder functions:  ceil - Round up value ceil  fabs - Compute absolute value fabs  floor - Round down value floor  fmod - Compute remainder of division fmod

13 13 TAC2000/2000.7 Protocol Engineering and Application Research Laboratory (PEARL) Exercise: Draw a sine function  Test your draw() function with the following main program. int main() { const float PI = 3.1415926535f; const float PI = 3.1415926535f; float x, y; float x, y; for (int d=0; d<=360; d+=10) for (int d=0; d<=360; d+=10) { x = d * PI/180; x = d * PI/180; y = sin(x); y = sin(x); draw(y); draw(y); } return 0; return 0;} 13

14 14 TAC2000/2000.7 Protocol Engineering and Application Research Laboratory (PEARL) sin(x) #include #include using std::cout; using std::endl; int main() { int degree; int degree; const double pi = 3.1415926535; const double pi = 3.1415926535; double x; double x; double y; double y; for (degree=0; degree<=360; degree+=15) for (degree=0; degree<=360; degree+=15) { x = degree * pi / 180; x = degree * pi / 180; y = sin(x); y = sin(x); cout << degree << '\t' << x << '\t' << y << endl; cout << degree << '\t' << x << '\t' << y << endl; } return 0; return 0;}

15 15 TAC2000/2000.7 Protocol Engineering and Application Research Laboratory (PEARL) setprecision(n) #include #include using std::cout; using std::endl; int main() { int degree; int degree; double x; double x; double y; double y; cout << std::fixed; cout << std::fixed; for (degree=0; degree<=360; degree+=15) for (degree=0; degree<=360; degree+=15) { x = degree * M_PI / 180; x = degree * M_PI / 180; y = sin(x); y = sin(x); cout << std::setw(3) << degree << '\t' cout << std::setw(3) << degree << '\t' << std::setprecision(5) << x << '\t' << std::setprecision(5) << x << '\t' << std::setprecision(5) << std::showpos << y << std::setprecision(5) << std::showpos << y << std::noshowpos << endl; << std::noshowpos << endl; } return 0; return 0;} /usr/include/math.h

16 16 TAC2000/2000.7 Protocol Engineering and Application Research Laboratory (PEARL) Plot sin(x) using characters int main() { int i; int i; int degree; int degree; const double pi = 3.1415926535; const double pi = 3.1415926535; double x; double x; double y; double y; cout << std::fixed; cout << std::fixed; for (degree=0; degree<=360; degree+=15) for (degree=0; degree<=360; degree+=15) { x = degree * pi / 180; x = degree * pi / 180; y = sin(x); y = sin(x); cout << std::setw(3) << degree << '\t' cout << std::setw(3) << degree << '\t' << std::setprecision(5) << x << '\t' << std::setprecision(5) << x << '\t' << std::setprecision(5) << std::showpos << y << std::setprecision(5) << std::showpos << y << std::noshowpos; << std::noshowpos; for (i=0; i< 20 * y + 22; i++) for (i=0; i< 20 * y + 22; i++) cout << ' '; cout << ' '; cout << '*' << endl; cout << '*' << endl; } return 0; return 0;}

17 17 TAC2000/2000.7 Protocol Engineering and Application Research Laboratory (PEARL) Exercise: Plot cos(x) using characters Amplify and Shift

18 18 TAC2000/2000.7 Protocol Engineering and Application Research Laboratory (PEARL) Exercise  Prime Numbers with sqrt() Prime Numbers with sqrt() Prime Numbers with sqrt()

19 19 TAC2000/2000.7 Protocol Engineering and Application Research Laboratory (PEARL) time( ) #include #include using std::cout; int main() { unsigned int i, t1, t2; unsigned int i, t1, t2; t1 = time(NULL); t1 = time(NULL); cout << t1 << '\n'; cout << t1 << '\n'; cout << "We want to measure how long it takes " cout << "We want to measure how long it takes " << "for this program to finish.\n"; << "for this program to finish.\n"; cout << "Wait for a few seconds, and then\n" cout << "Wait for a few seconds, and then\n" << "input a number and press ENTER -- "; << "input a number and press ENTER -- "; std::cin >> i; std::cin >> i; t2 = time(NULL); t2 = time(NULL); cout << "It takes " << t2 - t1 << " seconds.\n"; cout << "It takes " << t2 - t1 << " seconds.\n"; return 0; return 0;} The time() function returns the value of time in seconds since 0 hours, 0 minutes, 0 seconds, January 1, 1970, Greenwich Mean Time. For March 2014, the value would be something like 1393766766.

20 20 TAC2000/2000.7 Protocol Engineering and Application Research Laboratory (PEARL) 20 Random Number Generator  rand() The rand function returns a pseudorandom integer in the range 0 to RAND_MAX (32767) The rand function returns a pseudorandom integer in the range 0 to RAND_MAX (32767)rand // Print 5 random numbers. for (int i = 0; i < 5; i++ ) cout << rand() << endl; cout << rand() << endl;

21 21 TAC2000/2000.7 Protocol Engineering and Application Research Laboratory (PEARL) 21 Seed of rand()  With the same seed, the program will get the same result at each execution. #include #include using std::cout; using std::cin; int main() { int n; int n; cout << "Please input a number as the seed -- "; cout << "Please input a number as the seed -- "; cin >> n; cin >> n; srand(n); srand(n); for (int i = 0; i < 5; i++ ) for (int i = 0; i < 5; i++ ) cout << rand() << '\n'; cout << rand() << '\n'; return 0; return 0;} 種瓜得瓜,種豆得豆

22 22 TAC2000/2000.7 Protocol Engineering and Application Research Laboratory (PEARL) 22 Using time() as the Seed  Use srand() and choose the current time as the seed.  This guarantees that, at each execution, the program will automatically get different seed. #include #include srand((unsigned) time(NULL)); for (int i = 0; i < 5; i++ ) cout << rand() << endl; cout << rand() << endl;

23 23 TAC2000/2000.7 Protocol Engineering and Application Research Laboratory (PEARL) 23 Example: Coin Tossing  A coin has two sides – Head/Tail 0/1 0/1  Repeat tossing the coin 20 times  Count the occurrences of Head and Tail, respectively.

24 24 TAC2000/2000.7 Protocol Engineering and Application Research Laboratory (PEARL) Sample Code (coin_tossing.cpp) int main() { int head = 0; int head = 0; int tail = 0; int tail = 0; int i; int i; // srand((unsigned) time(NULL)); // srand((unsigned) time(NULL)); for (int k = 0; k < 20; k++ ) for (int k = 0; k < 20; k++ ) { i = rand() % 2; // coin tossing i = rand() % 2; // coin tossing if (i == 0) // 0 for Head; 1 for Tail if (i == 0) // 0 for Head; 1 for Tail { ++head; ++head; cout << "Head" << endl; cout << "Head" << endl; } else else { ++tail; ++tail; cout << "Tail" << endl; cout << "Tail" << endl; } } cout << "There are totally " << head << " Heads and " cout << "There are totally " << head << " Heads and " << tail << " Tails.\n"; << tail << " Tails.\n"; return 0; return 0;} 24

25 25 TAC2000/2000.7 Protocol Engineering and Application Research Laboratory (PEARL) Exercise: Multiplication 62 * 99 = 6138 97 * 8 = 776 79 * 37 = 2923 40 * 53 = 2120 30 * 87 = 2610 13 * 46 = 598 77 * 81 = 7237 Wrong! 77*81=6237 33 * 54 = 1782 87 * 36 = 4132 Wrong! 87*36=3132 39 * 6 = 234 You made 2 mistakes today.  Don’t forget to use conditional operator (P.103) to handle “You made 2 mistake(s) today.” 25 START count0 for i=0; i<10; i++ Generate two random number a,b[0,99] a,b c c==a*b count++“Incorrect!” End


Download ppt "1 TAC2000/2000.7 Protocol Engineering and Application Research Laboratory (PEARL) MATH Functions in C Language."

Similar presentations


Ads by Google