Big numbers
A Ring number is a double, so whole numbers are exact only up to about nine quadrillion. BigInteger keeps every digit of a whole number of any size, and Complex works with numbers that have an imaginary part.
What you will learn#
- the limit of a Ring number and why it exists,
BigInteger:Parse,Add,Multiply,Pow,Remainder,ModPow,GreatestCommonDivisor,Compare,- exact factorials and Fibonacci numbers,
Complex: the real and imaginary parts,Magnitude,Multiply.
(The tutorial Money used Decimal for exact decimals; this lesson is about exact large whole numbers.)
The program#
# numbers that do not fit - BigInteger for exact big whole numbers, Complex numbers and the limits of Ring numbers
load "dotnet.ring"
using {
"System"
"System.Numerics"
}
func main
# 1. A Ring number is a double: whole numbers are exact up to 9007199254740992 (2^53). After that, the last digits are lost.
? "1. Ring: 9007199254740992 + 1 = " + (9007199254740992 + 1) + " (the +1 is lost)"
# 2. BigInteger keeps every digit. It is made from a text, and its result is shown as a text.
oBig = new BigInteger()
oA = oBig.Parse("9007199254740992")
oB = oBig.Add(oA,oBig.One)
? nl + "2. BigInteger: 9007199254740992 + 1 = " + oB.ToString()
? " the operators of Ring work on these objects too: " + (oA + oBig.One).ToString() + ", and oA > 1 is " + (oA > oBig.One)
# 3. Factorials grow fast: 30! has 33 digits. The loop multiplies with BigInteger.Multiply
oFact = oBig.One
for n = 2 to 30
oFact = oBig.Multiply(oFact,oBig.Parse("" + n))
next
? nl + "3. 30! = " + oFact.ToString()
# 4. Power, remainder and the greatest common divisor. Pow takes the number and an exponent; ModPow does number^e mod m without
# ever building the huge number (this is the base of public-key cryptography).
oTwo = oBig.Parse("2")
? nl + "4. 2^100 = " + oBig.Pow(oTwo,100).ToString()
? " 2^100 mod 1000007 = " + oBig.Remainder(oBig.Pow(oTwo,100),oBig.Parse("1000007")).ToString()
? " ModPow(2, 100, 1000007) = " + oBig.ModPow(oTwo,oBig.Parse("100"),oBig.Parse("1000007")).ToString()
? " GCD(1071, 462) = " + oBig.GreatestCommonDivisor(oBig.Parse("1071"),oBig.Parse("462")).ToString()
# 5. Fibonacci with exact digits: fib(100) is 354224848179261915075
a = oBig.Zero
b = oBig.One
for i = 1 to 100
c = oBig.Add(a,b)
a = b
b = c
next
? nl + "5. fib(100) = " + a.ToString()
? " its digits: " + len(a.ToString())
# 6. Comparing: Compare answers -1, 0 or 1 (a text compare of digits would be wrong: "9" > "10" as text)
? nl + "6. Compare(2^100, 10^30) = " + oBig.Compare(oBig.Pow(oTwo,100),oBig.Pow(oBig.Parse("10"),30)) + " (1: the first is larger; -1 would mean smaller)"
# 7. Complex numbers: a real part and an imaginary part. Math with them is done by the static methods of Complex.
oC = new Complex()
oI = new Complex().@New([ :real = 0, :imaginary = 1 ]) # i
oMinusOne = oC.Multiply(oI,oI)
? nl + "7. i * i = " + oMinusOne.Real + " (real part), " + oMinusOne.Imaginary + " (imaginary part)"
oZ = new Complex().@New([ :real = 3, :imaginary = 4 ])
? " |3 + 4i| = " + oZ.Magnitude + ", 3+4i squared = " + oC.Multiply(oZ,oZ).ToString()1. Ring: 9007199254740992 + 1 = 9007199254740992.00 (the +1 is lost)
2. BigInteger: 9007199254740992 + 1 = 9007199254740993
the operators of Ring work on these objects too: 9007199254740993, and oA > 1 is 1
3. 30! = 265252859812191058636308480000000
4. 2^100 = 1267650600228229401496703205376
2^100 mod 1000007 = 698635
ModPow(2, 100, 1000007) = 698635
GCD(1071, 462) = 21
5. fib(100) = 354224848179261915075
its digits: 21
6. Compare(2^100, 10^30) = 1 (1: the first is larger; -1 would mean smaller)
7. i * i = -1 (real part), 0 (imaginary part)
|3 + 4i| = 5, 3+4i squared = <-7; 24>How it works#
The limit of Ring numbers#
A double stores 53 bits of precision. Whole numbers are exact up to 2^53 = 9007199254740992; above that, + 1 can be lost, as step 1 shows (it prints the same number).
BigInteger#
A BigInteger is a struct from System.Numerics. In Ring you create it from a text (Parse("9007199254740992")), calculate with the static methods (Add, Multiply, Pow, ...) and show it with ToString(). The operators of Ring work on these objects too (oA + oB, oA * oB, oA > oB), because the type defines them (step 2 shows + and >); the loops use the static methods, which also have Pow, ModPow and the rest. The first object is a handle to reach the static methods (new BigInteger()); One and Zero are ready-made values. Step 2 adds 1 to 2^53 and keeps the result exact.
Factorials, powers, Fibonacci#
The loops of steps 3 and 5 are the ordinary ones, with Multiply and Add in place of * and +. 30! has 33 digits and fib(100) has 21; the results are exact. Pow(number, exponent) takes a normal whole exponent. ModPow(number, exponent, modulus) computes the remainder of a huge power without ever building the huge number; public-key cryptography is built on it (the tutorial Protecting and signing messages). Step 4 shows that ModPow and Remainder(Pow(...)) give the same answer.
Comparing#
Compare big numbers with Compare: it answers -1, 0 or 1. Do not compare their texts: as texts "9" is larger than "10".
Complex numbers#
A Complex has Real and Imaginary parts. i is Complex(0, 1) and i * i is -1, which step 7 shows. It is made by the names of its parameters (new Complex().@New([ :real = 0, :imaginary = 1 ])). Magnitude is the distance from zero: 5 for 3 + 4i. Multiply, Add, Divide, Sqrt, Exp, Pow and the trigonometric functions are static methods. Functions such as Pow work with doubles inside and may give results like -6.999999999999998; use Multiply when you need exact whole values.
What you learned#
- Ring numbers are exact up to 2^53,
BigIntegeris created from a text and used through static methods,Pow,ModPow,GreatestCommonDivisor,Compare,ComplexwithReal,ImaginaryandMagnitude.
Exercises#
- Print 100! and count its digits.
- Find the first Fibonacci number with 100 digits.
- Write
isEven(oBig)withRemainder, and test it with 2^100 and 2^100 + 1. - Multiply two complex numbers by hand and check the result with
Multiply.