Python 3.x Float Rounding Error (Decimal)

Jul 25, 2019

Example of Python float rounding error.

v = 10.0 - 9.2print(v)    # 0.8000000000000007

Convert to Decimal

Solution 1: Thread context

import decimalfrom decimal import Decimaldecimal.getcontext().prec = 8v = Decimal(10.0) - Decimal(9.2)print(v)    # 0.80000000

Solution 2: Apply to all threads about to be launched

decimal.DefaultContext.prec = 8             # all future thread will use thisdecimal.setcontext(decimal.DefaultContext)  # must apply to current threadv = Decimal(10.0) - Decimal(9.2)print(v)    # 0.80000000

Solutuon 3: Local context

with decimal.localcontext() as context:    context.prec = 8    v = Decimal(10.0) - Decimal(9.2)    print(v)

Solution 4: Context instance

context = decimal.getcontext().copy()context.prec = 8v = context.create_decimal(10.0) - context.create_decimal(9.2)print(v)

or

context = decimal.Context(prec=8)

Mix usage with int and float

OK

Decimal(1) + 1      # Decimal('2')Decimal(1) == 1     # TrueDecimal(1) == 1.0   # TrueDecimal(1.0) == 1.0 # True

Error

Decimal(1) + 1.0    # TypeError

quantize

The quantize() method rounds a number to a fixed exponent. This method is useful for monetary applications that often round results to a fixed number of places:

import mathfrom decimal import Decimalv = Decimal(math.pi)print(v)    # 3.141592653589793115997963468544185161590576171875v = Decimal(math.pi).quantize(Decimal('.01'))print(v)    # 3.14v = Decimal(math.pi).quantize(Decimal('.01'), rounding=decimal.ROUND_UP)print(v)    # 3.15v = Decimal(math.pi).quantize(Decimal('1.'))print(v)    # 3

Pre-made Context

BasicContext

This is a standard context defined by the General Decimal Arithmetic Specification. Precision is set to nine. Rounding is set to ROUND_HALF_UP. All flags are cleared. All traps are enabled (treated as exceptions) except Inexact, Rounded, and Subnormal.

Because many of the traps are enabled, this context is useful for debugging.

ExtendedContext

This is a standard context defined by the General Decimal Arithmetic Specification. Precision is set to nine. Rounding is set to ROUND_HALF_EVEN. All flags are cleared. No traps are enabled (so that exceptions are not raised during computations).

Because the traps are disabled, this context is useful for applications that prefer to have result value of NaN or Infinity instead of raising exceptions. This allows an application to complete a run in the presence of conditions that would otherwise halt the program.

DefaultContext

This context is used by the Context constructor as a prototype for new contexts. Changing a field (such a precision) has the effect of changing the default for new contexts created by the Context constructor.

This context is most useful in multi-threaded environments. Changing one of the fields before threads are started has the effect of setting system-wide defaults. Changing the fields after threads have started is not recommended as it would require thread synchronization to prevent race conditions.

In single threaded environments, it is preferable to not use this context at all. Instead, simply create contexts explicitly as described below.

The default values are prec=28, rounding=ROUND_HALF_EVEN, and enabled traps for Overflow, InvalidOperation, and DivisionByZero.

Usage

decimal.setcontext(decimal.ExtendedContext)

Caveats

import decimalfrom decimal import Decimaldecimal.getcontext().prec = 8v = Decimal(5.86)print(v)    # 5.86000000000000031974423109204508364200592041015625

Solution

context = decimal.getcontext().copy()context.prec = 8v = context.create_decimal(5.86)print(v)

or

v = Decimal(5.86) + Decimal(0)print(v)

References:

❤️ Is this article helpful?

Buy me a coffee ☕ or support my work via PayPal to keep this space 🖖 and ad-free.

Do send some 💖 to @d_luaz or share this article.

✨ By Desmond Lua

A dream boy who enjoys making apps, travelling and making youtube videos. Follow me on @d_luaz

👶 Apps I built

Travelopy - discover travel places in Malaysia, Singapore, Taiwan, Japan.