CSE202: Lecture 4The Ohio State University1 Mathematical Functions.

Slides:



Advertisements
Similar presentations
Lecture Computer Science I - Martin Hardwick Arithmetic Expressions With Integers rOperators:result is always an integer SymbolNameExampleValue (x.
Advertisements

Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 2 Simple C Programs.
Functions. COMP104 Functions / Slide 2 Introduction to Functions * A complex problem is often easier to solve by dividing it into several smaller parts,
Return values.
Chapter 3: Expressions and Interactivity. Outline cin object Mathematical expressions Type Conversion and Some coding styles.
1 ICS103 Programming in C Lecture 5: Introduction to Functions.
CSE202: Lecture 2The Ohio State University1 Variables and C++ Data Types.
1 Lecture 6 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.
Copyright © 2012 Pearson Education, Inc. Chapter 3: Expressions and Interactivity.
1 Fundamental Data types Overview l Primitive Data Types l Variable declaration l Arithmetical Operations l Expressions l Assignment statement l Increment.
Function (L16) * Mathematical Library Functions * Program Components in C++ * Motivations for Functionalizing a Program * Function Prototype * Function.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 3- 1.
1 9/08/06CS150 Introduction to Computer Science 1 Arithmetic Operators.
ICS 103 Lab 2-Arithmetic Expressions. Lab Objectives Learn different arithmetic operators Learn different arithmetic operators Learn how to use arithmetic.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 3: Expressions and Interactivity.
CSE202: Lecture 10AThe Ohio State University1 Numerical Error.
Topic 2A – Library Functions and Casting. CISC 105 – Topic 2A Functions A function is a piece of code which performs a specific task. When a function.
1 Data types, operations, and expressions Continued l Overview l Assignment statement l Increment and Decrement operators l Short hand operators l The.
Data Types, Expressions and Functions (part I)
What is a variable?  A variable holds data in memory so the program may use that data, or store results.  Variables have a data type. int, boolean, char,
Expressions and Interactivity Chapter 3. 2 The cin Object Standard input object Like cout, requires iostream file Used to read input from keyboard Often.
Computer Programming Fundamental Data Types Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons.
1 TAC2000/ Protocol Engineering and Application Research Laboratory (PEARL) MATH Functions in C Language.
Chapter 3 COMPLETING THE BASICS Programming Fundamentals with C++1.
CSE1222: Lecture 4The Ohio State University1. Mathematical Functions (1)  The math library file cmath Yes, this is a file with definitions for common.
Chapter 3 Expressions and Interactivity Department of Computer Science Missouri State Univeristy.
Copyright © 2012 Pearson Education, Inc. Chapter 3: Expressions and Interactivity.
D-1 University of Washington Computer Programming I Lecture 4: Arithmetic Expressions © 2000 UW CSE.
Summary of what we learned yesterday Basics of C++ Format of a program Syntax of literals, keywords, symbols, variables Simple data types and arithmetic.
CSE1222: Lecture 3The Ohio State University1. Assignment Operations  The C++ assignment operator is: =  Examples: x = 3 * 5; y = x – 7; y = y + 4; Do.
OPERATORS.
C++ Programming: Basic Elements of C++.
CSE1222: Lecture 2The Ohio State University1. mathExample2.cpp // math example #include using namespace std; int main() { cout
1 ICS103 Programming in C Lecture 7: Introduction to Functions.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 6 September 17, 2009.
CHAPTER 2 COMPONENTS OF A PROGRAMMING LANGUAGE I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
CS Class 08 Today  Exercises  Nested loops  for statement  Built-in functions Announcements  Homework #3, group solution to in-class.
C++ Programming Lecture 9 Functions – Part I By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Chapter 6 Mathematical Operations. 6.1 Mathematical Expressions In mathematics this expression is valid 0 = -4y + 5 It is invalid in programming Left.
Data Types Declarations Expressions Data storage C++ Basics.
Expressions and Interactivity. 3.1 The cin Object.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Formatting Output.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 3-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Recap……Last Time [Variables, Data Types and Constants]
D-1 University of Washington Computer Programming I Lecture 4: Arithmetic Expressions © 2000 UW CSE.
2/4/2016Engineering Problem Solving with C++, Second Edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 2 Simple C++ Programs.
Lecture 5: Expressions and Interactivity Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
29 January 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
Chapter 3 – Variables and Arithmetic Operations. First Program – volume of a box /************************************************************/ /* Program.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 3: Expressions and Interactivity.
Last Time…. Operators Arithmetic Operators Assignment Operators Increment/Decrement Operators Relational Operators Logical Operators Expression Statements.
C-1 9/30/99 CSE / ENGR 142 Programming I Arithmetic Expressions © 1999 UW CSE.
Arithmetic, Functions and Input 9/16/13. Arithmetic Operators C++ has the same arithmetic operators as a calculator: * for multiplication: a * b – Not.
Mathematical Manipulation Data types Operator precedence Standard mathematical functions Worked examples.
Chapter 4: Introduction To C++ (Part 3). The cin Object Standard input object Like cout, requires iostream file Used to read input from keyboard Information.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 3: Expressions and Interactivity.
Chapter INTRODUCTION Data Types and Arithmetic Calculations.
1 09/10/04CS150 Introduction to Computer Science 1 What Actions Do We Have Part 2.
1 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.
Variables, Operators, and Expressions
Lecture 3 Expressions, Type Conversion, Math and String
BIL 104E Introduction to Scientific and Engineering Computing
Arithmetic & other operations
Introduction to Programming
Arithmetic Operator Operation Example + addition x + y
Expressions and Interactivity
Elementary Programming (C++)
CS150 Introduction to Computer Science 1
Arithmetic Operations
Summary of what we learned yesterday
Presentation transcript:

CSE202: Lecture 4The Ohio State University1 Mathematical Functions

CSE202: Lecture 4The Ohio State University2 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 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.

CSE202: Lecture 4The Ohio State University3 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;

CSE202: Lecture 4The Ohio State University4 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 4 th power.

CSE202: Lecture 4The Ohio State University5 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

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

CSE202: Lecture 4The Ohio State University7 Common Math Functions FunctionReturned ValueData Type of Returned Value abs(a) Absolute value of a Data type of a pow(b,e) Value of b raised to the e th power float or double Data type of b sqrt(a) Square root of a double sin(a) Sine of a in radians double cos(a) Cosine of a in radians double tan(a) Tangent of a in radians double log(a) Natural log of a double log10(a) Common log (base 10) of a double exp(a)e raised to the a th power double

CSE202: Lecture 4The Ohio State University8 Example: sqrt() #include 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; }

CSE202: Lecture 4The Ohio State University9 > 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) = … cout << "Enter x, y: "; cin >> x >> y; dist = sqrt(x*x + y*y); cout << "Distance of (" << x << "," << y << ") to (0,0) = " << dist << endl; …

CSE202: Lecture 4The Ohio State University10 Example 2: sqrt() #include #include // 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; }

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

CSE202: Lecture 4The Ohio State University12 Example: log() #include 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; }

CSE202: Lecture 4The Ohio State University13 > interest.exe Enter annual interest rate (percentage): 7 Your money will double in years. > interest.exe Enter annual interest rate (percentage): 2 Your money will double in years. … 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; …

Trigonometric Math Functions Trigonometric math functions (sin, cos, tan) require input in radians. CSE202: Lecture 4The Ohio State University14

CSE202: Lecture 4The Ohio State University15 Example: sin(), cos() #include 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; }

CSE202: Lecture 4The Ohio State University16 > rotate_radians.exe Enter rotation angle (radians): 1.57 Point (1,0) rotates to point ( ,1) > rotate_radians.exe Enter rotation angle (radians): 0.78 Point (1,0) rotates to point ( , ) … cout << "Enter rotation angle (radians): "; cin >> angle; x = cos(angle); y = sin(angle); cout << "Point (1,0) rotates to point" << "(" << x << "," << y << ")" << endl; …

CSE202: Lecture 4The Ohio State University17 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”). ConstantValue M_PI π, … M_E e, the base of natural logarithms M_LOG2E Base-2 logarithm of e M_LOG10E Base-10 logarithm of e M_LN2 Natural log of 2 M_LN10 Natural log of 10

CSE202: Lecture 4The Ohio State University18 degrees2radians.cpp #include #include // 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; }

CSE202: Lecture 4The Ohio State University19 rotate_degrees.cpp #include #include // 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; }

CSE202: Lecture 4The Ohio State University20 > rotate_degrees.exe Enter rotation angle (degrees): 90 Point (1,0) rotates to point ( e-17,1) > rotate_degrees.exe Enter rotation angle (degrees): 45 Point (1,0) rotates to point ( , ) … 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; …

CSE202: Lecture 4The Ohio State University21 log_2.cpp #include #include // 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; }

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 = ? CSE202: Lecture 4The Ohio State University22

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); CSE202: Lecture 4The Ohio State University23

CSE202: Lecture 4The Ohio State University24 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; }

CSE202: Lecture 4The Ohio State University25 > 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) … 15. // log(value) generates a compiler error 16. cout << "At 10% interest, it will take " << log(value) / log(1.1) 17. << " years for a $1 investment to be worth $" << value << "." << endl; …

CSE202: Lecture 4The Ohio State University26 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; }

CSE202: Lecture 4The Ohio State University27 > logExample.cpp Enter value: 10 At 10% interest, it will take years for a $1 investment to be worth $10. > … 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; …

Mixed Mode Expressions CSE202: Lecture 4The Ohio State University28

CSE202: Lecture 4The Ohio State University29 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.

CSE202: Lecture 4The Ohio State University30 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: 1.If both operands are integers, the result is an integer 2.If one operand is a floating-point number, then the result is a double

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); CSE202: Lecture 4The Ohio State University31

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); CSE202: Lecture 4The Ohio State University32

CSE202: Lecture 4The Ohio State University33 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; }

CSE202: Lecture 4The Ohio State University34 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; }

Operator Precedence CSE202: Lecture 4The Ohio State University35

Evaluate Evaluate: 1 + 3*6 – 4/2 = ??? CSE202: Lecture 4The Ohio State University36

Evaluate Evaluate: 20 – 16 / * 3 = ??? (((20 – 16) /2) +2) *3 = 12. ((20 – 16) / (2 + 2)) * 3 = – ((16/2) + (2*3)) = -4. CSE202: Lecture 4The Ohio State University37

CSE202: Lecture 4The Ohio State University38 Operator Precedence and Associativity The minus sign can also be used as a unary operator that serves to negate the sign of a number PrecedenceAssociativity Unary - * / %Left to right + -Left to right

CSE202: Lecture 4The Ohio State University39 arithmetic3.cpp // Precedence of arithmetic operators #include 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; }

Math in C++ Use #include 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; CSE202: Lecture 4The Ohio State University40

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