Python's integers (±0, 1, 2, etc.) have unlimited accuracy, which is unusual as far as programming languages go. In most languages they usually cap at 64-bits (aka long int) and there may be multiple integer types of various sizes. This makes Python very useful for scientific computing as you don't need a separate BigNum library to handle arbitrary precision. Python's floating-point numbers don't quite have this same luxury due to the inaccurate nature of them. Since many decimal numbers cannot be accurately represented in binary, floating-point math will always run into inaccuracies. For instance, 0.1 + 0.1 + 0.1 isn't 0.3, but something like 0.3000000000001. You'll use floats whenever whole numbers aren't enough, but you don't need a specific amount of accuracy. You probably never meant to ask about this, but decimal.Decimal is an alternative to float that lets you set its precision yourself. It's still not infinitely accurate, but it's often used in scientific computing where integers just can't cut it. Answer from Diapolo10 on reddit.com
🌐
Python
docs.python.org › 3 › library › decimal.html
decimal — Decimal fixed-point and floating-point arithmetic
Note Decimal.from_float(0.1) is ...51231257827021181583404541015625. ... From Python 3.2 onwards, a Decimal instance can also be constructed directly from a float....
🌐
TutorialsPoint
tutorialspoint.com › decimal-functions-in-python
Decimal Functions in Python
July 30, 2019 - In Python, there is a module called Decimal, which is used to do some decimal floating point related tasks. This module provides correctly-rounded floating point arithmetic. To use it at first we need to import it the Decimal standard libra
Discussions

