Python Tutorial: Python Decimal Module - Python Numbers #27

preview_player
Показать описание

Python 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.
Рекомендации по теме
Комментарии
Автор

One of the primary benefits of using the decimal module is the ability to avoid many floating point problems from the start. But using *Decimal(7.6)* creates a floating point approximation of 7.6 and then passes this approximation into the Decimal constructor--the precision is immediately lost. The example would benefit from using *Decimal("7.6")*, etc.--this creates a true 7.6 value that can be operated on _without_ fudging the result by lowering the decimal precision of the context:

>>> from decimal import Decimal
>>> Decimal('7.6') + Decimal('8.7')
Decimal('16.3')

There are no rounding issues to correct when you use precise values (the strings representations) to instantiate the Decimal objects. Yes, it's nice that the module gives us the tools to manage rounding issues but the ability to avoid them in the first place is the decimal module's real benefit.

sjbrown
Автор

that baby over there is feeling all floating points pain

MadHeadXL
Автор

Jesus I've been struggling with precision setting for so long thank you so much!

nastarankianersi
Автор

i dont get why at 9:37 2.67 was not good. if i were working with this number and tried to round it id be perfectly happy. if the original decimal is rounding down why should i interfere?

rukavzhoppe
Автор

Write a piece of python code that will take a decimal number eg.5263 and print out only the right-most digit. For example, if there is a number 5263, the code should print out 3?
Please any one can try this

lithishatannirru
Автор

num7 package is easier to use and with always best precision without context settings!

giovannicipriani
Автор

Are we mixing up significant figures with decimal places?

BN-hynd
Автор

Please how to use decimal in a loop for exemple to evaluate matrice multiplication and obtain a matrice with elements which contain 30 digit after the coma.

nguiyapassineo
Автор

well in case of importing decimal what's the difference between decimal and Decimal...what we do in decimal.Decimal(8.7) what's the difference?

shubhambhattacharya
Автор

Unresolved attribute reference "quantize' for class 'float'

raymondlee
Автор

hi, are there any inbuilt functions in Python to round the number to the integer in the negative direction. Such as, fix in Matlab. eg fix(7.6) = 7

abhideepsingh
Автор

Do we have any way, to set the precision as per required, through out the programme setting the PRECISION with GETCONTEXT is not at all a good idea and calling the QUANTIZE every time is not simple.. I WOULD LIKE TO THANK YOU FOR YOUR VALUABLE TUTORIALS ...THOUGH IAM FROM A REMOTE AREA YOUR TUTORIAL MADE THE JoB EASY

krishnasai
Автор

if we want to use decimals instead of int what would we type

dylan.
Автор

Why don't I use round() instead of quantizing?

arulmanisubramani
Автор

I found out that,
round(2.675, 2) gives 2.67; but
round(2.6751, 2) gives 2.68, and also round(2.6756, 2) gives 2.68.

What is reason behind this?

rohanghige
Автор

You have not been able to record a video without the cry of a child ??? Complete disrespect for the viewers of the channel.

ruziliakalyon
Автор

It´s the 21st. Century, 7.6 + 8.7 being stored as "16.29 999 999 999 999 7" is a bug, not an annoyance of the technology. Those that design computers need to fix the floating point bugs where possible, and standardize the behavior of the ones that are not fixable. Floating point behavior should be predicatable.

k.chriscaldwell
join shbcf.ru