0% found this document useful (0 votes)
21 views15 pages

OOP Lab - 2.3 - C++ Math

This document provides information about mathematical functions in C++. It discusses the max and min functions, which return the highest or lowest value of two numbers. It also covers headers like <cmath> which provide functions for square roots, rounding, logarithms and other math operations. Finally, it lists over 20 common mathematical functions like abs, sqrt, round, pow and their usage, and assigns implementing these functions as homework.

Uploaded by

Salman shakeel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views15 pages

OOP Lab - 2.3 - C++ Math

This document provides information about mathematical functions in C++. It discusses the max and min functions, which return the highest or lowest value of two numbers. It also covers headers like <cmath> which provide functions for square roots, rounding, logarithms and other math operations. Finally, it lists over 20 common mathematical functions like abs, sqrt, round, pow and their usage, and assigns implementing these functions as homework.

Uploaded by

Salman shakeel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

FAST National University of Computer and

Emerging Sciences Peshawar


OOP Lab # 2.3 DEPARTMENT OF COMPUTER SCIENCE

C++ Programming
Computer Instructor: Muhammad Abdullah Orakzai

Prepared By: Muhammad Abdullah Orakzai (Lab Instructor CS)


Contents

1) C++ Math

2) Min and Max functions

3) C++ <cmath> Headers

4) Other math functions

2
C++ Math

C++ has many functions that allows you to perform mathematical tasks on numbers.

3
Max and min

The max(x,y) function can be used to find the highest value of x and y:
Example
cout << max(5, 10);

4
Max and min…

And the min(x,y) function can be used to find the lowest value of x and y:

Example
cout << min(5, 10);

5
Max and min Example

#include<iostream>

using namespace std;

int main()
{

cout<<"Maximum Number is: "<<max(5,8)<<endl;


cout<<"Manimum Number is: "<<min(5,8)<<endl;

return 0;
}

6
C++ <cmath> Headers

Other functions, such as sqrt (square root), round (rounds a number) and log (natural
logarithm), can be found in the <cmath> header file:
Example
// Include the cmath library
#include <cmath>

cout << sqrt(64);


cout << round(2.6);
cout << log(2);

7
C++ <cmath> Headers Example
#include<iostream>
using namespace std;
#include<cmath>

int main()
{

cout<<"Square root of 64 is: "<<sqrt(64)<<endl;

cout<<"log of 2 is: "<<log(2)<<endl;

cout<<"Round of 2.6 is: "<<round(2.6)<<endl;

return 0; 8
}
Other Math Functions

A list of other popular Math functions (from the <cmath> library) can be found in the
table below:
Function Description
abs(x) Returns the absolute value of x
acos(x) Returns the arccosine of x
asin(x) Returns the arcsine of x
atan(x) Returns the arctangent of x
cbrt(x) Returns the cube root of x
ceil(x) Returns the value of x rounded up to its nearest integer
cos(x) Returns the cosine of x
9
Other Math Functions…

cosh(x) Returns the hyperbolic cosine of x


x
exp(x) Returns the value of E
x
expm1(x) Returns e -1
fabs(x) Returns the absolute value of a floating x
fdim(x, y) Returns the positive difference between x
and y
floor(x) Returns the value of x rounded down to its
nearest integer
2 2
hypot(x, y) Returns sqrt(x +y ) without intermediate
overflow or underflow

10
Other Math Functions…

fma(x, y, z) Returns x*y+z without losing precision


fmax(x, y) Returns the highest value of a floating
x and y
fmin(x, y) Returns the lowest value of a floating x
and y
fmod(x, y) Returns the floating point remainder of
x/y
pow(x, y) Returns the value of x to the power of y

11
Other Math Functions…

sin(x) Returns the sine of x (x is in


radians)
sinh(x) Returns the hyperbolic sine of a
double value
tan(x) Returns the tangent of an angle
tanh(x) Returns the hyperbolic tangent of a
double value

12
Home Task

In previous slides 23 math functions (Other Math Functions) are listed in the form of
table you all are directed to implement all these functions using C++ program. And show
me program in running form in upcoming lab. This task carry 10 marks.
Note: Program copied from someone or plagiarized will not be considered for grading.
Zero percent tolerance with plagiarism.

13
References

• https://beginnersbook.com/2017/08/cpp-data-types/
• https://www.geeksforgeeks.org/c-data-types/
• http://www.cplusplus.com/doc/tutorial/basic_io/
• https://www.geeksforgeeks.org/basic-input-output-c/
• https://www.w3schools.com/cpp/default.asp
• https://www.javatpoint.com/cpp-tutorial

14
THANK YOU

Thanks

15

You might also like