1.1 Choosing between float, decimal, and fraction
Python offers several ways to work with rational numbers and approximations of irrational numbers. We have three basic choices:
When we have choices, it helps to have some criteria for making a selection.
1.1.1 Getting ready
There are three general cases for expressions that involve numbers beyond integers, which are:
Currency: Dollars, cents, euros, and so on. Currency generally has a fixed number of decimal places and rounding rules to properly quantize results.
Rational Numbers or Fractions: When we scale a recipe that serves eight, for example, down to five people, we’re doing fractional math using a scaling factor of .
Floating Point: This includes all other kinds of calculations. This also includes irrational numbers, like π, root extraction, and logarithms.
When we have one of the first two cases, we should avoid floating-point numbers.
1.1.2 How to do it...
We’ll look at each of the three cases separately.
Doing currency calculations
When working with currency, we should always use the decimal module. If we try to use the values of Python’s built-in float type, we can run into problems with the rounding and truncation of numbers:
To work with currency, import the Decimal class from the decimal module:
>>> from decimal import Decimal
We need to create Decimal objects from strings or integers. In this case, we want 7.25%, which is . We can compute the value using Decimal objects:
>>> tax_rate = Decimal(’7.25’)/Decimal(100)
>>> purchase_amount = Decimal(’2.95’)
>>> tax_rate * purchase_amount
Decimal(’0.213875’)
We could also use Decimal(’0.0725’) instead of doing the division explicitly.
To round to the nearest penny, create a penny object:
>>> penny = Decimal(’0.01’)
Quantize the result using the penny object:
>>> total_amount = purchase_amount + tax_rate * purchase_amount
>>> total_amount.quantize(penny)
Decimal(’3.16’)
This uses the default rounding rule of ROUND_HALF_EVEN. The Decimal module offers other rounding variations. We might, for example, do something like this:
>>> import decimal
>>> total_amount.quantize(penny, decimal.ROUND_UP)
Decimal(’3.17’)
This shows the consequences of using a different rounding rule.
Fraction calculations
When we’re doing calculations that have exact fraction values, we can use the fractions module to create rational numbers. In this example, we want to scale a recipe for eight down to five people, using of each ingredient. When the recipe calls for 2 cups of rice, what does that scale down to?
To work with fractions, we’ll do this:
Import the Fraction class from the fractions module:
>>> from fractions import Fraction
Create Fraction objects from strings, integers, or pairs of integers. We created one fraction from a string, ’2.5’. We created the second fraction from a floating-point expression, 5 / 8. This only works when the denominator is a power of 2:
>>> sugar_cups = Fraction(’2.5’)
>>> scale_factor = Fraction(5/8)
>>> sugar_cups * scale_factor
Fraction(25, 16)
We can see that we’ll use almost a cup and a half of rice to scale the recipe for five people instead of eight. While float values will often be useful for rational fractions, they may not be exact unless the denominator is a power of two.
Floating-point approximations
Python’s built-in float type can represent a wide variety of values. The trade-off here is that a float value is often an approximation. There may be a small discrepancy that reveals the differences between the implementation of float and the mathematical ideal of an irrational number:
To work with float, we often need to round values to make them look sensible. It’s important to recognize that all float calculations are an approximation:
>>> (19/155)*(155/19)
0.9999999999999999
Mathematically, the value should be 1. Because of the approximations used, the computed result isn’t exactly 1. We can use round(answer, 3) to round to three digits, creating a value that’s more useful:
>>> answer = (19/155)*(155/19)
>>> round(answer, 3)
1.0
Approximations have a very important consequence.
Don’t compare floating-point values for exact equality.
Code that uses an exact == test between floating-point numbers has the potential to cause problems when two approximations differ by a single bit.
The float approximation rules come from the IEEE, and are not a unique feature of Python. Numerous programming languages work with float approximations and have identical behavior.
1.1.3 How it works...
For these numeric types, Python offers a variety of operators: +, -, *, /, //, %, and **. These are for addition, subtraction, multiplication, true division, truncated division, modulo, and raising to a power, respectively. We’ll look at the two division operators, / and //, in the Choosing between true division and floor division recipe.
Python will do some conversions between the various numeric types. We can mix int and float values; the integers will be promoted to floating-point to provide the most accurate answer possible. Similarly, we can mix int and Fraction as well as mixing int and Decimal. Note that we cannot casually mix Decimal with float or Fraction; an explicit conversion function will be required.
It’s important to note that float values are approximations. The Python syntax allows us to write floating-point values using base 10 digits; however, that’s not how values are represented internally.
We can write the value 8.066 × 1067 like this in Python:
>>> 8.066e+67
8.066e+67
The actual value used internally will involve a binary approximation of the decimal value we wrote. The internal value for this example is this:
>>> (6737037547376141/(2**53))*(2**226)
8.066e+67
The numerator is a big number, 6737037547376141. The denominator is always 253. This is why values can get truncated.
We can use the math.frexp() function to see these internal details of a number:
>>> import math
>>> math.frexp(8.066E+67)
(0.7479614202861186, 226)
The two parts are called the mantissa (or significand) and the exponent. If we multiply the mantissa by 253, we always get a whole number, which is the numerator of the binary fraction.
Unlike the built-in float, a Fraction is an exact ratio of two integer values. We can create ratios that involve integers with a very large number of digits. We’re not limited by a fixed denominator.
A Decimal value, similarly, is based on a very large integer value, as well as a scaling factor to determine where the decimal place goes. These numbers can be huge and won’t suffer from peculiar representation issues.
1.1.4 There’s more...
The Python math module contains several specialized functions for working with floating-point values. This module includes common elementary functions such as square root, logarithms, and various trigonometry functions. It also has some other functions such as gamma, factorial, and the Gaussian error function.
The math module includes several functions that can help us do more accurate floating-point calculations. For example, the math.fsum() function will compute a floating-point sum more carefully than the built-in sum() function. It’s less susceptible to approximation issues.
We can also make use of the math.isclose() function to compare two floating-point values, an expression, and a literal 1.0, to see if they’re nearly equal:
>>> (19/155)*(155/19) == 1.0
False
>>> math.isclose((19/155)*(155/19), 1.0)
True
This function provides us with a way to compare two floating-point numbers meaningfully for near-equality.
Python also offers complex numbers. A complex number has a real and an imaginary part. In Python, we write 3.14+2.78j to represent the complex number 3.14 + 2.78. Python will comfortably convert between float and complex. We have the usual group of operators available for complex numbers.
To support complex numbers, there’s the cmath package. The cmath.sqrt() function, for example, will return a complex value rather than raise an exception when extracting the square root of a negative number. Here’s an example:
>>> math.sqrt(-2)
Traceback (most recent call last):
...
ValueError: math domain error
>>> import cmath
>>> cmath.sqrt(-2)
1.4142135623730951j
This module is helpful when working with complex numbers.