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.

Answer from dawg on Stack Overflow
๐ŸŒ
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....
๐ŸŒ
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
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
0
2
January 16, 2025
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
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
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
๐ŸŒ
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
๐ŸŒ
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?
๐ŸŒ
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:
Find elsewhere
๐ŸŒ
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.
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.

๐ŸŒ
Jessealama
jessealama.net โ€บ articles โ€บ decimal-arithmetic-in-python
Decimal arithmetic in Python
October 30, 2023 - How to work with decimal numbers in Python using the decimal module.
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.
๐ŸŒ
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.
๐ŸŒ
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 - Unlike floats, Python represents decimal numbers exactly. And the exactness carries over into 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 ...
๐ŸŒ
Purple Engineer
purpletutor.com โ€บ home โ€บ math for programming โ€บ master python decimal precision for reliable financial calculations
python decimal precision for finance and science applications โšก๐Ÿ’ฐ
January 21, 2026 - When I first debugged that trading system error, I discovered that what seemed like simple decimal calculations were actually complex binary approximations. The infamous 0.1 + 0.2 != 0.3 example perfectly illustrates this problem โ€“ Python's built-in float type returns 0.30000000000000004 instead of the expected 0.3.
๐ŸŒ
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.