Updated April 1, 2023
Introduction to C++ Boost
The support for tasks and structures like generation of a pseudorandom number, linear algebra, working with regular expressions, multithreading, unit testing etc., is provided in C++ programming language by using a set of libraries called a boost of one hundred and sixty-one separate libraries. It is a handy library widely used in different sections with a huge number of applications. Using boost libraries with the data type of big integer, a precision of one thousand twenty-four is obtained easily. An example of using a boost library is when we are to handle very, very long numbers whose range crosses the long double data type in C++.
Syntax of Boost Libraries in C++:
(Big_integer_datatype) firstlargenum * secondlargenum;
Where Big_integer_datatype can be either int128_t, int256_t, int512_t or int1024_t datatype and firstlargenum and secondlargenum are two long, long numbers which are to be multiplied with each other.
cpp_int_datatype variablename;
Where cpp_int_datatype is the arbitrary precision data type which we use when we are not certain about what precision is needed in the future.
Working of Boost Libraries in C++
- The support for tasks and structures like generation of a pseudorandom number, linear algebra, working with regular expressions, multithreading, unit testing etc., is provided in C++ programming language by using a set of libraries called boost.
- The boost libraries in C++ consist of one hundred and sixty-one separate libraries. It is a handy library widely used in different sections with a huge number of applications.
- On using boost libraries with the data type of big integer, a precision of one thousand twenty-four is obtained easily.
- The boost libraries can be used with arbitrary precision data types when we are not certain about what precision is needed in the future.
Examples of C++ Boost
Given below are the examples of C++ Boost:
Example #1
C++ program to demonstrate boost libraries to multiply two large numbers whose range crosses the double data type range in C++.
Code:
//the iostream and boost libraries are included to be able to make use of cin, cout and multiply very very long integers
#include <iostream>
#include <boost/multiprecision/cpp_int.hpp>
using namespace boost::multiprecision;
using namespace std;
//main method is called
int main()
{
//two very very long integers are stored in two respective variables called first and second and then multiplied with each other and stored in a variable of type big data integer type prod and then their product is displayed as the output on the screen
long long first = 123456789987654321;
long long second=987654321123456789;
int128_t prod = (int128_t) first * second;
cout << "The product of the given two long long integers is:" << "\n" << prod; return 0;
}
Output:
In the above program, the iostream and boost libraries are included to be able to make use of cin, cout and multiply very, very long integers. Then the main method is called within which two very, very long integers are stored in two respective variables called first and second and then multiplied with each other and stored in a variable of type big data integer type prod and then their product is displayed as the output on the screen.
Example #2
C++ program to demonstrate boost libraries to multiply two large numbers whose range crosses the double data type range in C++.
Code:
//the iostream and boost libraries are included to be able to make use of cin, cout and multiply very very long integers
#include <iostream>
#include <boost/multiprecision/cpp_int.hpp>
using namespace boost::multiprecision;
using namespace std;
//main method is called
int main()
{
//two very very long integers are stored in two respective variables called first and second and then multiplied with each other and stored in a variable of type big data integer type prod and then their product is displayed as the output on the screen
long long first = 567894321432156789;
long long second= 123498765123498765;
int128_t prod = (int128_t) first * second;
cout << "The product of the given two long long integers is:" << "\n" << prod; return 0;
}
Output:
In the above program, the iostream and boost libraries are included to be able to make use of cin, cout and multiply very, very long integers. Then the main method is called within which two very, very long integers are stored in two respective variables called first and second and then multiplied with each other and stored in a variable of type big data integer type prod and then their product is displayed as the output on the screen.
Example #3
C++ program to demonstrate boost libraries to multiply two large numbers whose range crosses the double data type range in C++.
Code:
//the iostream and boost libraries are included to be able to make use of cin, cout and multiply very very long integers
#include <iostream>
#include <boost/multiprecision/cpp_int.hpp>
using namespace boost::multiprecision;
using namespace std;
//main method is called
int main()
{
//two very very long integers are stored in two respective variables called first and second and then multiplied with each other and stored in a variable of type big data integer type prod and then their product is displayed as the output on the screen
long long first = 1023847560192837465;
long long second= 128374651029384756;
int128_t prod = (int128_t) first * second;
cout << "The product of the given two long long integers is:" << "\n" << prod; return 0;
}
Output:
In the above program, the iostream and boost libraries are included to be able to make use of cin, cout and multiply very, very long integers. Then the main method is called within which two very, very long integers are stored in two respective variables called first and second and then multiplied with each other and stored in a variable of type big data integer type prod and then their product is displayed as the output on the screen.
Recommended Articles
This is a guide to C++ Boost. Here we discuss the introduction, working of boost libraries in C++ along with examples, respectively. You may also have a look at the following articles to learn more –