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

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

ในบทความนี้ เราจะอภิปรายการทำงาน ไวยากรณ์ และตัวอย่างของฟังก์ชัน std::exp2() สำหรับจำนวนเชิงซ้อนใน C++ STL

std::exp2() คืออะไร?

ฟังก์ชัน std::exp2() สำหรับจำนวนเชิงซ้อนเป็นฟังก์ชันสำเร็จรูปใน C++ STL ซึ่งถูกกำหนดในไฟล์หัวข้อ <cmath> หรือ <ctgmath> ฟังก์ชัน exp2() ใช้สำหรับคำนวณฟังก์ชันเอ็กซ์โพเนนเชียลแบบไบนารี คือฟังก์ชันเอ็กซ์โพเนนเชียลฐาน-2 ของตัวเลขที่กำหนด

ฟังก์ชันนี้ส่งคืนค่า double, float หรือ long double ซึ่งคือ

ไวยากรณ์

exp2(double n);
exp2(float n);
exp2(long double n);

พารามิเตอร์

ฟังก์ชันยอมรับพารามิเตอร์ต่อไปนี้:

  • n - เป็นค่าของเลขชี้กำลัง

ค่าที่ส่งคืน

ฟังก์ชันนี้ส่งคืนค่าเอ็กซ์โพเนนเชียลฐาน-2 คือ 2^n

ตัวอย่าง

Input

exp2(3.14);

Output

0.11344

ตัวอย่าง

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

int main() {
    double var = -2.34;
    double hold = exp2(var);
    cout << "Value of exp2(" << var << ") is: " << hold << endl;
    return 0;
}

ผลลัพธ์

Value of exp2(-2.34) is: 0.19751

ตัวอย่าง

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

int main() {
    int var = 10;
    int hold = exp2(var);
    cout << "Value of exp2(" << var << ") is: " << hold << endl;
    return 0;
}

ผลลัพธ์

Value of exp2(10) is: 1024

ตัวอย่าง

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

int main() {
    int var = 1/0;
    int hold = exp2(var);
    cout << "Value of exp2(" << var << ") is: " << hold << endl;
    return 0;
}

ผลลัพธ์

Floating point exception (core dumped)