Skip to content
Related Articles
Open in App
Not now

Related Articles

p5.js | log() function

Improve Article
Save Article
  • Last Updated : 17 Aug, 2021
Improve Article
Save Article

The log() function in p5.js is used to get the natural logarithm (of base “e”) of any number taken as input for the parameter of log() function.



Parameters: This function accepts a single parameter x which is any number greater than zero (0) taken as the input whose natural log is going to be calculated.

Return Value: It returns the natural log of any input number greater than zero (0).

Below program illustrates the log() function in p5.js:

Example: This example uses log() function to get the natural log value of any input number. 


function setup() {
    // Create Canvas of size 270*80
    createCanvas(550, 130);
function draw() {
    // Set the background color
    // Initialize the parameter
    let a = 5;
    let b = 7.7;
    let c = 0;
    let d = -5;
    // Call to log() function
    let v = log(a);
    let w = log(b);
    let x = log(c);
    let y = log(d);
    // Set the size of text
    // Set the text color
    // Getting natural log value
    text("Natural logarithm value of 5 is : " + v, 50, 30);
    text("Natural logarithm value of 7.7 is : " + w, 50, 50);
    text("Natural logarithm value of 0 is : " + x, 50, 70);
    text("Natural logarithm value of -5 is : " + y, 50, 90);


Note: If we take input as a negative value and zero then it returns the output as “NaN” and -Infinity respectively.


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!