You're getting inexact results because you're rounding down to two decimal places after every calculation. Your calculator isn't doing that.

With no rounding.

>>> (Decimal(40) - Decimal(56.5)) / Decimal(56.5)
Decimal('-0.2920353982300884955752212389')

With rounding:

>>> (Decimal(40) - Decimal(56.5)) / Decimal(56.5)
Decimal('-0.28')

It only gets worse after that.

Answer from Frank Yellin on Stack Overflow
🌐
Python
docs.python.org › 3 › library › decimal.html
decimal — Decimal fixed-point and floating-point arithmetic
Source code: Lib/decimal.py The decimal module provides support for fast correctly rounded decimal floating-point arithmetic. It offers several advantages over the float datatype: Decimal “is based...
🌐
W3Schools
w3schools.com › python › ref_module_decimal.asp
Python decimal Module
Built-in Modules Random Module Requests Module Statistics Module Math Module cMath Module · Remove List Duplicates Reverse a String Add Two Numbers · 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 »
🌐
Scaler
scaler.com › home › topics › python decimal module
Python Decimal Module | Scaler Topics
May 4, 2023 - There are several different numeric data types in Python, including int, float, and complex numbers, but due to floating point numbers' machine-dependent nature, we need a more precise data type. This article examines the Python decimal module, which implements decimal numbers with precision of up to 28 digits.
🌐
Real Python
realpython.com › ref › stdlib › decimal
decimal | Python Standard Library – Real Python
The Python decimal module provides support for correctly rounded decimal floating-point arithmetic.
🌐
AskPython
askpython.com › home › python decimal module – 7 functions you need to know!
Python decimal module - 7 functions you need to know! - AskPython
August 6, 2022 - Python decimal module contains various functions to handle numeric data and perform different mathematical operations on it.
🌐
PythonForBeginners.com
pythonforbeginners.com › home › decimal module in python
Decimal Module in Python - PythonForBeginners.com
June 24, 2021 - Python has numeric data types like int, float and complex numbers but due to the machine dependent nature of floating point numbers, we need a more precise data type for calculations which demand high precision. In this article, we will study about the decimal module in python which implements the decimal numbers having precision of upto 28 digits.
🌐
Python Module of the Week
pymotw.com › 2 › decimal
decimal – Fixed and floating point math - Python Module of the Week
Decimal operators also accept integer arguments, but floating point values must be converted to Decimal instances. $ python decimal_operators.py a = 5.1 b = 3.14 c = 4 d = 3.14 a + b = 8.24 a - b = 1.96 a * b = 16.014 a / b = 1.624203821656050955414012739 a + c = 9.1 a - c = 1.1 a * c = 20.4 a / c = 1.275 a + d = unsupported operand type(s) for +: 'Decimal' and 'float'
Find elsewhere
🌐
Python Tutorial
pythontutorial.net › home › advanced python › python decimal
An Essential Guide to Python Decimal By Examples
February 26, 2021 - Use the Python decimal module when you want to support fast correctly-rounded decimal floating-point arithmetic.
🌐
GitHub
github.com › python › cpython › blob › main › Lib › decimal.py
cpython/Lib/decimal.py at main · python/cpython
Decimal floating point has finite precision with arbitrarily large bounds. · The purpose of this module is to support arithmetic using familiar · "schoolhouse" rules and to avoid some of the tricky representation · issues associated with ...
Author   python
🌐
GeeksforGeeks
geeksforgeeks.org › python › decimal-functions-python-set-1
Decimal Functions in Python | Set 1 - GeeksforGeeks
# Python code to demonstrate the working of # ln() and log10() # importing "decimal" module to use decimal functions import decimal # using ln() to compute the natural log of decimal number a = decimal.Decimal(4.5).ln() # using sqrt() to compute the log10 of decimal number b = decimal.Decimal(4.5).log10() # printing the natural logarithm print ("The natural logarithm of decimal number is : ",end="") print (a) # printing the log10 print ("The log(base 10) of decimal number is : ",end="") print (b)
Published   January 7, 2025
🌐
PyPI
pypi.org › project › pypi-cdecimal
pypi-cdecimal · PyPI
Fast arbitrary precision correctly-rounded decimal floating point arithmetic.
      » pip install pypi-cdecimal
    
Published   May 04, 2016
Version   2.3
🌐
ZetCode
zetcode.com › python › decimal
Python Decimal - high-precision calculations in Python with Decimal
It is possible to change the default precision of the Decimal type. In the following example, we also use the mpmath module, which is a library for arbitrary-precision floating-point arithmetic. ... We need to install mpmath first. ... #!/usr/bin/python from decimal import Decimal, getcontext ...
🌐
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.
🌐
O'Reilly
oreilly.com › library › view › python-in-a › 0596100469 › ch15s04.html
The decimal Module - Python in a Nutshell, 2nd Edition [Book]
July 14, 2006 - Module decimal supplies a class Decimal whose immutable instances are decimal numbers, exception classes, and classes and functions to deal with the arithmetic context, which specifies such things as precision, rounding, and which computational ...
Author   Alex Martelli
Published   2006
Pages   734
🌐
YouTube
youtube.com › watch
Decimal Module in Python For Accurate Floats - YouTube
In this video, we will discuss the decimal module in Python, which is used to work with floating point arithmetics in a reliable way.◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾📚 Prog...
Published   September 24, 2022
🌐
Wikiversity
en.wikiversity.org › wiki › Python_Concepts › Using_Python's_Decimal_module
Python Concepts/Using Python's Decimal module - Wikiversity
Retrieved from "https://en.wikiversity.org/w/index.php?title=Python_Concepts/Using_Python's_Decimal_module&oldid=2745685" Category: Python · Hidden category: Pages with syntax highlighting errors · Search · Python Concepts/Using Python's Decimal module ·
🌐
Python
docs.python.org › 3.0 › library › decimal.html
decimal — Decimal fixed point and floating point arithmetic — Python v3.0.1 documentation
The decimal module was designed to support “without prejudice, both exact unrounded decimal arithmetic (sometimes called fixed-point arithmetic) and rounded floating-point arithmetic.” – excerpt from the decimal arithmetic specification.
🌐
Linux Hint
linuxhint.com › decimal-module-python
How to Use the Decimal Module in Python
Linux Hint LLC, [email protected] 1210 Kelly Park Circle, Morgan Hill, CA 95037 Privacy Policy and Terms of Use
🌐
Medium
medium.com › @hadi1812001 › decimal-class-in-python-an-intro-and-its-use-cases-28dc84380d87
Decimal Class in Python: An Intro and its Use Cases | by Hadi | Medium
February 8, 2023 - The Decimal class in Python is part of the decimal module, and it provides support for fast correctly rounded decimal floating point…