FAQ: Modules: Python - Modules Python Decimals

Maybe I’m missing the point of this lesson, but the solution doesn’t output what I would expect. Why isn’t the below more correct?

# Import Decimal below:
from decimal import Decimal

# Fix the floating point math below:
two_decimal_points = round(Decimal(0.2 +0.69), 2)
print(two_decimal_points)

four_decimal_points = round(Decimal(0.53 * 0.65), 4)
print(four_decimal_points)

1 Like

The idea would seem to be, operate on the Decimal objects.

three_decimal_points = Decimal('0.2') * Decimal('0.69')
print(three_decimal_points)

four_decimal_points = Decimal('0.53') * Decimal('0.65')
print(four_decimal_points)
0.138
0.3445

Note how we gave string objects to Decimal.

Thanks for your response. I wasn’t making the numbers into strings using the pure Decimal way, so I was getting a much longer number.

2 Likes

You’re welcome. These objects definitely require some in depth study in order to gain a full understanding of their usage, especially optimal usage. SigDigs is one area that is worthy of consideration. Decimal objects do not require a round() function since they have a precision attribute. It’s been a long while since I first tried to explore this class. In my limited clime there were not a lot of use cases and it fell to the wayside (but not forgotten).

Explore the class and bring forward any other observations or use cases that you envision.

SERP

what are possible use cases of the Decimal class py - Google Search

2 Likes

Why do we need to convert the integers to string in order for Decimal to work? And why does Python return those weirdly formatted numbers without decimal in the first place?

Because the goal of Decimal to create numbers and do math that more closely resembles how humans think through operations it is important to distinguish it from ordinary integers, and more importantly from ordinary floats. The only way to do this is to make each object a string, and let the decimal module sort things out.

Why use decimal instead of the built_in function round?

round is a general purpose function that does not take into account significant figures (sigdigs). Of course there is much more to it than that. Decimal objects are strings, rather than floats, which may contribute to faster processing (needs to be checked).

How does it know how many decimal points to do? It’s not because it’s written. This course is becoming worse and worse with each module. Why charge us so much money for a wiki entry on what the module is about? I learned nothing here from the course content.

From an amateur point of view, I found and still do find the Decimal module a little more than confusing. However, I expect that judicious use of the module might well fit an environment where decimal constraints are strictly conformed with, such as significant figures in science calculations. Bottom line, this is just one blip that most learners experience. Keep going. It’s how you will get your money’s worth as long as you see this as an investment in yourself.

HI, two_decimal_points = Decimal(‘0.2’) + Decimal(‘0.69’) Should we use single quotes for number?

1 Like

This is decimal multiplication. The number of decimal places is the summation of the decimal places.

I don’t get why the Decimal library is necessary. Why not just use round()?

The Decimal library is a course in itself. It won’t make complete sense unless one is constrained by ‘significant figures’ or ‘significant digits’ or sigdigs, for short.

Under the rule we can only carry one extra digit in subsequent operations and must resolve to number of digits of the least.

>>> from math import pi
>>> print (2 * pi)
6.283185307179586
>>> 

2 is the significant value upon which the constraint is based, one digit, 6. With slight lenience we might get away with, 6.3, which depends upon how the rule is interpreted.

No, I’m not going to get into the rule. If you are studying most kinds of science this will be moot, part of the introductory level. In maths it does not come up so will be a foreign idea. It will be up to the reader to follow this up.

what would be wrong wth doing this?


st = str(two_decimal_points)
ln = len(st)
sl = st[2:ln]
it = int(sl)
it += 1
st = str(it)
sp = st.split("0")
st = "0." + sp[0]

When I use this code, I get did you use Decimal to fix four decimal points? Here is my attempt at code:

four_decimal_points = Decimal(‘0.53’) * Decimal(‘0.65’)
print(Decimal(four_decimal_points))

The outcome is already a Decimal instance:

>>> print (four_decimal_points)
Decimal('0.3445')
>>> print (float(four_decimal_points))
0.3445
>>>
1 Like

This is embarrassing, but I read the problem wrong. The answer was decimal without the float; however, I duplicated the problem and did the duplication; ergo, there were problems with my code.

That was the real problem and I am greatly embarrassed for my dupliction.

1 Like

Don’t be. There is virtually nothing we can do when learning a programming language or logic and algorithms that would warrant embarrassment. Go easy on yourself and accept every mistake as a lesson partly learned. As long as your code runs, even if it has duplication, it is at least somewhere near correct. Again, something to learn from as we go.

Delight in every mistake, or perceived mistake. Guys like me will always be there to give you food for thought or minor insights. It means nothing more than two colleagues sharing across the learning space. We’re not here to point out mistakes, only critique the work in hopes it helps make it better.

Bottom line, welcome mistakes, and welcome criticism without beating up yourself or taking the critique personally. Be good to yourself.