C++ Valarray::log10() Function



The C++ Valarray::log10() function returns the valarray consisting of the base10 logarithm value of all the elements in the current valarray. It call's the cmath's log10() function for every element in the valarray once.

base10 logarithm is also known as the decimal or common logarithm. for example, the common logarithm of x is the power to which the number 10 is raised to obtain the value of the x.

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

  • If we pass the value which is greaterthan 1(>1). it will returns the positive value.

  • If we pass the value which is equals to 1(=1). it will returns '0'.

  • If we pass the value which is 0>x>1. it will returns the Negative value.

  • If we pass the value which is equals to 0(=0). it will returns the '-infinity'.

  • If we pass the value which is lessthan 0 (<0). it will returns the nan value.

Syntax

Following is the syntax for C++ Valarray::log10() Function −

log10 (const valarray<T>& x);

Parameters

x − It is containing elements of a type for which the unary function abs is defined.

Examples

Example 1

Consider the following example, where we are going to use log10() function and retrieving output of the possible value for the base 10 algorithm.

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

int main() {
	valarray<double> varr = { 12,14,16,18,-2 };
	valarray<double> varr1;
	varr1 = log10(varr);
	cout << "The New log10"
		<< " Valarray is : "
		<< endl;
	for (double& x : varr1) {
		cout << x << " ";
	}
	cout << endl;
	return 0;
}

Output

When we compile and run the above program, this will produce the following result −

The New log10 Valarray is : 
1.07918 1.14613 1.20412 1.25527 nan 

Example 2

In the following example, we are going to use log10() function and retrieving output of the possible value for the base 10 algorithm.

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

int main() {
   valarray<double> myvalarr = { 2, 1, 0.32, 0, -3 };
   cout << "The Orignal Valarray is : ";
   
   for (double& ele : myvalarr)
      cout << ele << " ";
   valarray<double> logValarray;
   logValarray = log10(myvalarr);
   cout << "\nThe log10 Valarray is : ";
   
   for (double& ele : logValarray)
      cout << ele << " ";
   return 0;
}

Output

Let us compile and run the above program, this will produce the following result −

The Orignal Valarray is : 2 1 0.32 0 -3 
The log10 Valarray is : 0.30103 0 -0.49485 -inf nan

Example 3

Let's look into the following example, where we are going to use the integral type with the log10() function and gets the output.

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

int main () {
   int a = 34;
   double result;
   result = log10(a);
   cout << "log10(a) = " << result << endl;
   return 0;
}

Output

On running the above program, it will produce the following result −

log10(a) = 1.53148

Example 4

Considering the following example, where we are going to declare an array that acts as a power to the base10 and then we used the log10() function to those pow10 values.

#include <cmath>
#include <iomanip>
#include <iostream>
#include <valarray>

void show(char const* title, const std::valarray<float>& va) {
   std::cout << title << " : " << std::right;
   for (float a : va) {
      std::cout << std::setw(5) << a;
   }
   std::cout << '\n';
}
int main() {
   const std::valarray<float> n {2,3,-4,-1,0.6,1.2};
   const std::valarray<float> pow10 { std::pow(10, n) };
   const std::valarray<float> log10_pow10 { std::log10(pow10) };
   show("10ⁿ    ", pow10);
   show("log(10ⁿ)", log10_pow10);
}

Output

When the above program gets executed, it will produce the following result −

10ⁿ      :   100 10000.0001  0.13.9810715.8489
log(10ⁿ) :     2    3   -4   -1  0.6  1.2
Advertisements