Math Log1P Function - C++ Programming Tutorial
C++ Course / Math Functions / Math Log1P Function

Math Log1P Function

BLUF: Mastering Math Log1P Function is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: Math Log1P Function

C++ is renowned for its efficiency. Learn how Math Log1P Function enables low-level control and high-performance computing in the tutorial below.

C++ Math log1p

The function calculates the natural logarithm of a specified number increased by one.

Suppose a number is 'x':

Example

log1p(x) = log(1+x);

Syntax

Example

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

Note: The return_type can be float, double or long double.

Parameter

x : What value are we trying to find the logarithm of?

Return value

Parameter Return value
x>0 Positive
x=0 zero
0>x> -1 Negative
x= -1 -infinity
x<-1 Not a Number(nan)

Example 1

Let's explore a basic scenario where the value of x is above zero.

Example

#include <iostream>
#include<math.h>
using namespace std;
int main()
{
     int x=10;
     std::cout << "Value of x is : " <<x <<std::endl;
     cout<<"log1p(x) = "<<log1p(x);
     return 0;
}

Output:

Output

Value of x is : 10
log1p(x) = 2.3979

In this instance, the log1p method calculates the natural logarithm for a value of x that is larger than zero.

Example 2

Let's consider a straightforward example where the value of x equals zero.

Example

#include <iostream>
#include<math.h>
using namespace std;
int main()
{
     int x=0;
     std::cout << "Value of x is : " <<x <<std::endl;
     cout<<"log1p(x) = "<<log1p(x);
     return 0;
}

Output:

Output

Value of x is : 0
log1p(x) = 0

In this instance, the log1p method calculates the natural logarithm of x plus one, specifically when x is close to zero.

Example 3

Let's examine a basic scenario where the value of x is negative.

Example

#include <iostream>
#include<math.h>
using namespace std;
int main()
{
     float x= -0.5;
     std::cout << "Value of x is : " <<x <<std::endl;
     cout<<"log1p(x) = "<<log1p(x);
     return 0;
}

Output:

Output

Value of x is : -0.5
log1p(x) = -0.693147

In this instance, the log1p method calculates the natural logarithm for values of x that are smaller than zero.

Example 4

Let's examine a basic scenario where the value assigned to x is -1.

Example

#include <iostream>
#include<math.h>
using namespace std;
int main()
{
     int x= -1;
     std::cout << "Value of x is : " <<x <<std::endl;
     cout<<"log1p(x) = "<<log1p(x);
     return 0;
}

Output:

Output

Value of x is : -1
log1p(x) = -inf

In this instance, the log1p function calculates the natural logarithm value when the input value x is -1.

Example 5

Let's examine a basic scenario where the value of x is below -1.

Example

#include <iostream>
#include<math.h>
using namespace std;
int main()
{
    int x= -3;
    std::cout << "Value of x is : " <<x <<std::endl;
    cout<<"log1p(x) = "<<log1p(x);
    return 0;
}

Output:

Output

Value of x is : -3
log1p(x) = -nan

In this instance, the log1p method calculates the natural logarithm when x is smaller than -1.

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience