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

C++ ขั้นสูงกับไลบรารี Boost: การคำนวณตัวเลขขนาดใหญ่และความแม่นยำสูง

ไลบรารี Boost ของ C++ เป็นเครื่องมือที่ทรงพลังและหลากหลายการประยุกต์ใช้ โดยเฉพาะอย่างยิ่งในการจัดการตัวเลขขนาดใหญ่และการคำนวณที่ต้องการความแม่นยำสูง ซึ่งเกินขีดจำกัดของชนิดข้อมูลพื้นฐาน เช่น long long หรือ double บทความนี้จะแนะนำการใช้งานโมดูล Boost.Multiprecision สำหรับการทำงานกับจำนวนเต็มขนาดใหญ่ (Big Integers) และทศนิยมความแม่นยำถูกต้องตามต้องการ (Arbitrary Precision Floats)

1. ชนิดข้อมูลจำนวนเต็มขนาดคงที่ (Fixed-Precision Integers)

Boost มอบชนิดข้อมูลเช่น int128_t, int256_t, int1024_t ให้ใช้งานได้ทันที ช่วยให้คำนวณค่าที่ใหญ่มากได้อย่างแม่นยำถึง 1024 บิต ตัวอย่างต่อไปนี้แสดงการคูณจำนวนเต็มขนาดใหญ่สองจำนวน:

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

#include <iostream>
#include <boost/multiprecision/cpp_int.hpp>

using namespace boost::multiprecision;
using namespace std;

int128_t large_product(long long n1, long long n2) {
    int128_t ans = (int128_t)n1 * n2;
    return ans;
}

int main() {
    long long num1 = 98745636214564698;
    long long num2 = 7459874565236544789;
    cout << "Product of " << num1 << " * " << num2 << " = " 
         << large_product(num1, num2);
    return 0;
}

ผลลัพธ์

Product of 98745636214564698 * 7459874565236544789 = 736630060025131838840151335215258722

2. ชนิดข้อมูลความแม่นยำตามต้องการ (Arbitrary Precision Integer: cpp_int)

เมื่อขนาดของตัวเลขไม่สามารถคาดการณ์ได้ล่วงหน้า สามารถใช้ cpp_int ซึ่งจะปรับขนาดหน่วยความจำโดยอัตโนมัติขณะรันโปรแกรม เหมาะสำหรับการคำนวณแฟกทอเรียลของจำนวนมาก

ตัวอย่างโค้ด: คำนวณ 50!

#include <iostream>
#include <boost/multiprecision/cpp_int.hpp>

using namespace boost::multiprecision;
using namespace std;

cpp_int large_fact(int num) {
    cpp_int fact = 1;
    for (int i = num; i > 1; --i)
        fact *= i;
    return fact;
}

int main() {
    cout << "Factorial of 50: " << large_fact(50) << endl;
    return 0;
}

ผลลัพธ์

Factorial of 50: 30414093201713378043612608166064768844377641568960512000000000000

3. จำนวนทศนิยมความแม่นยำสูง (Multiprecision Floats)

สำหรับการคำนวณทศนิยมที่ต้องการความแม่นยำหลายตำแหน่ง (เช่น 50 หรือ 100 ตำแหน่ง) สามารถใช้ cpp_dec_float_50 หรือ cpp_dec_float_100 ตัวอย่างเปรียบเทียบความแม่นยำระหว่าง float, double และ Boost Multiprecision ในการคำนวณพื้นที่วงกลม:

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

#include <iostream>
#include <iomanip>
#include <limits>
#include <boost/multiprecision/cpp_dec_float.hpp>
#include <boost/math/constants/constants.hpp>

using boost::multiprecision::cpp_dec_float_50;
using namespace std;

template <typename T>
inline T circle_area(T r) {
    using boost::math::constants::pi;
    return pi<T>() * r * r;
}

int main() {
    float f_rad = 243.0f / 100;
    double d_rad = 243.0 / 100;
    cpp_dec_float_50 rad_mp = 243.0 / 100;

    float f_area = circle_area(f_rad);
    double d_area = circle_area(d_rad);
    cpp_dec_float_50 area_mp = circle_area(rad_mp);

    cout << "Float: " << setprecision(numeric_limits<float>::digits10) << f_area << endl;
    cout << "Double: " << setprecision(numeric_limits<double>::digits10) << d_area << endl;
    cout << "Boost Multiprecision Res: " 
         << setprecision(numeric_limits<cpp_dec_float_50>::digits10) << area_mp << endl;
    return 0;
}

ผลลัพธ์

Float: 18.5508
Double: 18.5507904601824
Boost Multiprecision Res: 18.550790460182372534747952560288165408707655564121

สรุป

ไลบรารี Boost.Multiprecision เป็นโซลูชันที่ครบครันสำหรับการคำนวณตัวเลขขนาดใหญ่และความแม่นยำสูงใน C++ ไม่ว่าจะเป็นจำนวนเต็ม (cpp_int, int128_t) หรือจำนวนจริง (cpp_dec_float_50, cpp_dec_float_100) ช่วยลดความซับซ้อนในการจัดการ overflow และ precision loss ได้อย่างมีประสิทธิภาพ