Newbie question about 'decimal' calculations
hi @ all, shortly learned that python is using ‘mpdecimal’ for the decimal module, and now that mpdecimal doesn’t provide some functions, e.g. cbrt, trigonometrics, exp10 and so on, am I right in assumption calculations for such are performed in binary and the converted to decimal results? More on discuss.python.org
🌐 discuss.python.org
2
January 16, 2025
What's different between Decimal and Floating point numbers in Python?
Python's integers (±0, 1, 2, etc.) have unlimited accuracy, which is unusual as far as programming languages go. In most languages they usually cap at 64-bits (aka long int) and there may be multiple integer types of various sizes. This makes Python very useful for scientific computing as you don't need a separate BigNum library to handle arbitrary precision. Python's floating-point numbers don't quite have this same luxury due to the inaccurate nature of them. Since many decimal numbers cannot be accurately represented in binary, floating-point math will always run into inaccuracies. For instance, 0.1 + 0.1 + 0.1 isn't 0.3, but something like 0.3000000000001. You'll use floats whenever whole numbers aren't enough, but you don't need a specific amount of accuracy. You probably never meant to ask about this, but decimal.Decimal is an alternative to float that lets you set its precision yourself. It's still not infinitely accurate, but it's often used in scientific computing where integers just can't cut it. More on reddit.com
🌐 r/learnpython
6
5
March 5, 2021
Clarification on the Decimal type in Python - Stack Overflow
Everybody knows, or at least, every programmer should know, that using the float type could lead to precision errors. However, in some cases, an exact solution would be great and there are cases wh... More on stackoverflow.com
🌐 stackoverflow.com
How do you get a decimal in python? - Stack Overflow
In a truly surreal experience, I have spent 20 minutes on a task I thought would take 20 seconds. I want to use decimals with 3 or more places. I cannot get anything over 1 place, and even in that... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Real Python
realpython.com › ref › stdlib › decimal
decimal | Python Standard Library – Real Python
>>> from decimal import Decimal >>> Decimal("0.1") + Decimal("0.2") Decimal('0.3') >>> # Using the built-in float >>> 0.1 + 0.2 0.30000000000000004
🌐
W3Schools
w3schools.com › python › ref_module_decimal.asp
Python decimal Module
Python Examples Python Compiler Python Exercises Python Quiz Python Challenges Python Server Python Syllabus Python Study Plan Python Interview Q&A Python Bootcamp Python Certificate Python Training ... from decimal import Decimal print(Decimal('0.1') + Decimal('0.2')) print(Decimal('1') / Decimal('4')) Try it Yourself »
🌐
Python.org
discuss.python.org › python help
Newbie question about 'decimal' calculations - Python Help - Discussions on Python.org
January 16, 2025 - hi @ all, shortly learned that python is using ‘mpdecimal’ for the decimal module, and now that mpdecimal doesn’t provide some functions, e.g. cbrt, trigonometrics, exp10 and so on, am I right in assumption calculations for such are performed in binary and the converted to decimal results?
Top answer
1 of 4
9
Integers (int type) are "whole numbers", without the fractional part, eg. 10. You use this type when the value is always an integer, e.g. a counter. Floating point numbers (float type) can have fractional part, e.g. 10.1234. They are stored using the closest representation in binary notation. Most numbers cannot be represented accurately, so they are slightly off. You use float for most mathematical calculations involving fractional numbers. Decimal numbers (Decimal type in Python) are used to represent floating point numbers accurately, with a defined precision (a defined number of places after decimal point). They are represented with two integer numbers, one for the integer part and one for the fractional part. For example. 10.1234 is stored as (10, 1234). Decimal type is required when fractional numbers must be represented accurately, with defined precision. The most notable example is financial calculations. Using float type you may get a result of a financial operation as 1010.123456$. But money is expressed with at most two decimal places. What does 0.123456$ mean? You can round it to 1010.12$, but then what happens with the remaining 0.003456$? Some "smart" programmers used that to their advantage in the past and they made a lot of money (which they eventually had to give back). So, for money calculations, you should use Decimal type. A good explanation of the Decimal type is in the documentation: https://docs.python.org/3/library/decimal.html
2 of 4
7
Python's integers (±0, 1, 2, etc.) have unlimited accuracy, which is unusual as far as programming languages go. In most languages they usually cap at 64-bits (aka long int) and there may be multiple integer types of various sizes. This makes Python very useful for scientific computing as you don't need a separate BigNum library to handle arbitrary precision. Python's floating-point numbers don't quite have this same luxury due to the inaccurate nature of them. Since many decimal numbers cannot be accurately represented in binary, floating-point math will always run into inaccuracies. For instance, 0.1 + 0.1 + 0.1 isn't 0.3, but something like 0.3000000000001. You'll use floats whenever whole numbers aren't enough, but you don't need a specific amount of accuracy. You probably never meant to ask about this, but decimal.Decimal is an alternative to float that lets you set its precision yourself. It's still not infinitely accurate, but it's often used in scientific computing where integers just can't cut it.
Find elsewhere
Top answer
1 of 5
63

The Decimal class is best for financial type addition, subtraction multiplication, division type problems:

>>> (1.1+2.2-3.3)*10000000000000000000
4440.892098500626                            # relevant for government invoices...
>>> import decimal
>>> D=decimal.Decimal
>>> (D('1.1')+D('2.2')-D('3.3'))*10000000000000000000
Decimal('0.0')

The Fraction module works well with the rational number problem domain you describe:

>>> from fractions import Fraction
>>> f = Fraction(1) / Fraction(3)
>>> f
Fraction(1, 3)
>>> f * 3 < 1
False
>>> f * 3 == 1
True

For pure multi precision floating point for scientific work, consider mpmath.

If your problem can be held to the symbolic realm, consider sympy. Here is how you would handle the 1/3 issue:

>>> sympy.sympify('1/3')*3
1
>>> (sympy.sympify('1/3')*3) == 1
True

Sympy uses mpmath for arbitrary precision floating point, includes the ability to handle rational numbers and irrational numbers symbolically.

Consider the pure floating point representation of the irrational value of √2:

>>> math.sqrt(2)
1.4142135623730951
>>> math.sqrt(2)*math.sqrt(2)
2.0000000000000004
>>> math.sqrt(2)*math.sqrt(2)==2
False

