How to find the natural logarithm of a number in TypeScript?

The natural logarithm is the logarithm of any numeric value to the base e. Here e is Euler's constant, and the value of Euler’s constant is approximately 2.718. In TypeScript, we can use the built-in library methods to find the natural logarithm of any numeric value greater than or equal to zero.

Using the Math.log() Method

Math is a library of TypeScript which contains all the methods to perform mathematical operations. Inside the Math object, all methods are static. So, we can directly access all methods by taking Math (object name) as a reference.

The math method also contains the log() method, which calculates and returns the natural logarithm of any positive value.

Syntax

Users can follow the syntax below to learn the use of the math library's log () method to calculate the number value's natural logarithm.

let result: number = Math.log(value)


Parameters

• value − It takes a single parameter which is always required. It is a numeric value greater than or equal to zero for which we need to calculate the natural logarithm.

Return value

It returns the logarithm of a value to the base E (natural logarithm).

Example

We have taken the different numeric values in the example below to find the natural logarithm. We have invoked the Math object's static log() method by taking the Math keyword as a reference.

// Defining the different numeric values
let value1: number = 43;
let value2: number = 0;
let value3: number = Math.E;
let value4: number = -756;
let value5: number = Infinity;

// calculating the natural logarithm of different values.
console.log("The natural logarithm of " + value1 + " is " + Math.log(value1));
console.log("The natural logarithm of " + value2 + " is " + Math.log(value2));
console.log("The natural logarithm of " + value3 + " is " + Math.log(value3));
console.log("The natural logarithm of " + value4 + " is " + Math.log(value4));
console.log("The natural logarithm of " + value5 + " is " + Math.log(value5));


On compiling, it will generate the following JavaScript code −

// Defining the different numeric values
var value1 = 43;
var value2 = 0;
var value3 = Math.E;
var value4 = -756;
var value5 = Infinity;

// calculating the natural logarithm of different values.
console.log("The natural logarithm of " + value1 + " is " + Math.log(value1));
console.log("The natural logarithm of " + value2 + " is " + Math.log(value2));
console.log("The natural logarithm of " + value3 + " is " + Math.log(value3));
console.log("The natural logarithm of " + value4 + " is " + Math.log(value4));
console.log("The natural logarithm of " + value5 + " is " + Math.log(value5));


Output

The above code will produce the following output −

The natural logarithm of 43 is 3.7612001156935624
The natural logarithm of 0 is -Infinity
The natural logarithm of 2.718281828459045 is 1
The natural logarithm of -756 is NaN
The natural logarithm of Infinity is Infinity


In the above output, users can observe the range of the Math.log() method, which returns the value between the -Infinity to Infinity. It returns -Infinity for 0 and Infinity for the Infinity value. For the negative values, the log() method returns the NaN representing not a number.

Using the Math.LN2 and Math.LN10

The LN2 and LN 10 are the properties of the Math object. We can use the LN2 property to get the natural logarithm of 2 and the LN2 property to get the natural logarithm of 10.

Syntax

Follow the syntax below to use the value of the LN2 and LN10 property values.

let ln2: number = Math.LN2;
let ln10: number = Math.LN10;


Example

// using the LN2 and LN10 property values of the Math object
let ln2: number = Math.LN2;
let ln10: number = Math.LN10;
console.log("The value of natural logarithm of 2 is " + ln2);
console.log("The value of natural logarithm of 10 is " + ln10);


On compiling, it will generate the following JavaScript code −

// using the LN2 and LN10 property values of the Math object
var ln2 = Math.LN2;
var ln10 = Math.LN10;
console.log("The value of natural logarithm of 2 is " + ln2);
console.log("The value of natural logarithm of 10 is " + ln10);


Output

The above code will produce the following output −

The value of natural logarithm of 2 is 0.6931471805599453
The value of natural logarithm of 10 is 2.302585092994046


We learned to find the natural logarithm of different number values using the Math.log() method in this tutorial.

Updated on: 03-Jan-2023

207 Views