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

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

ฟังก์ชัน frexp() แยกตัวเลขทศนิยมออกเป็น Significant (Mantissa) และ Exponent ฐาน 2 โดยที่ x = significand * 2^exponent และ Significant อยู่ในช่วง [0.5, 1)

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

x = significand * 2^exponent

รูปแบบ

float frexp(float x, int* exponent);

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

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

int main() {
    double a = 4;
    int exp;
    cout << "Value of a : " << a << '\n';
    double s = frexp(a, &exp);
    cout << a << " = " << s << " * 2^" << exp;
    return 0;
}

ผลลัพธ์

Value of a : 4
4 = 0.5 * 2^3

การแยก: 4 = 0.5 × 2³ = 0.5 × 8 = 4