Log() function in C++ : The log() function in C++ returns the natural logarithm (base-e logarithm) of the argument passed in the parameter.
Syntax for returning natural logarithm: result = log(x)
Syntax for returning logarithm (base-10 logarithm) of the argument. result = log10(x)
The parameters can be of any data type like int, double or float or long double. Log() function returns value according to the following conditions are as follows:
a) if x>1 then positive ..
b) if 0<x<1 returns a negative value ..
c) if x=1 then it returns 0 ..
d) if x=0 then it returns -inf ..
e) if x<0 then it returns NaN(not a number)
CPP
#include <bits/stdc++.h>
using namespace std;
double valueE( double d)
{
return log (d);
}
double value10( double d)
{
return log10 (d);
}
int main()
{
double d = 10;
cout << "The logarithm value(base-e) of " << d
<< " is " << valueE(d) << endl;
cout << "The logarithm value(base-10) of " << d
<< " is " << value10(d) << endl;
return 0;
}
|
Output
The logarithm value(base-e) of 10 is 2.30259
The logarithm value(base-10) of 10 is 1
Time Complexity: O(1)
Auxiliary Space: O(1)
Application: One of the applications of log() function is to calculate values related to log, for e.g. while finding polite number we need the formula to be written in code, for that we can use log() function. Given below is an implementation of log() function.
CPP
#include<bits/stdc++.h>
using namespace std;
double polite( double n)
{
n += 1;
double base = 2;
return n + ( log ((n + ( log (n) / log (base))))) / log (base);
}
int main()
{
double n = 7;
cout<< ( int )polite(n);
return 0;
}
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Errors and Exceptions:
- If the input is very large then the result will be inf which stands for infinity.
- If the input is negative then the error will be nan.
Here is the demonstration through example:-
C++
#include <bits/stdc++.h>
using namespace std;
double valueE( double d)
{
return log (d);
}
double value10( double d)
{
return log10 (d);
}
int main()
{
double d = -2;
cout << "Here is the error for input -2 is " << valueE(d)<<endl;
cout << "Here is the error for input -2 is " << value10(d)<<endl;
return 0;
}
|
Output
Here is the error for input -2 is nan
Here is the error for input -2 is nan
Here the error is nan.
C++
#include <bits/stdc++.h>
using namespace std;
double valueE( double d)
{
return log (d);
}
double value10( double d)
{
return log10 (d);
}
int main()
{
double d = 1e1000;
cout << "Here is the error for input 1e1000 is " << valueE(d)<<endl;
cout << "Here is the error for input 1e1000 is " << value10(d)<<endl;
return 0;
}
|
Output
Here is the error for input 1e1000 is inf
Here is the error for input 1e1000 is inf
Here the error is inf.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
09 Feb, 2023
Like Article
Save Article