Double precision floating values in Python

Types Double Precision Floating Values In Python

Python’s built-in float data type provides up to 15 digits of decimal precision. While sufficient for most applications, some numerical computations require even higher precision. Luckily, Python offers some great options for working with high-precision decimal values.

Python’s standard float type, based on double-precision IEEE 754 format, provides up to 15 decimal digits of precision. For tasks needing more precision, Python offers decimal. Decimal with configurable precision, fractions.Fraction for exact irreducible fractions, and numpy.float128 for up to 113 bits of precision. These options cater to applications like high-frequency trading, scientific computing, and precise financial calculations.

Also read: 2 Ways to Round a Float to Nearest 10th

What Are Python Floats?

The float data type in Python represents binary floating point numbers conforming to the IEEE 754 standard. These are inexact decimal numbers that have a fixed precision determined by the CPU architecture.

Internally, Python floats are represented in 64 bits, providing 53 bits of binary precision. This equates to approximately 15-16 decimal digits of precision.

For example:

num = 1.23456789012345  

print(num)
# 1.23456789012345

We specified 15 decimal digits but float can accurately represent all of them. However, if we try 16 digits:

num = 1.234567890123456  

print(f'{num:.17}')
# 1.2345678901234568

Note the value is rounded to the nearest 15-digit value available.

The key aspects of Python floats are:

  • Double precision as per IEEE 754 standard
  • 64 bits internally (53 bits precision)
  • Inexact decimal representation
  • The precision of approximately 15 significant decimal digits
  • Rounded to nearest value if more precision specified
  • Built-in primitive numeric type in Python

The fixed precision of floats is sufficient for most applications but not when exact representations of very large/small decimal values is needed. This led to the development of Python’s high-precision “decimal” module.

When Higher Precision is Needed

Typical use cases that demand precision beyond Python’s float include:

  • Financial applications dealing with very small currency values. For example, accurately representing nanosecond-level pricing differentials in high-frequency trading systems.
  • Some scientific computing tasks like simulations, statistical analysis, and numerical integration. For example, introducing small computational perturbations and accurately assessing the effects.
  • Any application where decimal rounding errors could accumulate over many iterations leading to noticeable inaccuracies.

Python Alternatives for High Precision

Python offers excellent high-precision decimal options:

  • decimal.Decimal: Provides 28 places by default but is configurable
  • Fraction: Represents exact irreducible fractions which can be very precise
  • numpy.float128: Up to 113 bits of precision

Below we’ll look at examples of these in action.

decimal.Decimal for Arbitrary Precision

The decimal module provides the Decimal type which allows configuring the precision and scale. Precision refers to the total number of significant digits, while scale represents the number of digits following the decimal point.

from decimal import Decimal

a = Decimal('0.12345678')  

print(a)
#0.12345678  

a = Decimal('0.12345678')
a = a.quantize(Decimal('0.00000000001')) 

print(a) 
# 0.12345670000

As you can see, we can control the precision and scaling of Decimal values specifically for our use case. By default, 28 places are provided but the precision can be set arbitrarily high as needed.

Also read: Formatting Floating Points Before Decimal Separator in Python

Fractions for Exact Representation

The Fraction type from Python’s fractions module can represent values as exact irreducible fractions. This allows representing some values much more precisely than is possible even with Decimal.

from fractions import Fraction

a = Fraction(1, 97)

print(a)  
# 1/97

float(a)
# 0.010309278350515463917525773195876

As shown above, Fraction preserves the precise value whereas converting to float introduces imprecision from rounding errors.

NumPy for up to 113-bit Precision

NumPy offers the float128 type which utilizes the quad-precision float format supported by some processors. This provides up to 113 bits of precision – far greater than Python’s native floats.

import numpy as np

a = np.float128(1.2345678901234567)

print(a)
# 1.2345678901234567

print(f'{a:.30f}') 
# 1.23456789012345678919623565674840

So NumPy float128 allows gaining orders of magnitude higher precision when needed.

Comparison of High Precision Options in Python

TypePrecisionUse Cases
float15-16 digitsGeneral numerical programming
decimal.Decimal28 digits default. ConfigurableAccounting/finance. Control precision
fractions.FractionExact irreducible fractionsPrecision critical computations
numpy.float128113 bits / 34 digitsScientific / simulation computing

Conclusion

While Python’s built-in float type meets the needs of most applications, high precision alternatives like Decimal, Fraction, and float128 are available when required. By leveraging these, virtually any level of decimal precision can be achieved in Python without needing external libraries.

So for your next scientific computing, analytics, or financial application requiring precision beyond standard hardware floats, be sure to keep Python’s high precision numerical types in mind! They provide the exact representations needed for stable and accurate computations.