C++ log10()

The log10() function in C++ returns the common logarithm (base 10 logarithm) of the argument.

This function is defined in <cmath> header file.


[Mathematics] log10x = log10(x) [In C++ Programming]

log10() prototype [As of C++ 11 standard]

double log10 (double x);
float log10 (float x);
long double log10 (long double x);
double log10 (T x);  // For integral type

log10() Parameters

The log10() function takes a single mandatory argument in the range [0, ∞].

If the value is less than 0, log10() returns NaN (Not a Number).


log10() Return Value

The log10() function returns the base 10 logarithm of a number.


Parameter (x) Return VALUE
x > 1 Positive
x = 1 0
0 > x > 1 Negative
x = 0 -∞ (- infinity)
x < 0 nan (Not a Number)

Example 1: How log10() works?

#include <iostream>
#include <cmath>
using namespace std;

int main ()
{
	double x = 13.056, result;
	result = log10(x);
	cout << "log10(x) = " << result << endl;
	
	x = -3.591;
	result = log10(x);
	cout << "log10(x) = " << result << endl;
	
	return 0;
}

When you run the program, the output will be:

log10(x) = 1.11581
log10(x) = nan

Example 2: log10() With Integral Type

#include <iostream>
#include <cmath>
using namespace std;

int main ()
{
	int x = 2;
	double result;

	result = log10(x);
	cout << "log10(x) = " << result << endl;
	
	return 0;
}

When you run the program, the output will be:

log10(x) = 0.30103