Skip to content
Related Articles

Related Articles

log1p() in C++
  • Last Updated : 28 Apr, 2018

The log1p() function takes an argument x and returns the natural logarithm of the base-e logarithm of x+1. Here e is a mathematical constant with value equal to 2.71828.

Syntax:

double log1p (double x);
float log1p (float x);
long double log1p (long double x);

    Parameter:

  • The log1p() function takes a single argument in the range [-1, ?].
  • If we pass the value which is less than -1, log1p() returns Nan (Not a Number).

    Return:

  • a positive number : if x > 0
  • zero if x=0
  • a negative number if -1 > x > 0
  • -?(- infinity) if x=-1
  • NaN if x<-1

Error and Exception:



  1. It is mandatory to give both the arguments otherwise it will give error no matching function for call to ‘log1p()’.
  2. If we pass the string as argument we will get errorbno matching function for call to ‘log1p(const char [n]).
  3. It gives -inf if we pass -1.
  4. It gives zero if we pass 0.

Examples:

Input  : log1p(50.35)
Output :  3.93866
Input  : log1p(143)
Output : 4.96981

# CODE 1




// CPP program to illustrate log1p()
#include <cmath>
#include <iostream>
using namespace std;
  
int main()
{
    double x = 50.35, answer;
  
    // returns logarithm of 51.35 base e
    answer = log1p(x);
    cout << "log1p(" << x << ") = " 
         << answer << endl;
  
    return 0;
}
Output:
log1p(50.35) = 3.93866

# CODE 2




// CPP program to illustrate log1p()
#include <cmath>
#include <iostream>
  
using namespace std;
  
int main()
{
    double answer;
    int x = 143;
  
    // returns logarithm of 144 base e
    answer = log1p(x);
    cout << "log1p(" << x << ") = " 
         << answer << endl;
  
    return 0;
}
Output:
log1p(143) = 4.96981

Practical usage:

  • It is practically used to get logarithm value of given argument+1.
Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.
My Personal Notes arrow_drop_up
Recommended Articles
Page :