3.3: Extending integers, the BigInt type
- Page ID
- 53661
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 \(10^{100}\) which these two integer types will not hold. However, if we enter
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.
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
which shows the limitation of the floating-point type. The BigFloat
datatype is more flexible. However, note that
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
which looks more like an expected result. To determine the number of digits of accuracy, you can count (painfully) or try
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
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 \(\pi\) 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:
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
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
as the number of decimal digits. This is about 20 times the precision of Float64
.