Home »
C++ STL
valarray log10() Function in C++ with Examples
C++ valarray log10() Function: Here, we will learn about the log10() function, its usages, syntax and examples.
Submitted by Shivang Yadav, on May 12, 2022
The valarray class in C++ is a special container that is used for holding elements like an array and performing operations on them.
Mathematical Log10 Value
Log10 or log base 10 is the exponent or power value to which the base value 10 is raised in order to yield the given number.
For expression,
10a = x
Log10x = a
C++ STL std::log10(std::valarray) Function
The log10() function of the valarray class is used to calculate the value of the log base 10 of elements of the valarray. It returns a valarray with each element calculated using log10() function.
Syntax
template< class T >
valarray<T> log10( const valarray<T>& va );
// or
log10(valarrayName)
Parameter(s)
It accepts a single parameter which is the original valarray.
Return value
It returns a valarray with the logarithmic value of each element of the original valarray.
Example 1
#include <iostream>
#include <valarray>
using namespace std;
int main()
{
// Declaring valarray
valarray<double> myvalarr = { 0, 10, 2, 1001, 1 };
// Printing the elements of valarray
cout << "The elements of orignal valarray are : ";
for (double& ele : myvalarr)
cout << ele << " ";
// Creating a new valarray
valarray<double> logValarray;
logValarray = log(myvalarr);
cout << "\nThe elements of log base 10 valarray are : ";
for (double& ele : logValarray)
cout << ele << " ";
return 0;
}
Output
The elements of orignal valarray are : 0 10 2 1001 1
The elements of log base 10 valarray are : -inf 2.30259 0.693147 6.90875 0
Example 2
#include <iostream>
#include <valarray>
using namespace std;
int main()
{
// Declaring valarray
valarray<double> myvalarr = { 0, 10, 2.32, -100, 1 };
// Printing the elements of valarray
cout << "The elements of orignal valarray are : ";
for (double& ele : myvalarr)
cout << ele << " ";
// Creating a new valarray
valarray<double> logValarray;
logValarray = log(myvalarr);
cout << "\nThe elements of log base 10 valarray are : ";
for (double& ele : logValarray)
cout << ele << " ";
return 0;
}
Output
The elements of orignal valarray are : 0 10 2.32 -100 1
The elements of log base 10 valarray are : -inf 2.30259 0.841567 -nan 0
Note: log10 of a negative value is nan (not a number).