Thursday, October 23, 2025
HomeLanguagesJavascriptp5.js log() function

p5.js log() function

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.

Syntax: 

log(x)

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. 

Javascript




function setup() { 
   
    // Create Canvas of size 270*80 
    createCanvas(550, 130); 
   
function draw() { 
       
    // Set the background color 
    background(220); 
       
    // 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 
    textSize(16); 
       
    // Set the text color 
    fill(color('red')); 
     
    // 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);
        


Output: 

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

Reference: https://p5js.org/reference/#/p5/log
 

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
Previous article
Next article
RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS