Advanced C++ with Boost Library - GeeksforGeeks

27 Dec, 2021
Boost Libraries are intended to be widely useful, and usable across a broad spectrum of applications. For example, they are helpful for handling large numbers having a range beyond the long long, long double data type (264) in C++. 
Installation
Please refer to this Article for the installation of boost. We can download the zip file. After that, we just need to extract the whole in a specified folder of gcc or we can do this easily by command prompt.
Example Applications
We can efficiently use this library in Competitive Programming but before this, we must ensure that your online judge must support boost. Here are some cool tricks that you can use: 
1) Big Integer Data Type: We can use either int128_t, int256_t, int512_t, or int1024_t data type according to your requirement. By using these ones, we can achieve precision up to 1024 easily. 
Below C++ implementation code for finding the product of large numbers:
CPP
// CPP Program to demonstrate Big Integer data type#include <boost/multiprecision/cpp_int.hpp>using namespace boost::multiprecision;using namespace std; int128_t boost_product(long long A, long long B){    int128_t ans = (int128_t)A * B;    return ans;} int main(){    long long first = 98745636214564698;    long long second = 7459874565236544789;    cout << "Product of " << first << " * " << second         << " = \n"         << boost_product(first, second);    return 0;}
Product of 98745636214564698 * 7459874565236544789 = 
736630060025131838840151335215258722
2) Arbitrary Precision Data Type: We can use any precision with the help of the cpp_int data type if we are not sure about how much precision is needed in the future. It automatically converts the desired precision at the Run-time. 
Below implementation of C++ code for finding the factorial of 30. 
CPP
// CPP Program to demonstrate Arbitrary precision data type#include <boost/multiprecision/cpp_int.hpp>using namespace boost::multiprecision;using namespace std; cpp_int boost_factorial(int num){    cpp_int fact = 1;    for (int i = num; i > 1; --i)        fact *= i;    return fact;} int main(){    int num = 30;    cout << "Factorial of " << num << " = "         << boost_factorial(num);    return 0;}
Output: 
Factorial of 30 = 265252859812191058636308480000000
3) Multiprecision Float: With Boost Multiprecision float, we can achieve precision up to 50 and 100 decimal with cpp_float_50 and cpp_dec_float_100 respectively. 
Below is C++ code to calculate the area of a circle with different precision by using a float, decimal, and cpp_float_50 types:
CPP
// CPP Program to demonstrate Boost Multiprecision float#include <boost/math/constants/constants.hpp>#include <boost/multiprecision/cpp_dec_float.hpp>#include <iostream> using boost::multiprecision::cpp_dec_float_50; using namespace std; template <typename T> inline T area_of_a_circle(T r){    // pi represent predefined constant having value    // 3.1415926535897932384...    using boost::math::constants::pi;    return pi<T>() * r * r;} int main(){    float radius_f = 123.0 / 100;    float area_f = area_of_a_circle(radius_f);     double radius_d = 123.0 / 100;    double area_d = area_of_a_circle(radius_d);     cpp_dec_float_50 r_mp = 123.0 / 100;    cpp_dec_float_50 area_mp = area_of_a_circle(r_mp);     // numeric_limits::digits10 represent the number    // of decimal digits that can be held of particular    // data type without any loss.     // Area by using float data type    cout << "Float: "         << setprecision(numeric_limits<float>::digits10)         << area_f << endl;     // Area by using double data type    cout << "Double: "         << setprecision(numeric_limits<double>::digits10)         << area_d << endl;     // Area by using Boost Multiprecision    cout << "Boost Multiprecision: "         << setprecision(                numeric_limits<cpp_dec_float_50>::digits10)         << area_mp << endl;    return 0;}
Float: 4.75292
Double: 4.752915525616
Boost Multiprecision: 4.7529155256159980531876290929438093413108253981451
This article is contributed by Shubham Bansal. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 
anshikajain26
cpp-advanced
cpp-boost
C++
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Vector in C++ STL
Initialize a vector in C++ (6 different ways)
std::sort() in C++ STL
Socket Programming in C/C++
Bitwise Operators in C/C++
Virtual Function in C++
Templates in C++ with Examples
rand() and srand() in C/C++
unordered_map in C++ STL
Friend class and function in C++