C++ Math log10
The function calculates the standard base 10 algorithm of a provided number.
Mathematically:
Suppose a number is 'x':
log10x = log10(x);
Syntax
float log10(float x);
double log10(double x);
long double log10(long double x);
double log10(integral x);
Note: The return_type can be float, double or long double.
Parameter
x : The value for which we need to determine the logarithm.
Return value
Following are the output values of a specified number:
| Parameter(x) | Return value |
|---|---|
x>1 |
Positive |
x=1 |
0 |
| 1>x>0 | Negative |
x=0 |
-infinity |
x<0 |
Not a Number |
Example 1
Let's explore a basic scenario where the value of x exceeds 1.
#include <iostream>
#include<math.h>
using namespace std;
int main()
{
int x=5;
std::cout << "Value of x is : " <<x <<std::endl;
cout<<"Log value of x is : "<<log10(x);
return 0;
}
Output:
Value of x is : 5
Log value of x is : 0.69897
In this instance, x is assigned a value of 5. Consequently, the log10 function yields the absolute value, which is approximately 0.69.
Example 2
Let's examine a basic scenario where the value of x is set to 1.
#include <iostream>
#include<math.h>
using namespace std;
int main()
{
int x=1;
std::cout << "Value of x is : " <<x <<std::endl;
cout<<"Log value of x is : "<<log10(x);
return 0;
}
Output:
Value of x is : 1
Log value of x is : 0
In this instance, the variable x is set to 1. As a result, the log10 function will yield a result of zero.
Example 3
Let's examine a basic scenario where the value assigned to x is 0.3.
#include <iostream>
#include<math.h>
using namespace std;
int main()
{
float x=0.3;
std::cout << "Value of x is : " <<x <<std::endl;
cout<<"Log value of x is : "<<log10(x);
return 0;
}
Output:
Value of x is : 0.3
Log value of x is : -0.522879
In this demonstration, x is assigned a value of 0.3. Thus, when utilizing the log10 function, it yields a negative result which is -0.52.
Example 4
Let's examine a basic scenario where the value of x equals zero.
#include <iostream>
#include<math.h>
using namespace std;
int main()
{
int x=0;
std::cout << "Value of x is : " <<x <<std::endl;
cout<<"Log value of x is : "<<log10(x);
return 0;
}
Output:
Value of x is : 0
Log value of x is : -inf
In this particular case, when x equals zero, the log10 function yields a value of negative infinity.
Example 5
Let's examine a basic scenario where the value of x equals -4.
#include <iostream>
#include<math.h>
using namespace std;
int main()
{
float x= -4;
std::cout << "Value of x is : " <<x <<std::endl;
cout<<"Log value of x is : "<<log10(x);
return 0;
}
Output:
Value of x is : -4
Log value of x is : nan
In this instance, with x being equal to -4, the log10 function will result in Not a Number (nan) being returned.