The Wayback Machine - https://web.archive.org/web/20250329155838/https://www.geeksforgeeks.org/log-function-cpp/
Open In App

log() Function in C++

Last Updated : 30 Sep, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

The std::log() in C++ is a built-in function that is used to calculate the natural logarithm (base e) of a given number. The number can be of any data type i.e. int, double, float, long long. It is defined inside the <cmath> header file.

In this article we will learn about how to use std::log() in C++.

Syntax

std::log(n);

Parameters

Return Value

  • Returns the natural log (base e) of the value

Example of std::log()

C++
// C++ program to show the working of log()
#include <bits/stdc++.h>
using namespace std;

int main() {

    // Return positive integer if the number is
    // greater than 1
    cout << log(11) << endl;

    // Return negative integer if the number is
    // between 0 and 1 (not inclusive)
    cout << log(0.5) << endl;

    // Return 0 if the number is 1
    cout << log(1) << endl;

    // Return -inf (infinity) if the number is 0
    cout << log(0) << endl;

    // Return inf (infinity) if the number is very large
    cout << log(1e1000) << endl;

    // Return NaN (Not a Number) if the number is ngative
    cout << log(-11) << endl;

    return 0;
}

Output
2.3979
-0.693147
0
-inf
inf
nan




Next Article
Article Tags :
Practice Tags :

Similar Reads

three90RightbarBannerImg