Math functions – sqrt/ pow/ log/ log10 – CSKC
Sign In

Search

Code Learning #CPP#2127    siteicon   siteicon  

Math functions – sqrt/ pow/ log/ log10

See the working of some common math functions like sqrt, pow, log and log10

Learning Objectives

  • Learning the use of four major math functions like sqrt, pow, log and log10

Source Code

TC++ #2127

Source Code

×

Ask AI About This Code

Default Prompt that is passed along with the code is given below, You can write your own prompt if you so desire.



×

Test CPP

Code Understanding

int n=25; double d=30.25;
These two initialisation have been done two pass two variables to the functions being tested. One being an integer value and another being an double (double precision fraction value)

cout<<“sqrt(“<<n<<“) = “<<sqrt(n)<<endl;
cout<<“sqrt(“<<d<<“) = “<<sqrt(d)<<endl;
The sqrt function can work well on int as well as fraction value (double/float). It will usually return a double type value.

cout<<“pow(“<<n<<“,2) = “<<pow(n,2)<<endl;
cout<<“pow(“<<d<<“,2) = “<<pow(d,2)<<endl;
pow(x,y) works as x to the power y, so here we are basically squaring a number. This can also return a double value.

cout<<“log(“<<n<<“) = “<<log(n)<<endl;
cout<<“log(“<<d<<“) = “<<log(d)<<endl;
This natural log function which can also return a double value.

cout<<“log10(“<<n<<“) = “<<log10(n)<<endl;
cout<<“log10(“<<d<<“) = “<<log10(d)<<endl;
This is log to the base 10 function and can return a double values.

Notes

  • In the earlier versions of C++, these functions were limited to returning double type value only. Now the return type in c++11 can be controlled by type of input. When float is input the function may return a float type.
  • For modern c++ environments like codeblock, eclipse, netbeans etc, use of cmath library is recommended instead of math.h


Suggested Filename(s): math-func.cpp



Share

Created: 2-Jan-2018 23:54 | Updated: 3-Jan-2018 17:07|