Programmer's Python Data - Bignum
Written by Mike James   
Monday, 02 May 2022
Article Index
Programmer's Python Data - Bignum
Bignum
How Does Bignum work?

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

pythondata360Contents

  1. Python – A Lightning Tour
  2. The Basic Data Type – Numbers
       Extract: Bignum
  3. Truthy & Falsey
  4. Dates & Times
  5. Sequences, Lists & Tuples
       Extract Sequences 
  6. Strings
       Extract Unicode Strings
  7. Regular Expressions
  8. The Dictionary
       Extract The Dictionary 
  9. Iterables, Sets & Generators
       Extract  Iterables 
  10. Comprehensions
       Extract  Comprehensions 
  11. Data Structures & Collections
  12. Bits & Bit Manipulation
         Extract Bits and BigNum ***NEW!!!
  13. Bytes
        Extract Bytes And Strings
        Extract Byte Manipulation 
  14. Binary Files
  15. Text Files
  16. Creating Custom Data Classes
        Extract A Custom Data Class 
  17. 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 built-in 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 built-in 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 8-byte, 64-bit IEEE 754 floating point number. As such there is very little to say that is Python-specific. 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 floating-point 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.



Last Updated ( Monday, 09 January 2023 )