ที่นี่เราจะดูว่าการใช้ ldexp() วิธีการใน C หรือ C ++ คืออะไร ฟังก์ชันนี้จะคืนค่าตัวแปร x ยกกำลังของค่า exp ต้องใช้สองอาร์กิวเมนต์ x และ exp
ไวยากรณ์เป็นเหมือนด้านล่าง
float ldexp (float x, int exp) double ldexp (double x, int exp) long double ldexp (long double x, int exp) double ldexp (T x, int exp)
เรามาดูตัวอย่างหนึ่งตัวอย่างเพื่อทำความเข้าใจกันดีกว่า
ตัวอย่าง
#include <iostream>
#include <cmath>
using namespace std;
int main() {
double a = 10, res;
int exp = 2;
res = ldexp(a, exp); // Finds a*(2^exp)
cout << "The result is = " << res << endl;
} ผลลัพธ์
The result is = 40
ตอนนี้ให้เราดูข้อผิดพลาดบางอย่างที่สามารถสร้างได้จากฟังก์ชันนี้ หากค่าที่ส่งกลับมีขนาดใหญ่เกินกว่าจะแสดงได้ ฟังก์ชันนี้จะคืนค่า HUGE_VAL
เรามาดูตัวอย่างกัน
ตัวอย่าง
#include <iostream>
#include <cmath>
using namespace std;
int main() {
double a = 10, res;
int exp = 5000;
res = ldexp(a, exp); // Finds a*(2^exp)
cout << "The result is = " << res << endl;
} ผลลัพธ์
The result is = inf