Advanced C++ with boost library

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 range beyond the long long, long double data type (264) in C++.


  • Please refer this Article for installation of boost. We can download the zip file. After that we just need to extract the whole in specified folder of gcc or we can do this easily by command prompt.
  • If you have a codeblock compiler, then here is very nice article with full explanation.

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 one, we can achieve precision up to 1024 easily.
    Below C++ implementation code of finding the product of large numbers.

    #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 =
  2. Arbitrary precision data type: We can use any precision with the help of cpp_int data type if we are not sure about how much precision is needed in future. It automatically converts the desired precision at the Run-time.
    Below implementation of C++ code for finding the factorial of 30.

    #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;


    Factorial of 30 = 265252859812191058636308480000000
  3. Multiprecsion 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 Area of circle with different precision by using float, decimal and cpp_float_50 type.

    #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 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: "
             << 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 or mail your article to 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.

GATE CS Corner    Company Wise Coding Practice

Writing code in comment? Please use, generate link and share the link here.