You are on page 1of 11

Mathematical Functions in C++

• C++ supports large number of useful mathematical


functions.
• These functions are available in standard C++ and C
to support various mathematical calculations.
• These functions can be directly used to simplify
code and programs.
• C++ provides large set of mathematical functions, in
order to use these functions we need to include
header file- <math.h> or <cmath>.
Mathematical Functions in C++

• double sin(double) : This function takes angle (in degree) as an


argument and return its sine value that could be verified using sine
curve.
• double cos(double) : This function takes angle (in degree) as an
argument and return its cosine value that could be verified using cosine
curve.
• double tan(double) : This function takes angle (in degree) as an
argument and return its tangent value. This could also be verified using
Trigonometry as Tan(x) = Sin(x)/Cos(x).
• double sqrt(double) : This function takes number as an argument and
return its square root value. Number can not be negative value.
• int abs(int) : This function takes integer number as an argument and
return its absolute value. It means, the output will always be positive
regardless of sign of input.
Continue…
• double pow(double, double) : This function takes one
argument as base and other as exponent.
• double hypot(double, double) : This function requires two
sides of the right angled triangle to give output as its
hypotenuse.
• double floor(double) : This functions returns the integer
value lesser or equal to argument passed in the function.
• double fabs(double) : This function returns the absolute
value of any number.
• double acos(double) : This function returns the arc cosine of
argument. The argument to acos() must be in the range -1 to
1 ; otherwise, a domain error occurs.

Continue…
• double asin(double) : This function returns the arc sine of argument.
The argument to asin() must be in the range -1 to 1 ; otherwise, a
domain error occurs.
• double atan(double) : This function returns the arc tangent of arg.
• double atan2(double, double) : This function returns the arc tangent
of (double a)/(double b).
• double ceil(double) : This function returns the smallest integer as
double not less than the argument provided.
• double cosh(double) : This function returns the hyperbolic cosine of
argument provided. The value of argument provided must be in
radians.
• double tanh(double) : This function returns the hyperbolic tangent of
argument provided. The value of argument provided must be in
radians.
• double log(double) : This function takes a number and returns the
natural log of that number.
C++ Code for Demonstration of mathematical
Functions
• #include <iostream> • int z = -10;
• #include <math.h> • cout << "Absolute value of z=-10 : "
• using namespace std; << abs(z) << endl;
• int main() • cout << "Power value: x^y =
(2.3^0.25) : " << pow(x, y) << endl;
• {
• double x = 2.3;
• x = 3.0;
• cout << "Sine value of x=2.3 : " <<
sin(x) << endl; • y = 4.0;
• cout << "Cosine value of x=2.3 : " • cout << "Hypotenuse having other
<< cos(x) << endl; two sides as x=3.0 and"
• cout << "Tangent value of x=2.3 : " • << " y=4.0 : " << hypot(x, y) <<
<< tan(x) << endl; endl;

• double y = 0.25;
• cout << "Square root value of
y=0.25 : " << sqrt(y) << endl; Continue…
• x = 4.56;
• cout << "Floor value of x=4.56 is : " << • x = 57.3; // in degrees
floor(x) << endl;
• cout << "Hyperbolic Cosine of x=57.3 : " <<
cosh(x) << endl;
• x = -4.57; • cout << "Hyperbolic tangent of x=57.3 : " <<
• cout << "Absolute value of x=-4.57 is : " << tanh(x) << endl;
fabs(x) << endl;
• y = 100.0;
• x = 1.0; • // Natural base with 'e'
• cout << "Arc Cosine value of x=1.0 : " << • cout << "Log value of y=100.0 is : " << log(y)
acos(x) << endl; << endl;
• cout << "Arc Sine value of x=1.0 : " << asin(x)
<< endl;
• return 0;
• cout << "Arc Tangent value of x=1.0 : " <<
• }
atan(x) << endl;

• y = 12.3;
• cout << "Ceiling value of y=12.3 : " << ceil(y)
<< endl; Continue…
Output
• Sine value of x=2.3 : 0.745705
• Cosine value of x=2.3 : -0.666276
• Tangent value of x=2.3 : -1.11921
• Square root value of y=0.25 : 0.5
• Absolute value of z=-10 : 10
• Power value: x^y = (2.3^0.25) : 1.23149
• Hypotenuse having other two sides as x=3.0 and y=4.0 : 5
• Floor value of x=4.56 is : 4
• Absolute value of x=-4.57 is : 4.57 Arc
• Cosine value of x=1.0 : 0
• Arc Sine value of x=1.0 : 1.5708
• Arc Tangent value of x=1.0 : 0.785398
• Ceiling value of y=12.3 : 13
• Hyperbolic Cosine of x=57.3 : 3.83746e+24
• Hyperbolic tangent of x=57.3 : 1
• Log value of y=100.0 is : 4.60517
Program for Basic Arithmetic Calculations
• #include<iostream>
• #include<string>
• using namespace std;
• float mult(float num1,float num2);
• float add(float num1,float num2);
• float sub(float num1,float num2);
• float divide(float num1, float num2);
• int main()
•{
• int choice;
• float num1,num2;
• cout<<" Arithmetic Calculations "<<endl;
• cout<<" Make a selction:"<<endl;
• cout<<" 1.Division"<<endl;
• cout<<" 2.Adding"<<endl;
• cout<<" 3.Subtraction"<<endl;
• cout<<" 4.multiplication"<<endl;
• cout<<"YOUR CHIOCE IS: ";
• cin>>choice;
Continue…
• switch(choice){
• case 1:
• cout<<"Please enter the two numbers you want to
divide, divisor first!: ";
• cin>>num1>> num2;
• cin.ignore();
• cout<<" "<<num1<<"/"<<num2<<"=“
<<divide(num1,num2)<<endl;
• break;
• case 2:
• cout<<"Please enter the two numbers you want to
add: ";
• cin>>num1>> num2;
• cin.ignore();
• cout<<" "<<num1<<"+"<<num2<<"=“ <<add(num1,num2)<<endl;
• break;
Continue…
• case 3:
• cout<<"Please enter the two numbers you want to subtract, The
number you want to subtract from first!: ";
• cin>>num1>> num2;
• cin.ignore();
• cout<<" "<<num1<<"-"<<num2<<"="<<sub(num1,num2)<<endl;
• break;
• case 4:
• cout<<"Please enter the two numbers you want to multiply: ";

• cin>>num1>>num2;
• cin.ignore();

• cout<<" "<<num1<<"*"<<num2<<"="<<mult(num1,num2)<<endl;
• break;
• default:
• cout<<"Invalid Entry!, BYE!!!"<<endl;
• cin.ignore();
• break; }
• cin.ignore();
• return 0; }
Continue…
• float mult(float num1,float num2){
• return (num1*num2);
•}
• float add(float num1,float num2){
• return (num1+num2);
•}
• float sub(float num1,float num2){
• return (num1-num2);
•}
• float divide(float num1,float num2){
• return (num1/num2);
•}

You might also like