C++Introduction to C++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.

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

Now you can use multiprecision namespace as below.

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

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

Or you can declare safely as below.

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

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.

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.

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

Learn How To Work With Very Large Real Numbers 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++17Language FeatureLearn C++

How To Use The SVG Image Format With Skia In C++ Builder

C++C++17Language FeatureLearn C++

How To Use Skia Images in C++ Builder?

C++C++17Code SnippetGame DevelopmentLanguage FeatureLearn C++

What Is Skia In Modern C++?

C++C++17Learn C++

How To Use Skia in C++ Builder 12?