Math Log10 Function - C++ Programming Tutorial
C++ Course / Math Functions / Math Log10 Function

Math Log10 Function

BLUF: Mastering Math Log10 Function is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: Math Log10 Function

C++ is renowned for its efficiency. Learn how Math Log10 Function enables low-level control and high-performance computing in the tutorial below.

C++ Math log10

The function calculates the standard base 10 algorithm of a provided number.

Mathematically:

Suppose a number is 'x':

Example

log10x = log10(x);

Syntax

Example

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.

Example

#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:

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.

Example

#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:

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.

Example

#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:

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.

Example

#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:

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.

Example

#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:

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.

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience