Ada big numbers
last modified November 23, 2026
An Integer holds a value that fits in a machine word, and a
Long_Long_Integer adds a few more digits, but neither can hold
100 factorial. Ada 2022 includes Big_Integer, a signed integer
that grows as needed, and Big_Real, an exact rational number
built from two big integers. Both live in child packages of
Ada.Numerics.Big_Numbers. This article computes huge powers and
factorials, sums the digits of a 302-digit number, compares big values, and
prints one third as an exact fraction.
Integers without a limit
Big_Integer is a private type with the usual arithmetic
operators. A small value is created with To_Big_Integer, a long
decimal string with From_String, and To_String turns
it back into text. The exponentiation operator accepts a
Natural exponent, so N ** 20 is written exactly as
it is for an ordinary integer.
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Numerics.Big_Numbers.Big_Integers;
use Ada.Numerics.Big_Numbers.Big_Integers;
procedure Big_Intro is
N : constant Big_Integer := To_Big_Integer (9);
begin
Put_Line ("9 ** 20 =" & To_String (N ** 20));
Put_Line ("2 ** 100 =" & To_String (To_Big_Integer (2) ** 100));
Put_Line ("from text =" &
To_String (From_String ("123456789012345678901234567890")));
end Big_Intro;
920 is about 1.2 × 1019, already beyond a 64-bit
signed integer, and 2100 has 31 digits. The last line shows that
any length of decimal text can be read as long as it contains a valid
integer. The operators "+", "-",
"*", "/", "mod", "rem",
and "**" are all defined, together with the six comparisons and
the Min and Max functions.
$ gnatmake -gnat2022 -gnatwa big_intro.adb $ ./big_intro 9 ** 20 = 12157665459056928801 2 ** 100 = 1267650600228229401496703205376 from text = 123456789012345678901234567890
One hundred factorial
Factorial is the classic test of an arbitrary-precision integer: the result
grows by a factor of I on every step, and after a hundred steps
it has 158 digits. The accumulator starts at one and is multiplied by each
integer from one to a hundred. Each I is converted with
To_Big_Integer because there is no implicit conversion from
Integer.
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Numerics.Big_Numbers.Big_Integers;
use Ada.Numerics.Big_Numbers.Big_Integers;
procedure Factorial is
Result : Big_Integer := To_Big_Integer (1);
begin
for I in 1 .. 100 loop
Result := Result * To_Big_Integer (I);
end loop;
Put_Line ("100! = " & To_String (Result));
end Factorial;
The loop performs a hundred multiplications, and every intermediate value is
stored exactly. To_String follows the convention of
'Image and leaves a space where a minus sign could go, so the
printed line has two spaces after the equals sign. The value ends in
twenty-four zeros, one for every factor of ten among the numbers from one to a
hundred.
$ gnatmake -gnat2022 -gnatwa factorial.adb $ ./factorial 100! = 93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000
The digits of a power
Once a number is a string, ordinary string processing applies. The next
program computes 21000, trims the leading space from its image,
counts the digits, sums their values, and prints the first and last nine
digits. The character '0' has a position in
Character, so subtracting it from a digit character gives the
numeric value of that digit.
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Strings; use Ada.Strings;
with Ada.Strings.Fixed; use Ada.Strings.Fixed;
with Ada.Numerics.Big_Numbers.Big_Integers;
use Ada.Numerics.Big_Numbers.Big_Integers;
procedure Digit_Sum is
Power : constant Big_Integer := To_Big_Integer (2) ** 1000;
Text : constant String := Trim (To_String (Power), Left);
Sum : Natural := 0;
begin
for C of Text loop
Sum := Sum + Character'Pos (C) - Character'Pos ('0');
end loop;
Put_Line ("2 ** 1000 has" & Natural'Image (Text'Length) & " digits");
Put_Line ("digit sum =" & Natural'Image (Sum));
Put_Line ("first nine =" & Text (Text'First .. Text'First + 8));
Put_Line ("last nine =" & Text (Text'Last - 8 .. Text'Last));
end Digit_Sum;
The power has 302 digits and their sum is 1366, a result that would be
tedious to reach with a fixed-width integer. Every character in
Text is a digit, so the loop needs no classification test; the
program relies on To_String producing only decimal digits after
the leading space has been trimmed. The slices at the end use the first and
last positions of the string, which is why Text'Length and the
offsets are enough to print both ends.
$ gnatmake -gnat2022 -gnatwa digit_sum.adb $ ./digit_sum 2 ** 1000 has 302 digits digit sum = 1366 first nine =107150860 last nine =668069376
Comparing and reducing
Big values are compared with the same operators as ordinary numbers, and a
value that fits in an Integer can be converted back with
To_Integer. The modulo operator returns a big integer, and
Greatest_Common_Divisor works on two big values. The next program
uses all four operations on 2100 and 1030.
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Numerics.Big_Numbers.Big_Integers;
use Ada.Numerics.Big_Numbers.Big_Integers;
procedure Compare is
Small : constant Big_Integer := To_Big_Integer (42);
Huge : constant Big_Integer := To_Big_Integer (2) ** 100;
Limit : constant Big_Integer := To_Big_Integer (10) ** 30;
begin
Put_Line ("small as integer =" & Integer'Image (To_Integer (Small)));
Put_Line ("2 ** 100 > 10 ** 30 =" & Boolean'Image (Huge > Limit));
Put_Line ("2 ** 100 mod 1000 =" & To_String (Huge mod To_Big_Integer (1000)));
Put_Line ("gcd =" &
To_String (Greatest_Common_Divisor (Huge, Limit)));
end Compare;
2100 is larger than 1030, its last three digits are
376, and the greatest common divisor with 1030 is
230, or 1073741824, because the only prime factors of
1030 are two and five. An attempt to convert a value outside the
range of Integer with To_Integer raises
Constraint_Error; the program only converts 42, so the call is
safe.
$ gnatmake -gnat2022 -gnatwa compare.adb $ ./compare small as integer = 42 2 ** 100 > 10 ** 30 =TRUE 2 ** 100 mod 1000 = 376 gcd = 1073741824
Exact rationals
Big_Real is not a binary floating-point type. It stores a
numerator and a denominator, both Big_Integer values, and reduces
them to lowest terms, so one third is kept as the exact pair 1 and 3 rather
than as an approximation. Dividing two big integers with "/"
produces the rational value. To_Quotient_String prints the pair,
and To_String prints a decimal expansion with the requested
number of digits after the point.
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Numerics.Big_Numbers.Big_Integers;
use Ada.Numerics.Big_Numbers.Big_Integers;
with Ada.Numerics.Big_Numbers.Big_Reals;
use Ada.Numerics.Big_Numbers.Big_Reals;
procedure Rational is
Third : constant Big_Real :=
To_Big_Real (To_Big_Integer (1)) / To_Big_Real (To_Big_Integer (3));
begin
Put_Line ("exact = " & To_Quotient_String (Third));
Put_Line ("decimal = " & To_String (Third, Fore => 1, Aft => 30, Exp => 0));
end Rational;
The exact form is 1 / 3, printed with the leading spaces that
To_String gives to nonnegative big integers. The decimal form has
thirty threes, and it is still only an approximation: no finite decimal
expansion equals one third. Additions, subtractions, multiplications, and
divisions stay exact, so a long chain of rational operations loses nothing
until the final conversion to text.
$ gnatmake -gnat2022 -gnatwa rational.adb $ ./rational exact = 1 / 3 decimal = 0.333333333333333333333333333333
Next steps
The next article works with dates and times. It splits a time into year, month, day, hour, minute, and second, formats and parses the ISO 8601 form, adds durations across a year boundary, measures elapsed real time, and finds the weekday of a date. The fixed-point article explains the exact decimal arithmetic that a big rational generalizes, and scalar types introduces the ordinary integer and real types.
Source
In this article we have computed 920 and 2100, read a thirty-digit integer from text, calculated 100 factorial exactly, summed the digits of 21000, compared and reduced big values, and represented one third as an exact rational with a thirty-digit decimal expansion. The types article explains the machine-sized numeric types that these packages extend.
Author
List all Ada tutorials.