Compare to sympy:

>>> sympy.sqrt(2)
sqrt(2)                              # treated symbolically
>>> sympy.sqrt(2)*sympy.sqrt(2)==2
True

You can also reduce values:

>>> import sympy
>>> sympy.sqrt(8)
2*sqrt(2)                            # √8 == √(4 x 2) == 2*√2...

However, you can see issues with Sympy similar to straight floating point if not careful:

>>> 1.1+2.2-3.3
4.440892098500626e-16
>>> sympy.sympify('1.1+2.2-3.3')
4.44089209850063e-16                   # :-(

This is better done with Decimal:

>>> D('1.1')+D('2.2')-D('3.3')
Decimal('0.0')

Or using Fractions or Sympy and keeping values such as 1.1 as ratios:

>>> sympy.sympify('11/10+22/10-33/10')==0
True
>>> Fraction('1.1')+Fraction('2.2')-Fraction('3.3')==0
True

Or use Rational in sympy:

>>> frac=sympy.Rational
>>> frac('1.1')+frac('2.2')-frac('3.3')==0
True
>>> frac('1/3')*3
1

You can play with sympy live.

2 of 5
6

So, my question is: is there a way to have a Decimal type with an infinite precision?

No, since storing an irrational number would require infinite memory.

Where Decimal is useful is representing things like monetary amounts, where the values need to be exact and the precision is known a priori.

From the question, it is not entirely clear that Decimal is more appropriate for your use case than float.

🌐
W3Schools
w3schools.com › python › python_numbers.asp
Python Numbers
Float, or "floating point number" is a number, positive or negative, containing one or more decimals. ... Float can also be scientific numbers with an "e" to indicate the power of 10. ... x = 1 # int y = 2.8 # float z = 1j # complex #convert from int to float: a = float(x) #convert from float to int: b = int(y) #convert from int to complex: c = complex(x) print(a) print(b) print(c) print(type(a)) print(type(b)) print(type(c)) Try it Yourself » · Note: You cannot convert complex numbers into another number type. Python does not have a random() function to make a random number, but Python has a built-in module called random that can be used to make random numbers:
🌐
GeeksforGeeks
geeksforgeeks.org › python › precision-handling-python
Precision Handling in Python - GeeksforGeeks
December 19, 2025 - Given a number, the task is to control its precision either by rounding it or formatting it to a specific number of decimal places. For Example: Input: x = 2.4 Output: Integral value = 2 Smallest integer greater than x = 3 Greatest integer smaller than x = 2 · Let's explore different ways to do this task in Python.
🌐
Jessealama
jessealama.net › articles › decimal-arithmetic-in-python
Decimal arithmetic in Python
October 30, 2023 - Dec­i­mals aren't avail­able out-of-the-box, in the sense that all Python pro­grams, re­gard­less of what they im­port, can start work­ing with dec­i­mals. There is no dec­i­mal lit­er­al syn­tax in the lan­guage. That said, all one needs to do is import * from decimal and you're ready to rock.
🌐
GeeksforGeeks
geeksforgeeks.org › python › decimal-functions-python-set-1
Decimal Functions in Python | Set 1 - GeeksforGeeks
Returns 1 if 1st Decimal argument is greater than 2nd(ignoring sign), -1 if 1st Decimal argument is smaller than 2nd(ignoring sign) and 0 if both are equal(ignoring sign). ... # Python code to demonstrate the working of # compare() and compare_total_mag() # importing "decimal" module to use decimal functions import decimal # Initializing decimal number a = decimal.Decimal(9.53) # Initializing decimal number b = decimal.Decimal(-9.56) # comparing decimal numbers using compare() print ("The result of comparison using compare() is : ",end="") print (a.compare(b)) # comparing decimal numbers using compare_total_mag() print ("The result of comparison using compare_total_mag() is : ",end="") print (a.compare_total_mag(b))
Published   January 7, 2025
🌐
DigitalOcean
digitalocean.com › community › tutorials › python-decimal-division-round-precision
Python decimal - division, round, precision | DigitalOcean
August 4, 2022 - Python decimal module helps us in division with proper precision and rounding of numbers.
🌐
Medium
pranaysuyash.medium.com › how-i-lost-10-000-because-of-a-python-float-and-how-you-can-avoid-my-mistake-3bd2e5b4094d
Python Decimal vs Float: The $10,000 Mistake to Avoid | Medium
April 9, 2025 - Discover why using Python's float for financial calculations can be disastrous, and learn how to properly handle money with the Decimal module to avoid costly errors.
🌐
SSOJet
ssojet.com › binary-encoding-decoding › decimal-in-python
Decimal in Python | Binary to Text Encodings in Programming Languages
To work with precise decimal numbers in Python, you must first import the Decimal type from the decimal module. The most reliable way to create a Decimal object is by initializing it from a string.
🌐
LAAC Technology
laac.dev › blog › float-vs-decimal-python
Float vs Decimal in Python | LAAC Technology
March 4, 2021 - Generally, decimals exist in Python to solve the precision issues of floats.
🌐
Shiksha
shiksha.com › home › it & software › it & software articles › programming articles › precision handling in python
Precision Handling in Python - Shiksha Online
October 31, 2022 - The format() function in python can be used to format a float value for precision based on the format specifier. ... format_specifier: The format specifier in the above syntax specifies how the value is to be formatted.
🌐
Python Tutorial
pythontutorial.net › home › advanced python › python decimal
An Essential Guide to Python Decimal By Examples
February 26, 2021 - In this tutorial, you'll learn about the Python decimal module that supports fast correctly-rounded decimal floating-point arithmetic
Top answer
1 of 5
22

