We discussed very simple C++ programs in the previous post which would have really helped the C++ beginners. In this post, I thought to show you how to deal with a little complex functions in cmath library
A simple C++ program which uses pow, sqrt and cos functions of cmath library
This program gets three user inputs. they are a, b and Ơ. The program should do a calculation and output the answer. The formula to get the value of C is,
C=√(a2+b2+2abcosƠ)
refer the below code...
#include<iostream> //cout, cin funtions are from iostream library
#include<cmath> //pow,sqrt,cos funtions are from cmath
using namespace std;
int main()
{
double a,b,c,theta,theta_rad,pi;
pi=(22.0)/7; //we store the value of PI in this variable
cout<<"Enter a :"; //asking the user to enter a
cin>>a;
cout<<"Enter b :"; //asking the user to enter b
cin>>b;
cout<<"Enter theta :"; //asking the user to enter theta
cin>>theta;
theta_rad=(theta/180.0)*pi; //converting the user entered theta value to radians
c=sqrt(pow(a,2)+pow(b,2)+(2.0*a*b*cos(theta_rad))); //the calculation
cout<<"If a= "<<a<<",b = "<<b<<" and thata = "<<theta<<" then c is "<<c<<endl; //output the result
}
sqrt ()----> is a function available in cmath library which gets the square-root of the value within the brackets after sqrt.
pow(x,y)----> is a function which considers the first parameter (x) as the base value and the second parameter (y) as the power value. for an instance to get 5^2 -----> pow(5,2)
cos(x)------->whatever the value x (x should be in radient) within the function cos, will return the cos value from this function
A simple C++ program which use of exp() function of cmath library
The following program will describe you on how to use the exponent functionality with the aid of c++ in calculations. For the purpose, there is an inbuilt function available in the cmath library named exp().
"exp" means "exponential" function.
In other words, exp(x)= ex It's used when it is too much trouble or too expensive to Add superscript on top of superscript. The number e (about 2.18) to the x power. It is the inverse function to natural logarithm.
refer the code below
- #include<iostream>
- #include<cmath>
- using namespace std;
- int main()
- {
- double I,Is,V,Vt,K,T,Cel,Q;
- Is=5e-9;
- V=0.62f;
- Q=1.6e-19;
- K=1.38e-23;
- T=Cel+273.0;
- Vt=(K*T)/Q;
- I=Is*(exp(V/Vt)-1.0);
- cout<<"Current is "<<Is<<endl;
- return 0;
- }
You have to note some special points here.
in line number 9, the value assigned for Is variable is, 5 multiplied by e and that value is deducted by 9.
now, you may think what actually the value e means. Let me explain it a bit to you.
The number e is an important mathematical constant, approximately equal to 2.71828, that is the base of the natural logarithm. If you do the math without using a calculator or a program like this, you may surely tend to use 2.8 (which the value e approximately is) but it is not 100% accurate, isn't it?
Note that you have options to get the exact value of e from a scientific calculator. Similar to that, e in C++ also gives you the value of the constant e. So, 5e means 5 multiplied by the value of e.
In line number 18, you can see another function exp() which is a function in cmath library where the value passed inside the exp function as a parameter, will be considered as a value of the power of e.
If you execute the above program, you will have to get the following output.
Current is 5e-09
Some other functions that are available under cmath Library to make mathematical equations easier in C++.
Trigonometric functions:
Hyperbolic functions:
Exponential and logarithmic functions:
Power functions
Rounding, absolute value and remainder 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
- pow
- Raise to power
- sqrt
- Compute square root
Rounding, absolute value and remainder functions:
- ceil
- Round up value
- fabs
- Compute absolute value
- floor
- Round down value
- fmod
- Compute remainder of division
Though I did not explained you each and every function of cmath, I think you had an idea on how to use the cmath functions to make your program efficient and accurate. Also remember, whenever you think like introducing a function to do some task, first search whether there is an inbuilt function available in the system libraries. If available, you may use them straight away and if not, you can go ahead with your own method.
No comments:
Post a Comment