exp, log, and log10

Illustrates how to use the exp, log, and log10 Standard Template Library (STL) functions in Visual C++.

template<class T> 
   valarray<T> exp( 
      const valarray<T>& x 
   );
template<class T> 
   valarray<T> log( 
      const valarray<T>& x 
   );
template<class T> 
   valarray<T> log10( 
      const valarray<T>& x 
   );

Remarks

Notes

The class/parameter names in the prototype do not match the version in the header file. Some have been modified to improve readability.

Example

// explog10.cpp
// compile with: /EHsc
#include <iostream>   // for i/o functions
#include <valarray>   // for valarray
#include <math.h>    // for exp(), log(), and log10()
using namespace std;

#define ARRAY_SIZE  3   // array size

typedef valarray<double> DB_VARRAY;

int main() {
   // Set val_array to contain values 1, 10, 100 for the following test.
   DB_VARRAY val_array(ARRAY_SIZE);
   int i;
   for (i = 0; i < ARRAY_SIZE; i++)
      val_array[i] = pow((double)10, i);

   // Display the size of val_array.
   cout << "Size of val_array = " << val_array.size() << endl;

   // Display the content of val_array before calling exp, log, and
   // log10 functions.
   cout << "val_array values:";
   for (i = 0; i < ARRAY_SIZE; i++)
      cout << "  " << val_array[i];
   cout << endl;

   // rvalue_array to hold the return value from calling the exp,
   // log, and log10 functions.
   DB_VARRAY rvalue_array;

   // ----------------------------------------------------------------
   // exp() - display the content of rvalue_array
   // ----------------------------------------------------------------
   rvalue_array = exp(val_array);
   cout << "After calling exp():";
   for (i = 0; i < ARRAY_SIZE; i++)
      cout << "   " << rvalue_array[i];
   cout << endl;

   // ----------------------------------------------------------------
   // log() - display the content of rvalue_array
   // ----------------------------------------------------------------
   rvalue_array = log(val_array);
   cout << "After calling log():";
   for (i = 0; i < ARRAY_SIZE; i++)
      cout << "  " << rvalue_array[i];
   cout << endl;

   // ----------------------------------------------------------------
   // log10() - display the content of rvalue_array
   // ----------------------------------------------------------------
   rvalue_array = log10(val_array);
   cout << "After calling log10():";
   for (i = 0; i < ARRAY_SIZE; i++)
      cout << "  " << rvalue_array[i];
   cout << endl;
}

Output

Size of val_array = 3
val_array values:  1  10  100
After calling exp():   2.71828   22026.5   2.68812e+043
After calling log():  0  2.30259  4.60517
After calling log10():  0  1  2

Requirements

Header: <valarray>

See Also

Concepts

Standard Template Library Samples