C++ Standard Library C++ STL Library

C++ <valarray> - log() Function



The C++ <valarray> log() function returns a valarray containing natural logarithm (base-e logarithm) value of all the elements of valarray va, in the same order. This function overloads with cmath's log() function and calls this function once for each element.

Syntax

template<class T> valarray<T> log (const valarray<T>& va);

Parameters

va Specify valarray containing elements of a type for which the function log() is defined.

Return Value

Returns valarray containing natural logarithm (base-e logarithm) value of all the elements of va.

Example:

The example below shows the usage of log() function.

#include <iostream>
#include <valarray>
using namespace std;
 
int main (){
  valarray<double> va1 = {1, 2, 3, 4};
  
  //va2 will contain log of all elements of va1
  valarray<double> va2 = log(va1);

  cout<<fixed;
  cout<<"va1 contains: ";
  for(int i = 0; i < va1.size(); i++)
    cout<<va1[i]<<" ";

  cout<<"\nva2 contains: ";
  for(int i = 0; i < va2.size(); i++)
    cout<<va2[i]<<" ";   
  
  return 0;
}

The output of the above code will be:

va1 contains: 1.000000 2.000000 3.000000 4.000000 
va2 contains: 0.000000 0.693147 1.098612 1.386294

❮ C++ <valarray> Library