Page 1 of 3 The idea of unlimited precision arithmetic is amazing and Python has it as standard. What is bignum and how does it work explained in this extract from my book, Programmer's Python: Everything is Data.
Programmer's Python Everything is Data
Is now available as a print book: Amazon
Contents
 Python – A Lightning Tour
 The Basic Data Type – Numbers
Extract: Bignum
 Truthy & Falsey
 Dates & Times
 Sequences, Lists & Tuples
 Strings
Extract Unicode Strings
 Regular Expressions
 The Dictionary
Extract The Dictionary ***NEW!!!
 Iterables, Sets & Generators
Extract Iterables
 Comprehensions
Extract Comprehensions
 Data Structures & Collections
 Bits & Bit Manipulation
 Bytes
Extract Bytes And Strings Extract Byte Manipulation
 Binary Files
 Text Files
 Creating Custom Data Classes
 Python and Native Code
Extract Native Code Appendix I Python in Visual Studio Code Appendix II C Programming Using Visual Studio Code
<ASIN:1871962765>
<ASIN:1871962749>
<ASIN:1871962595>
<ASIN:187196265X>
The Basic Data Type – Numbers
Every computer language has data types that are simple and are used to build all of the other data types. Python partly follows the pattern of having simple numerical types and a simple string type, but it also has some surprises if you are relying on it to be like every other language you have encountered. In particular, its approach to numeric values is very different. Numbers are where it all begins and so this is a good place to start our look at basic types.
Int, Float and Complex
There are three builtin numeric types – int, float and complex. Python’s int is very different from what you will find in other languages, but its float is fairly standard. The complex type isn’t at all complex as long as you know about complex numbers, but it is unusual to have it as a builtin type.
To start at the beginning, let’s ask why we have ints and floats at all? Beginners often have trouble seeing that there is a difference between:
myVar = 42
and
myVar = 42.0
From the point of view of what they mean then there really is no difference, but they are generally stored in different ways. This is the reason why in other languages numeric variables are assigned either the type of int(eger) or float. In Python variables don’t have types and mVar can equally well reference 42 as an int or as a float, but the objects that are referenced are very different.
The int type cannot have a fractional part – there is no provision for it in the way it is represented internally.
The float type always has a fractional part, even if it is zero.
As far as possible Python tries to hide the distinction from you, but occasionally you need to notice it if you hope to get an appropriate answer.
If you write a number with a decimal point then Python assumes that it is a float. If the number doesn’t have a decimal point it is represented as an int.
All Python floats are based on C’s double precision type, which on most machines is an 8byte, 64bit IEEE 754 floating point number. As such there is very little to say that is Pythonspecific. It is worth pointing out that floating point numbers, and binary representations in general, often fail to represent decimal fractions as you might naively suppose. The best known such problem is:
print(0.1 + 0.1 + 0.1)
which prints:
0.30000000000000004
The reason for the discrepancy is that 0,1 cannot be represented exactly in binary in the same way that 1/3 cannot be represented exactly in decimal the best we can do is 0.3333.. repeating the 3 until we run out of precision. This sort of problem isn’t just surprising it is capable of causing algorithms to fail in unexpected ways. For example:
f = 0
for i in range(0,1000):
f = f+0.1
print(f)
should print 100, but in fact prints:
99.9999999999986
You can imagine the problems caused if you try to test f==100.0. In general you should avoid testing floating values for equality and recast the condition in terms of greater than or equals, or less than or equals, to allow for inaccuracies due to floatingpoint arithmetic.
Python’s complex numbers are essentially a pair of floats complete with suitable operations. If you understand complex numbers you will have no difficulty working with them and this isn’t the place to explain how the math works. To create a complex value you can use the complex constructor or you can write literals using j as the imaginary number. For example:
z = 1.0 + 10.0j
z = z*2 + 42.0j
print(z)
Once you have a complex number you can use it in arithmetic expressions and the operations will all be complex, along with the final result. If you are wondering why j is used in place of the more commonly encountered i, it is because Python follows the practice in engineering to avoid confusion with i for current. You can also use a range of different constructors to create complex numbers.
For example:
z = complex(1.0,10.0)
z = z*2+complex("42.0j")
print(z)
You can consult the documentation to find out about ways of creating complex numbers.
