如何在TypeScript中查詢一個數的自然對數?


自然對數是以e為底的任何數值的對數。這裡的e是尤拉常數,尤拉常數的值約為2.718。在TypeScript中,我們可以使用內建的庫方法來查詢任何大於或等於零的數值的自然對數。

使用Math.log()方法

Math是TypeScript的一個庫,其中包含執行所有數學運算的方法。在Math物件中,所有方法都是靜態的。因此,我們可以透過將Math(物件名稱)作為引用來直接訪問所有方法。

math方法還包含log()方法,它計算並返回任何正值的自然對數。

語法

使用者可以按照以下語法學習如何使用math庫的log()方法計算數值的自然對數。

let result: number = Math.log(value)

引數

  • value − 它接受一個始終必需的單個引數。這是一個大於或等於零的數值,我們需要計算它的自然對數。

返回值

它返回以E為底的值的對數(自然對數)。

示例

我們在下面的示例中使用了不同的數值來查詢自然對數。我們透過將Math關鍵字作為引用來呼叫Math物件的靜態log()方法。

// 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));

編譯後,它將生成以下JavaScript程式碼:

// 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));

輸出

以上程式碼將產生以下輸出:

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

在上面的輸出中,使用者可以看到Math.log()方法的範圍,它返回-Infinity到Infinity之間的值。它對於0返回-Infinity,對於Infinity值返回Infinity。對於負值,log()方法返回NaN,表示非數字。

使用Math.LN2Math.LN10

LN2和LN10是Math物件的屬性。我們可以使用LN2屬性獲取2的自然對數,使用LN10屬性獲取10的自然對數。

語法

按照以下語法使用LN2和LN10屬性的值。

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

示例

// 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);

編譯後,它將生成以下JavaScript程式碼:

// 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);

輸出

以上程式碼將產生以下輸出:

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

在本教程中,我們學習瞭如何使用Math.log()方法查詢不同數值的自然對數。

更新於:2023年1月3日

414 次瀏覽

開啟您的職業生涯

透過完成課程獲得認證

開始學習
廣告
© . All rights reserved.