Skip to main content
Library homepage
 

Text Color

Text Size

 

Margin Size

 

Font Type

Enable Dyslexic Font
Mathematics LibreTexts

3.3: Extending integers, the BigInt type

( \newcommand{\kernel}{\mathrm{null}\,}\)

In Chapter 8, we will explore prime numbers and it is common for them to exceed the maximum allowable Int64 or even Int128. If this is needed, there is a type called BigInt with no maximum or minimum. The number googol is 10100 which these two integer types will not hold.  However, if we enter

Login with LibreOne to run this code cell interactively.

If you have already signed in, please refresh the page.

big(10)^100
big(10)^100
10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

which is 1 followed by 100 zeros is awfully big. Note: the command big creates a BigInt and generally normal operations with integers result in BigInts as well. Note: if you think there was a typo with googol, google "googol". 

It’s very important to understand how big(10)^100 works. First a number of type BigInt is made with a value of 10. Then that is raised to the 100th power. As noted ear- lier, a BigInt doesn’t have a upper or lower limit on the number. It can grow as needed.

Trying entering big(10^100)below. 

Login with LibreOne to run this code cell interactively.

If you have already signed in, please refresh the page.

 
 
 

 

The result is 0, which should be surprising, however note that because of order of operations, first 10^100 is calculated in standard Int64 and then turned into a BigInt. Again, for details on what happens here, look at Appendix C, in short this continually multiplies the number ten, 100 times which results in overflow and as such results in 0.

It is recommended only to use a BigInt if needed. As we will see in chapter 8, operations with them are significantly slower than Int64 or even Int128. Under few cases do you need to do this.  For example, in that later chapter with prime numbers.

 

The BigFloat Datatype

Let’s revisit an example from section XXXX  and sum 1/9 nine times.  Recall that

Login with LibreOne to run this code cell interactively.

If you have already signed in, please refresh the page.

1/9+1/9+1/9+1/9+1/9+1/9+1/9+1/9+1/9
1/9+1/9+1/9+1/9+1/9+1/9+1/9+1/9+1/9
1.0000000000000002

which shows the limitation of the floating-point type.  The BigFloat datatype is more flexible. However, note that 

Login with LibreOne to run this code cell interactively.

If you have already signed in, please refresh the page.

a=big(1/9)
a=big(1/9)
0.111111111111111104943205418749130330979824066162109375

which seems to have an accuracy of only 17 digits, which is typical for a 64-bit floating point, so it doesn’t appear to have improved anything. This, like above, is a case of being careful in constructing a BigFloat. What happens with big(1/9)? Put on your order-of-operations hat and take a look. The 1/9 is done first and since both 1 and 9 are regular integers (Int64), the result is a Float64. Then the big function turns the Float64 into a BigFloat, but not we the accuracy expected.

Instead, if we define 

Login with LibreOne to run this code cell interactively.

If you have already signed in, please refresh the page.

a=big(1)/big(9)
a=big(1)/big(9)
0.1111111111111111111111111111111111111111111111111111111111111111111111111111109

which looks more like an expected result. To determine the number of digits of accuracy, you can count (painfully) or try 

Login with LibreOne to run this code cell interactively.

If you have already signed in, please refresh the page.

length(string(a))
length(string(a))
81

which is about 5 times the accuracy of Float64. Note: looking again at order of operations, the command length(string(a)) first takes the number a and returns it as a String. Then working inside to outside, find the length of the string.

The precision function used on a variable of type BigFloat will tell the precision of the variable. Thus

Login with LibreOne to run this code cell interactively.

If you have already signed in, please refresh the page.

precision(a)
precision(a)
256

which is the number of bits of precision and it has 4 times the binary precision of Float64 but about 5 times the decimal precision.

 

As noted at the beginning of this section, though, if we want to compute π to 1 million decimal digits, what we’ve seen so far only has about 80 digits of accuracy.  As a bit of an enticement, we will do this in Chapter XXX. 

The BigFloat type is quite flexible. The above example used it in its default precision. We can change this with the setprecision function. For example:

Login with LibreOne to run this code cell interactively.

If you have already signed in, please refresh the page.

  setprecision(2^10)
  setprecision(2^10)
1024

showing that now BigFloats will be stored in this many bits. Such a number will using 16 times the number of binary digits as a Float64. Entering 

Login with LibreOne to run this code cell interactively.

If you have already signed in, please refresh the page.

a2=big(1)/big(9)
a2=big(1)/big(9)
0.111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111

a number that, at a glance, looks to store about 4 times the number of digits as the default BigFloat size. To determine the total number of decimal digits, if we enter

Login with LibreOne to run this code cell interactively.

If you have already signed in, please refresh the page.

length(string(big(a2)))
length(string(big(a2)))
311

 as the number of decimal digits. This is about 20 times the precision of Float64.


This page titled 3.3: Extending integers, the BigInt type is shared under a CC BY-NC-SA license and was authored, remixed, and/or curated by Peter Staab.

Support Center

How can we help?