640 likes | 2.26k Views
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
E N D
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
Absolute Function • Likewise, if we have an absolute functiona(y) = | y |then we know that
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)?
#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
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.
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
Hyperbolic functions • cosh - Compute hyperbolic cosine • sinh - Compute hyperbolic sine • tanh - Compute hyperbolic tangent
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
Power functions • sqrt - Compute square root • pow - Raise to power • pow(64.0, 1.0/3.0)
Rounding, absolute value and remainder functions: • ceil - Round up value • fabs - Compute absolute value • floor - Round down value • fmod - Compute remainder of division
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; }
Exercise • Prime Numbers with sqrt()
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; }
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; }
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; }
Exercise: Plot cos(x) using characters Amplify and Shift
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.
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;
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; } 種瓜得瓜,種豆得豆
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;
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.
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; }
START Exercise: Multiplication count0 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