1 / 41

Mathematical Functions

Mathematical Functions. Mathematical Functions (1). The math library file cmath contains common and often used mathematical functions such as sine, cosine, log, square root, etc. To use, your program must have the following line in the header: #include <cmath>

astrid
Télécharger la présentation

Mathematical Functions

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. Mathematical Functions The Ohio State University

  2. Mathematical Functions (1) • The math library file cmath contains common and often used mathematical functions such as sine, cosine, log, square root, etc. To use, your program must have the following line in the header: #include <cmath> • As in math, functions take input and produce an output. For example, the square root function, sqrt(), needs a number input to product an output. The Ohio State University

  3. Mathematical Functions (2) • These math functions can be used like a variable of the same data type: double a = 81; double b = sqrt(a); double c = sqrt(sqrt(a)) * b; • Here, a is 81, b is 9, and c is 27 • This statement would output 11: cout << sqrt(121.0) << endl; The Ohio State University

  4. Mathematical Functions (3) • When data needs to be passed into the function, you place it inside the parentheses (). The passed data are called arguments or parameters. • A function can have more than one input. For instance, the power function needs a base and an exponent to produce an output. • For example, cout << pow(3.0, 4.0); would output 81 which is 3 to the 4th power. The Ohio State University

  5. Mathematical Functions (4) • Arguments passed into any function can be a constant, variable, an expression that evaluates to the appropriate data type such as the following: double j = 2.0; cout << sqrt(j * 32.0); //outputs 8 The Ohio State University

  6. Mathematical Functions (5) • Functions can themselves be part of an expression. double x = 10; cout << 5 * pow( (x - 3), 2.0 ); //output? The Ohio State University

  7. Common Math Functions The Ohio State University

  8. Example: sqrt() #include <iostream> #include <cmath> using namespace std; int main() { double x(0.0), y(0.0); double dist(0.0); cout << "Enter x, y: "; cin >> x >> y; dist = sqrt(x*x + y*y); cout << "Distance of (" << x << "," << y << ") to (0,0) = " << dist << endl; return 0; } The Ohio State University

  9. cout << "Enter x, y: "; cin >> x >> y; dist = sqrt(x*x + y*y); cout << "Distance of (" << x << "," << y << ") to (0,0) = " << dist << endl; … > distance.exe Enter x, y: 3 4 Distance of (3,4) to (0,0) = 5 > distance.exe Enter x, y: 5 8 Distance of (5,8) to (0,0) = 9.43398 The Ohio State University

  10. Example 2: sqrt() #include <iostream> #include <cmath> // math function library using namespace std; int main () { double height(0), time(0.0); cout << "Enter height (feet): "; cin >> height; time = sqrt(2.0 * height / 32.2); cout << "It will take " << time << " seconds to fall " << height << " feet. " << endl; return 0; } The Ohio State University

  11. cout << "Enter height (feet): "; cin >> height; time = sqrt(2.0 * height / 32.2); cout << "It will take " << time << " seconds to fall " << height << " feet. " << endl; … > gravity.exe Enter height: 100 It will take 2.49222 seconds to fall 100 feet. > gravity.exe Enter height (feet): 500 It will take 5.57278 seconds to fall 500 feet. The Ohio State University

  12. Example: log() #include <iostream> #include <cmath> using namespace std; int main() { double rate(0.0), years(0.0); cout << "Enter annual interest rate (percentage): "; cin >> rate; years = log(2.0)/log(1+(rate/100.0)); cout << "Your money will double in " << years << " years." << endl; return 0; } The Ohio State University

  13. cout << "Enter annual interest rate (percentage): "; cin >> rate; years = log(2.0)/log(1+(rate/100.0)); cout << "Your money will double in " << years << " years." << endl; … > interest.exe Enter annual interest rate (percentage): 7 Your money will double in 10.2448 years. > interest.exe Enter annual interest rate (percentage): 2 Your money will double in 35.0028 years. The Ohio State University

  14. Trigonometric Math Functions • Trigonometric math functions (sin, cos, tan) require input in radians. The Ohio State University

  15. Example: sin(), cos() #include <iostream> #include <cmath> using namespace std; int main() { double angle(0.0), x(0.0), y(0.0); cout << "Enter rotation angle (radians): "; cin >> angle; x = cos(angle); y = sin(angle); cout << "Point (1,0) rotates to point" << "(" << x << "," << y << ")" << endl; return 0; } The Ohio State University

  16. cout << "Enter rotation angle (radians): "; cin >> angle; x = cos(angle); y = sin(angle); cout << "Point (1,0) rotates to point" << "(" << x << "," << y << ")" << endl; … > rotate_radians.exe Enter rotation angle (radians): 1.57 Point (1,0) rotates to point (0.000796327,1) > rotate_radians.exe Enter rotation angle (radians): 0.78 Point (1,0) rotates to point (0.710914,0.703279) The Ohio State University

  17. Common Math Constants • In addition to functions, cmath also defines some commonly used math constants. • These are used like variables, except that they cannot be assigned a new value (hence “constants”). The Ohio State University

  18. degrees2radians.cpp #include <iostream> #include <cmath> // cmath contains definitions of math constants using namespace std; int main() { double degrees(0.0), radians(0.0); cout << "Enter angle in degrees: "; cin >> degrees; radians = (degrees * M_PI) / 180.0; cout << "Angle in radians = " << radians << endl; return 0; } The Ohio State University

  19. rotate_degrees.cpp #include <iostream> #include <cmath> // cmath contains definitions of math constants using namespace std; int main() { double degrees(0.0), radians(0.0), x(0.0), y(0.0); cout << "Enter rotation angle (degrees): "; cin >> degrees; radians = (degrees * M_PI)/180.0; x = cos(radians); y = sin(radians); cout << "Point (1,0) rotates to point (" << x << "," << y << ")" << endl; return 0; } The Ohio State University

  20. cout << "Enter rotation angle (degrees): "; cin >> degrees; radians = (degrees * M_PI)/180.0; x = cos(radians); y = sin(radians); cout << "Point (1,0) rotates to point" << "(" << x << "," << y << ")" << endl; … > rotate_degrees.exe Enter rotation angle (degrees): 90 Point (1,0) rotates to point (6.12323e-17,1) > rotate_degrees.exe Enter rotation angle (degrees): 45 Point (1,0) rotates to point (0.707107,0.707107) The Ohio State University

  21. log_2.cpp #include <iostream> #include <cmath> // cmath contains definitions of math constants using namespace std; int main() { double x(0.0), y(0.0); cout << "Enter number: "; cin >> x; y = log(x) / M_LN2; cout << "log_e(" << x << ") = " << log(x) << endl; cout << "log_2(" << x << ") = " << y << endl; return 0; } The Ohio State University

  22. Modulus Operator: % • a mod b is the remainder after integer a is divided by integer b; • In C++ a mod b is written as: a%b • Examples: 25 % 3 = ? 137 % 10 = ? 2751 % 2 = ? The Ohio State University

  23. Arguments to Math Functions • Arguments to math functions should have type double, e.g.: double x(3.6), y(0.3), z(0.0); z = sin(1.2); z = sqrt(x); z = log(3.2 * x); z = pow(x / 0.5, 1.2 * y); The Ohio State University

  24. logError.cpp . . . int main() { int value(0); cout << "Enter value: "; cin >> value; // log(value) generates a compiler error cout << "At 10% interest, it will take " << log(value)/log(1.1) << " years for a $1 investment to be worth $" << value << "." << endl; return 0; } The Ohio State University

  25. • // log(value) generates a compiler error • cout << "At 10% interest, it will take " << log(value) / log(1.1) • << " years for a $1 investment to be worth $" << value << "." << endl; • … > g++ logError.cpp –o logError.exe Compiling logError.cpp into logError.exe. logError.cpp: In function `int main()': logError.cpp:16: call of overloaded `log(int&)' is ambiguous /usr/include/iso/math_iso.h:52: candidates are: double log(double) /usr/local/include/g++-v3/bits/std_cmath.h:333: long double std::log(long double) /usr/local/include/g++-v3/bits/std_cmath.h:323: float std::log(float) The Ohio State University

  26. logExample.cpp . . . int main() { int value(0); double x(0.0); cout << "Enter value: "; cin >> value; x = value; // implicit conversion to double cout << "At 10% interest, it will take " << log(x)/log(1.1) << " years for a $1 investment to be worth $" << value << "." << endl; return 0; } The Ohio State University

  27. double x(0.0); ... x = value; // implicit conversion to double cout << "At 10% interest, it will take " << log(x)/log(1.1) << " years for a $1 investment to be worth $" << value << "." << endl; … > logExample.cpp Enter value: 10 At 10% interest, it will take 24.1589 years for a $1 investment to be worth $10. > The Ohio State University

  28. Mixed Mode Expressions The Ohio State University

  29. Expression Types • An expression that contains only integer operands is an integer expression. • An expression that contains only floating point operands is a floating-point expression. The Ohio State University

  30. Mixing Expression Types • A mixed-mode expression has both floating-point and integer data types. • The rules governing the data type of the result are: • If both operands are integers, the result is an integer • If one operand is a floating-point number, then the result is a double The Ohio State University

  31. Mixed Mode Expressions int a(3); double x(3.5), y(5), z(0.0); z = 3.0 * 25; z = a * x; z = a * y; What about: z = x + (a/2); The Ohio State University

  32. Exercises int a(3), b(2); double y(5), z(0.0); After each operation, what is z? z = (y + a) / b; z = (y * a) / b; z = y * (a / b); z = (y / b) * (a / b); z = (a + b) / (b*y); The Ohio State University

  33. logError.cpp . . . int main() { int value(0); cout << "Enter value: "; cin >> value; // log(value) generates a compiler error cout << "At 10% interest, it will take " << log(value)/log(1.1) << " years for a $1 investment to be worth $" << value << "." << endl; return 0; } The Ohio State University

  34. logExample2.cpp . . . int main() { int value(0); cout << "Enter value: "; cin >> value; // Mixed mode operation "value*1.0" returns double. cout << "At 10% interest, it will take " << log(value*1.0)/log(1.1) << " years for a $1 investment to be worth $" << value << "." << endl; return 0; } The Ohio State University

  35. Operator Precedence The Ohio State University

  36. Evaluate • Evaluate: 1 + 3*6 – 4/2 = ??? The Ohio State University

  37. Evaluate • Evaluate: 20 – 16 / 2 + 2 * 3 = ??? • (((20 – 16) /2) +2) *3 = 12. • ((20 – 16) / (2 + 2)) * 3 = 3. • 20 – ((16/2) + (2*3)) = -4. The Ohio State University

  38. Operator Precedence and Associativity • The minus sign can also be used as a unary operator that serves to negate the sign of a number The Ohio State University

  39. arithmetic3.cpp // Precedence of arithmetic operators #include <iostream> using namespace std; int main() { cout << "-3+5*2 = " << -3+5*2 << endl << endl; // Is this? cout << "((-3)+5)*2 = " << ((-3)+5)*2 << endl; cout << "(-(3+5))*2 = " << (-(3+5))*2 << endl; cout << "(-3)+(5*2) = " << (-3)+(5*2) << endl; cout << "-(3+(5*2)) = " << -(3+(5*2)) << endl; return 0; } The Ohio State University

  40. Math in C++ • Use #include<cmath> for math functions; • Common math functions: abs(a), pow(b,e), sqrt(a), sin(a), cos(a), tan(a), log(a), log10(a), exp(a); • Common math constants: M_PI, M_E, M_LN2, M_LN10; The Ohio State University

  41. Math in C++ • Arguments to functions should always be double; • Mixed mode operations: (3.0 + 5) or (3.0 * 5) have type double; • Operator precedence: Multiplication and division before addition and subtraction. The Ohio State University

More Related