1 / 25

MATH Functions in C Language

MATH Functions in C Language. 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

gates
Download Presentation

MATH Functions in C Language

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. MATH Functions in C Language

  2. 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

  3. Absolute Function • Likewise, if we have an absolute functiona(y) = | y |then we know that

  4. Math Functions in C Standard Library

  5. Parameter Type • C language has concepts that are analogous to the mathematical domain and range: parameter typeand 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. #include <cmath> • To utilize these mathematical functions, you must include the header files at the beginning of your program. #include <iostream> #include <cmath> using std::cout; using std::endl; int main() { cout << fabs(-3.2) << endl; cout << pow(2.0, 3.0) << endl; cout << sqrt(2.0) << endl; cout << exp(1.0) << endl; cout << tan(3.1415926/4) << endl; cout << round(4.5) << endl; return 0; } 3.2 8 1.41421 2.71828 1 5

  7. C Math Functions • C numerics library • cmath declares a set of functions to compute common mathematical operations and transformations • #include <cmath> before you invoke the following functions.

  8. Trigonometric functions • cos - Compute cosine • sin - Compute sine • tan - Compute tangent • acos - Compute arc cosine • asin - Compute arc sine • atan - Compute arc tangent • atan2 - Compute arc tangent with two parameters

  9. Hyperbolic functions • cosh - Compute hyperbolic cosine • sinh - Compute hyperbolic sine • tanh - Compute hyperbolic tangent

  10. Exponential and logarithmic functions: • exp - Compute exponential function • frexp - Get significand and exponent • ldexp - Generate number from significand and exponent • log - Compute natural logarithm • log10 - Compute common logarithm • modf - Break into fractional and integral parts

  11. Power functions • sqrt - Compute square root • pow - Raise to power • pow(64.0, 1.0/3.0)

  12. Rounding, absolute value and remainder functions: • ceil - Round up value • fabs - Compute absolute value • floor - Round down value • fmod - Compute remainder of division

  13. Exercise: Draw a sine function • Test your draw() function with the following main program. int main() { const float PI = 3.1415926535f; float x, y; for (int d=0; d<=360; d+=10) { x = d * PI/180; y = sin(x); draw(y); } return 0; }

  14. Exercise • Prime Numbers with sqrt()

  15. sin(x) #include <iostream> #include <cmath> using std::cout; using std::endl; int main() { int degree; const double pi = 3.1415926535; double x; double y; for (degree=0; degree<360; degree+=15) { x = degree * pi / 180; y = sin(x); cout << degree << '\t' << x << '\t' << y << endl; } return 0; }

  16. setprecision(n) #include <iostream> #include <iomanip> #include <cmath> using std::cout; using std::endl; int main() { int degree; double x; double y; cout << std::fixed; for (degree=0; degree<=360; degree+=15) { x = degree * M_PI / 180; y = sin(x); cout << std::setw(3) << degree << '\t' << std::setprecision(5) << x << '\t' << std::setprecision(5) << std::showpos << y << std::noshowpos << endl; } return 0; }

  17. Plot sin(x) using characters int main() { int i; int degree; const double pi = 3.1415926535; double x; double y; cout << std::fixed; for (degree=0; degree<=360; degree+=15) { x = degree * pi / 180; y = sin(x); cout << std::setw(3) << degree << '\t' << std::setprecision(5) << x << '\t' << std::setprecision(5) << std::showpos << y << std::noshowpos; for (i=0; i< 20 * y + 22; i++) cout << ' '; cout << '*' << endl; } return 0; }

  18. Exercise: Plot cos(x) using characters Amplify and Shift

  19. time( ) #include <iostream> #include <ctime> using std::cout; int main() { unsigned int i, t1, t2; t1 = time(NULL); cout << t1 << '\n'; cout << "We want to measure how long it takes " << "for this program to finish.\n"; cout << "Wait for a few seconds, and then\n" << "input a number and press ENTER -- "; std::cin >> i; t2 = time(NULL); cout << "It takes " << t2 - t1 << " seconds.\n"; 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. Random Number Generator • rand() • The rand function returns a pseudorandom integer in the range 0 to RAND_MAX (32767) // Print 5 random numbers. for (int i = 0; i < 5; i++ ) cout << rand() << endl;

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

  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 <ctime> srand((unsigned) time(NULL)); for (int i = 0; i < 5; i++ ) cout << rand() << endl;

  23. Example: Coin Tossing • A coin has two sides – Head/Tail • 0/1 • Repeat tossing the coin 20 times • Count the occurrences of Head and Tail, respectively.

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

  25. START Exercise: Multiplication count0 for i=0; i<10; i++ 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.133) to handle “You made 2 mistake(s) today.” Generate two random number a,b[0,99] a,b c c==a*b count++ “Incorrect!” End

More Related