Site icon Learn C++

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

Learn How To Work With Very Large Real Numbers

C++ is a superb programming language that has rich collection of very useful libraries. One of the most common problems in programming is limitations of the types of numbers that we use. If you are about to calculate very big, large real numbers (i.e. factorial 100!) the Boost C++ Libraries can be very useful in avoiding issues and help make things much easier. Boost allows you to use very large real numbers with most C++ compiler versions.

In this post we explain how to work with very large real numbers (floating point numbers) in C++.

What are the limits of variables for very large real 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 consider what the minimum and maximum ranges could be. In most operations, these are less important, but if you carry out operations which breach the limits of the variable type you may have problems which can be very subtle and difficult to track down.

In C++, there are many float types. These can be found in Floating Point Constants docwiki section. For most uses float, double and long double are used. Their ranges are listed as below.

Type SpecifierSize Range
float2 Bytes1.17549e-38 to 3.40282e+38
double8 Bytes2.22507e-308 to 1.79769e+308
long double8, 12 or 16 Bytes3.3621e-4932 to 1.18973e+4932

Note that there is no unsigned type for floating point numbers. If your variable has values in the range shown, then one of the above types can be used. If you have larger numbers, or you need more precision, you should use the Boost library. Let’s see what Boost is and how you can install it.

What are the Boost C++ libraries and how to use them with very large Real numbers?

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 provides methods for the computational tasks and structures such as linear algebra, multithreading, image processing, regular expressions, pseudorandom number generation, and unit testing. Boost has libraries that work well with the C++ Standard Library. Boost works on almost any modern operating systems.

The first Boost 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 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 larger Real numbers in C++ Builder, just install Boost library (i.e. Boost 1.70) via Get-It. Select Tools > GetIt Package Manager, search ‘boost’ and select one of the Boost packages. Click Install to start the process.

Here,

How to work with very large Real numbers in C++?

If you properly installed the Boost library as listed above, you can use the Multiprecision Library from Boost. If you want to use very large Real numbers you need to use one of the cpp_dec_float, cpp_dec_float_50, cpp_dec_float_100 types. For those very large Real number types, you just need to add cpp_dec_float.hpp header in your code.

[crayon-6741db73b023c459161548/]

Now you can use multiprecision namespace as below.

[crayon-6741db73b0246894163438/]

Or you can directly use cpp_dec_float_50 for the 50 digits floating point numbers as below.

[crayon-6741db73b0249987858789/]

Or you can directly use cpp_dec_float_100 for the 50 digits floating point numbers as below.

[crayon-6741db73b024b150430983/]

Or you can declare safely as below.

[crayon-6741db73b024d002688524/]

You can directly create your own typedef for a custom digits (here we use 20 digits) as below.

[crayon-6741db73b024f274537900/]

Is there a simple example of how to work with very large Real numbers in C++?

Here is a simple example how you can use cpp_dec_float_50.

[crayon-6741db73b0251144829561/]

As you see here variable f is cpp_dec_float_50.

Is there a full example of how to work with very large real numbers in C++?

Here is a full example, about how to use with very large Real numbers.

[crayon-6741db73b0253193307794/]

In this example you can use cpp_dec_float_100 type or your custom cpp_float_dec type as we described above.

You can find more documents and examples about boost library online in;

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.

Exit mobile version