C++Introduction to C++Learn C++

Learn How To Work With Very Large Integer Numbers In C++

Learn How To Work With Very Large Integer Numbers In C++

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++.

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 SpecifierSize 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++ LibraryPlatformCompilerBoost Version
Boost Win32 Classic Toolchain
Boost Win32 Clang-enhanced Classic Toolchain
32-bit WindowsBCC32
BCC32C
1.39.0
1.70.0
Boost Win64 Toolchain64-bit WindowsBCC641.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.

What Are The Boost C++ Libraries The GetIt listing for the C++ Boost libraries

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.

now you can use multiprecision namespace as below,

or you can directly use cpp_int as below,

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,

for example, you can create your factorial function as below,

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.

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

Learn How To Work With Very Large Integer Numbers In C++The C++ Builder Logo

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.

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.

About author

Dr. Yilmaz Yoru has 35+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He graduated and received his MSc and PhD degrees from the Department of Mechanical Engineering of Eskisehir Osmangazi University. He is the founder and CEO of ESENJA LLC Company. His interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++11C++14C++17C++20Introduction to C++Learn C++

Learn Copy Constructors in C++ Classes

C++C++11C++14C++17Introduction to C++Learn C++Syntax

Learn How To Use Types Of Destructors In C++?

C++C++11C++14Learn C++Syntax

How To Convert u32string To A wstring In C++

C++C++11C++14C++17C++20Introduction to C++Learn C++

How To Learn The Move Constructors In Modern C++?