C++ is a great programming language that has many useful libraries which help developers write all sorts of programs. One of the most common scientific problems in programming is the limitation of numbers that we use. If you are about to calculate very large numbers (i.e. factorial 100!) there are the Boost C++ Libraries which is a set of C++ libraries that you can use to help with computational operations. You can use very large numbers with Boost libraries with almost any C++ IDE and compiler.
In this post we explain how to work with very large Integer numbers in C++.
Table of Contents
What kind of variables can be used for very large integer numbers in C++?
Firstly, when you declare a variable as a programmer, you should think about its variable kind, and how big it could be. You need to take into account what the minimum and maximum ranges could be. In most operations, the exact choice of variable might not be too important, but for larger numbers it is important to understand the most appropriate choice to make to avoid overflow or limit errors which can sometimes be difficult to track down.
In C++, there are many integer types, these can be found in extended Integer Types docwiki section. For a short description, mainly short int
, int
, long int
and long long int
has these sizes in the memory and their ranges are listed as below,
Ranges of Int :
Type Specifier | Size | Range (Signed) |
short int unsigned short int | 2 Bytes | -32,768 to 32,767 0 to 65,535 |
int unsigned int | 4 Bytes | -2,147,483,647 to 2,147,483,647 0 to 4,294,967,295 |
long int unsigned long int | 4 Bytes | -2,147,483,647 to 2,147,483,647 0 to 4,294,967,295 |
long long int unsigned long long int | 8 Bytes | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 0 to 18, 446, 744, 073, 709, 551, 615 |
If you are sure your variable is a positive number, you can double your limit by using unsigned
types of these integer types. If you have more bigger and larger variables, you should use the Boost library.
What are the Boost C++ libraries and can we use them with very large integer numbers in C++?
Boost is a set of C++ libraries that contains 164 individual libraries (as of version 1.76) that significantly expand the C++ programming language using template metaprogramming. Boost libraries provide methods for specialized computational tasks and structures such as linear algebra, multithreading, image processing, regular expressions, pseudorandom number generation, and even unit testing. Boost has libraries that work well with the C++ Standard Library. Boost works on almost any modern operating system.
The first Boost library was released on 1 September 1999 and now it has 1.81.0 version released in 2022. Boost C++ libraries are designed to allow Boost to be used with both free and proprietary software projects and they are licensed under a scheme called the Boost Software License.
RAD Studio allows you to install a subset of Boost that has been fully tested and preconfigured specifically for C++Builder. RAD Studio supports different versions of Boost depending on the compiler that you use to build your application:
Boost C++ Library | Platform | Compiler | Boost Version |
Boost Win32 Classic Toolchain Boost Win32 Clang-enhanced Classic Toolchain | 32-bit Windows | BCC32 BCC32C | 1.39.0 1.70.0 |
Boost Win64 Toolchain | 64-bit Windows | BCC64 | 1.70.0 |
How to install the Boost C++ libraries?
If you want to use very large numbers in C++ Builder, just install Boost library (i.e. Boost 1.70) via Get-It. Just select Tools > GetIt Package Manager, search ‘boost’ and select one of the Boost packages. Click Install to start the process.
Here,
- The Boost Win32 Classic Toolchain C++ Libraries are for the classic toolchain for Win32 only C++ Builder applications. If you have older classic C++ Builder 32bit applications that uses classic compiler you need this library. If you want to modernize, you can use Boost for the modern Win32 and Win64 Clang-enhanced platforms.
- The Boost Win32 Clang enhanced toolchain C++ libraries are a set of C++ libraries that significantly expand the language using template metaprogramming. This is for the Win32 Clang-enhanced toolchain only.
- Boost C++ libraries for the Win64 are a set of C++ libraries that significantly expand the language using template metaprogramming. This is for the Wi64 platform only.
How to use the Boost libraries to work with very large Integer numbers in C++?
If you installed your proper Boost library as listed above, now you need to use cpp_int
type for those very larger integer numbers, you just need to add cpp_int.hpp
header in your code.
1 |
#include <boost/multiprecision/cpp_int.hpp> |
now you can use multiprecision namespace as below,
1 |
using namespace boost::multiprecision; |
or you can directly use cpp_int
as below,
1 |
boost::multiprecision::cpp_int x = 0; |
Is there a simple example of how to use very large integer numbers in C++?
Here is a simple example how you can use cpp_int
,
1 2 3 |
#include <boost/multiprecision/cpp_int.hpp> boost::multiprecision::cpp_int x = 0; |
for example, you can create your factorial function as below,
1 2 3 4 5 6 |
boost::multiprecision::cpp_int factorial(int n) { boost::multiprecision::cpp_int f = 1; for (int i=n; i>1; --i) f *= i; return f; } |
as you see here variable f
is cpp_int
and out factorial function is a cpp_int
function.
Is there a full example of how to use very large integer numbers in C++?
Here is a full example, of how to use very large integer numbers.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#include<iostream> #include <boost/multiprecision/cpp_int.hpp> boost::multiprecision::cpp_int factorial(int n) { boost::multiprecision::cpp_int f = 1; for (int i=n; i>1; --i) f *= i; return f; } int main() { std::cout << "50! = " << factorial(50) << std::endl; std::cout << "100! = " << factorial(100) << std::endl; system("pause"); return 0; } |
You can find more documents and examples about boost library online in;
and the latest version can be downloaded from https://www.boost.org or you can use C++ Builder compatible version via https://getitnow.embarcadero.com/?q=boost&product=rad-studio&sub=all&sortby=date&categories=-1
C++ Builder is the easiest and fastest C and C++ IDE for building simple or professional applications on the Windows, MacOS, iOS & Android operating systems. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs.
There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise versions of C++ Builder and there is a trial version you can download from here.