Modern Python Cookbook
上QQ阅读APP看书,第一时间看更新

Choosing between true pision and floor pision

Python offers us two kinds of pision operators. What are they, and how do we know which one to use? We'll also look at the Python pision rules and how they apply to integer values.

Getting ready

There are several general cases for pision:

  • A p-mod pair: We want both parts – the quotient and the remainder. The name refers to the pision and modulo operations combined together. We can summarize the quotient and remainder as .

    We often use this when converting values from one base into another. When we convert seconds into hours, minutes, and seconds, we'll be doing a p-mod kind of pision. We don't want the exact number of hours; we want a truncated number of hours, and the remainder will be converted into minutes and seconds.

  • The true value: This is a typical floating-point value; it will be a good approximation to the quotient. For example, if we're computing an average of several measurements, we usually expect the result to be floating-point, even if the input values are all integers.
  • A rational fraction value: This is often necessary when working in American units of feet, inches, and cups. For this, we should be using the Fraction class. When we pide Fraction objects, we always get exact answers.

We need to decide which of these cases apply, so we know which pision operator to use.

How to do it...

We'll look at these three cases separately. First, we'll look at truncated floor pision. Then, we'll look at true floating-point pision. Finally, we'll look at the pision of fractions.

Doing floor pision

When we are doing the p-mod kind of calculations, we might use the floor pision operator, //, and the modulo operator, %. The expression a % b gives us the remainder from an integer pision of a // b. Or, we might use the pmod() built-in function to compute both at once:

  1. We'll pide the number of seconds by 3,600 to get the value of hours. The modulo, or remainder in pision, computed with the % operator, can be converted separately into minutes and seconds:
    >>> total_seconds = 7385
    >>> hours = total_seconds//3600
    >>> remaining_seconds = total_seconds % 3600
    
  2. Next, we'll pide the number of seconds by 60 to get minutes; the remainder is the number of seconds less than 60:
    >>> minutes = remaining_seconds//60
    >>> seconds = remaining_seconds % 60
    >>> hours, minutes, seconds
    (2, 3, 5)
    

Here's the alternative, using the pmod() function to compute quotient and modulo together:

  1. Compute quotient and remainder at the same time:
    >>> total_seconds = 7385
    >>> hours, remaining_seconds = pmod(total_seconds, 3600)
    
  2. Compute quotient and remainder again:
    >>> minutes, seconds = pmod(remaining_seconds, 60)
    >>> hours, minutes, seconds
    (2, 3, 5)
    

Doing true pision

A true value calculation gives as a floating-point approximation. For example, about how many hours is 7,386 seconds? Divide using the true pision operator:

>>> total_seconds = 7385
>>> hours = total_seconds / 3600
>>> round(hours, 4)
2.0514

We provided two integer values, but got a floating-point exact result. Consistent with our previous recipe, when using floating-point values, we rounded the result to avoid having to look at tiny error values.

This true pision is a feature of Python 3 that Python 2 didn't offer by default.

Rational fraction calculations

We can do pision using Fraction objects and integers. This forces the result to be a mathematically exact rational number:

  1. Create at least one Fraction value:
    >>> from fractions import Fraction
    >>> total_seconds = Fraction(7385)
    
  2. Use the Fraction value in a calculation. Any integer will be promoted to a Fraction:
    >>> hours = total_seconds / 3600
    >>> hours
    Fraction(1477, 720)
    
  3. If necessary, convert the exact fraction into a floating-point approximation:
    >>> round(float(hours),4)
    2.0514
    

First, we created a Fraction object for the total number of seconds. When we do arithmetic on fractions, Python will promote any integers to be fractions; this promotion means that the math is done as precisely as possible.

How it works...

Python has two pision operators:

  • The / true pision operator produces a true, floating-point result. It does this even when the two operands are integers. This is an unusual operator in this respect. All other operators preserve the type of the data. The true pision operation – when applied to integers – produces a float result.
  • The // truncated pision operator always produces a truncated result. For two integer operands, this is the truncated quotient. When floating-point operands are used, this is a truncated floating-point result:
    >>> 7358.0 // 3600.0
    2.0
    

See also