Saturday, June 13, 2015

Python Decimal Module - Python Numbers - Python Tutorial

Python Decimal ModulePython Decimal Module


In the previous two Python tutorials, we looked at issues that arise when working collaboratively with floating point numbers in the Python programming language. When working with floating point numbers we have seen that Python sometimes returns a value that we are not suspecting. This is doesn’t happen often but when we get a number that is a few ten thousandths off this can be concerning especially if you are working with numbers that need to have precise returns like a accounting program or a program that controls trajectory of a spacecraft. It would be concerning if we could not get the correct amount of money or if our spacecraft was off course by a tad bit we would probably miss the earth on the return and fly right into the sun.

In this tutorial, we are going to look at the Python decimal module which needs to import into your program for the functions to work. Decimals are basically floating point numbers but, they have fixed number of decimal points.  We can think of them as fixed-precision floating point numbers. We can also control how Python rounds the numbers unlike with the round() function where we saw if can round in the wrong direction.  With the decimal module, we will be capable of more precise numbers with a little bit more work.


Importing The Decimal Module


We have seen how to import a few modules in previous tutorials, in this tutorial we are going to follow the same steps. To make the decimal module to work we first need to import the module and we do this by entering the following “import decimal” with no quotes.  Once we have imported the Python decimal module we will be able to access the functions included in the module.


Importing The Decimal Module Example


#import the whole module. We will use this method in this tutorial

>>> import decimal

#import a specific function in the module
>>> from decimal import Decimal

In this Python decimal module, tutorial we will just use import decimal. This will make it easier to focus on the decimal module over importing specific functions.


Working With Python Decimal Module


First we need to look at an example where we would need to use the decimal module. Let’s look at the example we have been using over and over in our last two tutorials.


Why We Need Decimal Module Example


#below you will find two examples why we would need to use the decimal module

#answer should be 16.3
>>> 7.6 + 8.7
16.299999999999997

Ways To Fix This Using the Decimal Module


Method 1


In this example, we set a global precision on our decimal module and we may be limited in using this method. Take a look at how it works.


#first example

#issue
>>> 7.6 + 8.7
16.299999999999997

#import Decimal Module
>>> import decimal

#set the precision globally this tells decimal module we want 3 numbers returned.
>>> decimal.getcontext().prec = 3

#Now add the two numbers together using the decimal module
>>> decimal.Decimal(7.6) + decimal.Decimal(8.7)
Decimal('16.3')

In the above example, we take our problem floating point number and we use the decimal module to return the correct number. Let’s take a closer look at the process we use above.


Import Decimal Module


First we import the Python decimal module.  This needs to be done so we can use the functions the module contains.


Code: import decimal


Set Global Precision


When we set global precision the program to only show three numbers with our code.  Global means that our program will only show three numbers throughout our program or until we change it. This method may not be the best if you are working with numerous different numbers.


Code: decimal.getcontext().prec = 3


The Equation


Here we perform a simple addition equation.  We do have some words in front of our floats.  The first word decimal says go to the module decimal and then the second word Decimal says get the Decimal function which is inside the decimal module.


Code: decimal.Decimal(7.6) + decimal.Decimal(8.7)


Method 2


In this example, we will set the precision for our numbers temporarily so we will look at global with temporary precision adjustment.


#Temporary Precision

#import decimal module
>>> import decimal

#set global
>>> decimal.getcontext().prec = 3

#our equation for this example but we want the dollar and cents in this equation. We only get 3 numbers which are the dollars
>>> decimal.Decimal(78.96) + decimal.Decimal(90.99)
Decimal('170')

#temporary fix setting our precision for one equation
>>> with decimal.localcontext() as ctx:
... ctx.prec = 5
... decimal.Decimal(78.96) + decimal.Decimal(90.99)
...
Decimal('169.95')

#see only temporary
>>> decimal.Decimal(78.96) + decimal.Decimal(90.99)
Decimal('170')

Import Decimal Module


Is the same as above example


code: import decimal


Set Global Precision


Is the same as above example


Code: decimal.getcontext().prec = 3


The Equation


Here we have an equation and in this equation we want to find out how much a client owes you. This equation does not show cents and rounds up so your client is paying you more than is actually owed to you. This may not be the best choice when running a business


codedecimal.Decimal(78.96) + decimal.Decimal(90.99)


The Temporary Precision Solution


What in the world is that funky looking code. We are nowhere near learning about exceptions in Python but I thought I would share this one to show you the possibilities in Python and the decimal module. Let’s take a closer look “With” is an exception statement in Python. So, what I did here was say “with” the “decimal module” look inside the decimal module and get the “localcontext()” function and set this function “as” a variable called “cxt” so we can call it in the statement. The next line you must indent in your Python interpreter(I use two spaces) we set the precision using “cxt” variable like this cxt.precision = 5. After, we define the precision we can run our equation like before. Then in Python interpreter press return twice.


This also may not be the best solution for your program seems like a lot of code to do something simple.


Method 3


This is our final example in this tutorial. If you are looking for more precise decimal numbers then quantize may be our best option. We use quantize to return the exact amount of decimal numbers.


#using quantize in decimal module

#import decimal
>>> import decimal

#assign a variable an equation
>>> a = decimal.Decimal(78.96 * 89.65)

#Then use quantize to set exact decimal position
>>> a.quantize(decimal.Decimal('0.00'))
Decimal('7078.76')

Import Decimal Module


Same as before


code: import decimal


A Variable assigned To An Equation


Here we allocate a variable to an equation. We use a variable to cut down on the amount of code we need to type, but we could have done it all on one line. This is just simpler.  So with equation, we want to know the monetized amount so we want two numbers after the decimal point.


Quantize The Decimal


Here we use a method to round the number to a certain set of numbers after the decimal. We use a method called quantize to achieve this. We set how we want the method to return the value with ‘0.00’ it must be in a string format. If we want more numbers after the decimal for example we want four we could do ‘0.0000’ to make this happen.


Last Example


We also looked at another problem using the built-in function round().  round(2.675, 2) this function does not round as we expect so is there a way to fix this?  Of course there is. 


#Last Example Fixing Rounding Issues

#This should round up to 2.68
>>> round(2.675, 2)
2.67

#Lets take a look at what this number actually represents
>>> b = decimal.Decimal(2.675)
>>> b
Decimal('2.67499999999999982236431605997495353221893310546875')

#as you can see it rounds down because of the 4 instead of the five

#the fix
b.quantize(decimal.Decimal('0.00'), rounding=decimal.ROUND_UP)
Decimal('2.68')

In this example we once again use the quantize module but this time we use rounding to tell Python to round the number up.  We can use the following rounding commands ROUND_CEILING, ROUND_UP, ROUND_HALF_UP, ROUND_FLOOR, ROUND_DOWN, ROUND_05UP, ROUND_HALF_EVEN.


In Conclusion


We have taken a brief look at the Python decimal module. This is not a full tutorial on this module and we do have one planned for the future.  That tutorial will need to be broken down into several mini tutorials do the shear size of the Python decimal module. If you want to read more about the Python decimal module visit https://docs.python.org/3.4/library/decimal.html#module-decimal If you have any questions about today’s tutorial, please leave a comment below.



1 comment:

  1. Pretty article! I found some useful information in your blog, it was awesome to read, thanks for sharing this great content to my vision, keep sharing.
    Regards,
    Python Training in Chennai|Python Training Institutes in Chennai

    ReplyDelete