In Python 2, 1/3 does integer division because both operands are integers. You need to do float division:

Copy1.0/3.0

Or:

Copyfrom __future__ import division

Which will make / do real division and // do integer division. This is the default as of Python 3.

2 of 5
7

If you divide 2 integers you'll end up with a truncated integer result (i.e., any fractional part of the result is discarded) which is why

Copy 1 / 3

gives you:

Copy 0

To avoid this problem, at least one of the operands needs to be a float. e.g., 1.0 / 3 or 1 / 3.0 or (of course) 1.0 / 3.0 will avoid integer truncation. This behavior is not unique to Python by the way.

(Edit: As mentioned in a helpful comment below by @Ivc if one of the integer operands is negative, the result is floor()'ed instead - see this article on the reason for this).

Also, there might be some confusion about the internal and external represenation of the number. The number is what it is, but we can determine how it's displayed.

You can control the external representation with formatting instructions. For instance a number can be displayed with 5 digits after the decimal point like this:

Copyn = 1/3.0

print '%.5f' %n
0.33333

To get 15 digits after the decimal.

Copyprint '%.15f' %n
0.333333333333333

Finally, there is a "new and improved" way of formatting strings/numbers using the .format() function which will be around for probably much longer than the %-formatting I showed above. An example would be:

Copyprint 'the number is {:.2}'.format(1.0/3.0)

would give you:

Copythe number is 0.33
🌐
Readthedocs
pydoc-zh.readthedocs.io › en › latest › library › decimal.html
9.4. decimal — Decimal fixed point and floating point arithmetic — Python 2.7.6 documentation
Note Decimal.from_float(0.1) is ... 0.1000000000000000055511151231257827021181583404541015625. ... From Python 2.7 onwards, a Decimal instance can also be constructed directly from a float....
🌐
Python
peps.python.org › pep-0327
PEP 327 – Decimal Data Type | peps.python.org
Decimal will be floating point (as opposed to fixed point) and will have bounded precision (the precision is the upper limit on the number of significant digits in a result). However, precision is user-settable, and a notion of significant trailing ...