Skip to main content

Command Palette

Search for a command to run...

When Decimal is better than float(Python)

Published
4 min read
K

Mechatronics Engineer || Self-taught Python Developer || AI/ML Enthusiast || Tech nerd

How it began?

This week I was dealing with something that required me to store data in AWS Dynamo and I found something interesting which lead me to write this piece of article.

Here is what happened?

I was trying to store float values to the aws dynamo table but it didn't well it raised a Type Error that inexact numeric for data I was trying to solve.

Understood why (0.1+0.2) != 0.3

After a bit of googling, I came to realize that python does not store exact floating numbers but with tons of decimal other decimal precision(this has to do with the design of the language itself)

Here an example

>>> 1.1 + 1.3
2.4000000000000004

what can we do now?

Python has got you covered, there is a built-in module called decimal which is built on top of float-point data types allowing you to have exactly decimal numbers

Here an example

>>> from decimal import Decimal
>>> Decimal(0.1)
Decimal('0.1000000000000000055511151231257827021181583404541015625')

As we can see 0.1 in python is nothing but 0.1 with tons of decimals, to express 0.1 as 0.1 we have to pass it as string to our Decimal function just as shown below;

>>> from decimal import Decimal
>>> Decimal('0.1')
Decimal('0.1')
>>> Decimal('0.1') + Decimal('0.2') == Decimal('0.3')
True

As we can see now 0.1 is presented in exact values, and with this trick I was able to effictively store my float values in the aws lambda smoothly.

Other areas to use Decimal instead of float ?

  1. Accounting Application

When you're dealing with accounting calculation which is probably going to involve calculation with decimal point use Decimal points instead of float values for more accurate and precision in your financial calculation

  1. Converting Decimal to Fraction

When you're writing a program which in some ways do the conversion of decimal numbers to integer fraction, you don't wanna use float, it will screw you up;

Just to put it into action try converting a float value 0.35 to integer fraction

>>> (0.35).as_integer_ratio()
(3152519739159347, 9007199254740992)

Using decimal instead

>>> Decimal('0.35').as_integer_ratio()
(7, 20)

Well that's all I have got for today, you can keep in touch with me on Twitter kalebu for more tricks

Gota any other places you think we should be using Decimal instead of float, Please share in the comment box below;

K

I find that this article is extremely helpful and informed, and it is quite instructive. baldi's basics

A

I think this is an informative post and it is very beneficial and knowledgeable. mario games

A

Python's float type is used to store values that are calculated using the floating-point model. In computer memory, float values are composed of a mantissa and an exponent.

M

Python's decimal module provides support for fast correctly-rounded decimal floating point arithmetic. The decimal module is especially useful for computations involving money and for other applications where exact decimal representation is required.

The float type in Python represents a floating point number. Float values are represented in computer memory by a mantissa and an exponent. The mantissa stores the significant digits of the number while the exponent represents the power of 10 by which the mantissa must be multiplied to obtain the actual value.

While floats are accurate to approximately six or seven decimal places, they can sometimes give unexpected results due to rounding errors. Decimals, on the other hand, have a fixed number of decimal places and no rounding errors will occur. For this reason, decimals are often used for financial calculations.

mybkexperience mykfcexperience mybpcreditcard

C

Thank you so much for these helpful comments! happy wheels unblocked

More from this blog

K

Kalebu Jordan

35 posts

Hi there,

I'm Kalebu, Former Mechatronics Engineering student working as a Software Engineer focusing mainly on python stack based on BackEnd Developement, IoT , Opensource and Cloud Computing