Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C++

ฟังก์ชัน log1p() ใน C++

ฟังก์ชัน log1p() คำนวณลอการิทึมธรรมชาติ (Natural Logarithm) ของ x + 1 ให้ความแม่นยำสูงเมื่อ x ใกล้ศูนย์ ส่งคืน NaN หาก x < -1

นิพจน์ทางคณิตศาสตร์

log1p(x) = ln(x + 1)

รูปแบบ

float log1p(float x);

ตัวอย่างโค้ด

#include <iostream>
#include <cmath>
using namespace std;

int main() {
    int x = 10;
    float y = 28.88;
    cout << "The log value of x : " << log1p(x);
    cout << "\nThe log value of y : " << log1p(y);
    return 0;
}

ผลลัพธ์

The log value of x : 2.3979
The log value of y : 3.39719