log() in CPP

Learn via video course
FREE
View all courses
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
Topics Covered

Overview

The log() function defined in the <cmath> header file in C++ returns the natural logarithm of the argument passed.

Syntax of log() in CPP

Types of prototype of log() function in C++11.

The syntax of the log() function is given as follows :

Here, result is the variable in which the natural log of x is stored.

Scaler Placement Report and Statistics

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data

Parameters of log() in CPP

The log() in cpp takes a number in the range of [0,\infty] as a valid argument. If the argument passed is out of the range, i.e., less than 0, then the log() function returns NaN (Not a Number).

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

Return Value of log() in CPP

ParameterReturn value
x > 1Positive number
x = 10
0< x <1Negative number
x = 0-inf
x < 0NaN

Example

Let us try to implement the log() function in cpp.

Code:

Output:

Explanation:

In the above code,

  • We are storing the value of x as 10.
  • We will pass the value of x to the log() function in cpp.
  • The log() function evaluates the natural logarithm of x i.e, 10.
  • The value of the natural logarithm of x is then returned by the log() function.
  • The value is stored in the result variable, which is later printed out as output.

What is log() in C++?

Natural logarithms are inverse of the natural exponential function. When we use the log in cpp, it returns the logarithmic value of the parameter passed, with base e. The log() function takes a number as an input and returns a double type value for integral parameters. Depending on the different values of the arguments passed, the log() function returns different values. A valid argument is in the range of [0, \infty]. Any number out of the given range will result in NaN (Not a Number) by the log() function.

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

More Examples

Let us consider trying to pass different possible values as arguments to the log() function in cpp.

Passing 0 as Argument

Code:

Output:

In the above code, we passed 0 as the parameter to log in cpp. The log() function in cpp returns -inf.

Passing a Negative Number as an Argument

Code:

Output:

In the above code, we passed a negative number as the parameter to log in cpp. The log() function in cpp returns -nan.

Passing a Positive Number Greater than 1 as an Argument

Code:

Output:

In the above code, we passed a positive number greater than 1 as the parameter to log in cpp. The log() function in cpp returns the natural logarithm of the number passed.

Passing a Number Between 0 and 1 as an Argument

Code:

Output:

In the above code, we passed a positive number greater than 0 and less than 1 as the parameter to log in cpp. The log() function in cpp returns the natural logarithm of the number passed. The result will be negative.

Passing 1 as an Argument

Code:

Output:

In the above code, we passed 1 as the parameter to log in cpp. The log() function in cpp returns 0 as the result.

Conclusion

  • The log() function in cpp is used to find the natural logarithm of the number.
  • The log() function takes a single number as a parameter.
  • The log() function returns a double type value for an integral type valid argument.