Concepts / Integers and Floating-Point Numbers

Integers and Floating-Point Numbers

In Python 3, the / operator always returns a float, even when dividing two integers—a significant change from Python 2.

  • Programming

A Surprising Quotient

Consider the expression 5 / 2. Both operands are integers, but Python 3 does not produce an integer result. The / operator produces a floating-point result. This behavior is a significant change from Python 2 and is the starting point for choosing the correct division operator.

What do you think happens?

Before running 5 / 2 in Python 3, what value and result kind do you predict?

  • 2, an integer
  • 2.5, a floating-point result
  • 3, an integer
  • 2.0, an integer
Reveal answer

Answer: 2.5, a floating-point result

In Python 3, / always returns a float, even when both operands are integers.

produces5integer operand2.5floating-point result/true division2integer operand
What value and output type result when two integers are divided with /?

Reading the Division Operator

The operator determines the kind of division Python performs. In Python 3, a slash, /, always returns a float. Therefore, 8 / 4 produces a floating-point result even though the division has no remainder. The operands being integers does not change this rule.

A floating-point result is the result produced by Python 3's / operator. It remains a floating-point result even when the operands supplied to / are integers.

interpretsinterpretsin Python 3Python 2historical divisionbehavior5 / 2same expression2.5Python 3 floating-pointresultPython 3/ always returns a float
How should you understand the historical difference between Python 2 and Python 3 for the same division expression?

Choosing Floored Division

Use //, called floored division, when you need an integer result. Floored division rounds down toward negative infinity. For positive values such as 5 // 2, the result is 2. The word down is important: it does not mean simply removing the fractional part in every situation.

Selecting the Integer Result

You need the floored result of dividing 5 by 2. Which operator should you choose, and what result follows?

Identify the required result: The requirement is an integer result rather than the exact floating-point quotient.

Select the operator: Use // because Python defines it as floored division.

Evaluate the expression: The expression 5 // 2 gives 2 because 2 is the value reached by rounding the quotient down toward negative infinity.

Use 5 // 2 when the required result is the floored integer 2.

producesfloors to5 / 2floating-point division2.5floating-point result5 // 2floored division2integer result
What changes when the operator is changed from / to //?

Following the Number Line

Floored division is especially important for negative numbers. The result moves toward negative infinity, not toward zero. Therefore, -5 // 2 equals -3, not -2. A result of -2 would reflect truncating the fractional part toward zero, but that is not the rule used by //.

What do you think happens?

What does -5 // 2 produce?

  • -2, because the fractional part is removed
  • -3, because floored division moves toward negative infinity
  • 2, because the sign is ignored
  • -2.5, because // returns a floating-point quotient
Reveal answer

Answer: -3, because floored division moves toward negative infinity

For negative values, flooring does not mean truncating toward zero. The next lower integer than -2.5 is -3.

floor toward negative infinity-2.5quotient before flooring-3floored result0direction not used byflooring
How does -5 // 2 relate to the number line when flooring moves toward negative infinity?

Matching Operator to Intent

Choose the operator from the result your program needs. Choose / when you need Python 3's floating-point quotient. Choose // when you need floored division and an integer result. Treat this as a requirement-matching decision rather than as a question about whether the operands happen to be integers.

need quotientneed floored integerselectselectDesired resultchoose by requirementFloating-pointquotientchoose //always returns a float inPython 3Floored integerchoose ////floors toward negativeinfinity
Which operator should be selected for the desired result?
RequirementOperatorResult behavior
Floating-point quotient/Python 3 returns a float
Floored integer result//The result is rounded toward negative infinity

A compact operator-selection guide

Mistakes Beginners Make

  • Assuming that two integer operands force an integer result.

    In Python 3, / always returns a floating-point result.

    Fix: Use // when a floored integer result is required.

  • Calling / truncating division.

    Python 3's / operator returns a floating-point result rather than truncating the quotient to an integer.

    Fix: Use / for the floating-point quotient and // for floored division.

  • Treating // as truncation toward zero.

    Floored division rounds toward negative infinity.

    Fix: Expect -5 // 2 to equal -3.

  • Choosing an operator based only on the operand types.

    The required result, not just the operand type, determines the appropriate operator.

    Fix: Choose / for a floating-point quotient and // for a floored integer result.

determines result kind7left operand2.333...floating-point quotient/returns a float3right operand
How can the operands and operator be mapped to the resulting value and data type before execution?

Check Your Prediction

MEDIUM

For each expression, predict the result before checking it: 9 / 2, 9 // 2, and -9 // 2. For each one, identify whether the result is a floating-point result or a floored integer result, and explain the direction of rounding where it applies.

Hints
  • Start by identifying whether the operator is / or //.
  • Remember that / always returns a float in Python 3.
  • For the negative // expression, move toward negative infinity rather than toward zero.

Practice Answers

Determine the behavior of 9 / 2, 9 // 2, and -9 // 2.

Evaluate 9 / 2: The / operator returns a floating-point quotient, so the result is 4.5.

Evaluate 9 // 2: The // operator performs floored division. For this positive quotient, the floored result is 4.

Evaluate -9 // 2: The quotient lies between -4 and -5, and flooring moves toward negative infinity, so the result is -5.

The expressions produce 4.5, 4, and -5 respectively.

Essential Rules

  1. In Python 3, / always returns a floating-point result, even when both operands are integers.
  2. Use / when the exact floating-point quotient is the required kind of result.
  3. Use // when a floored integer result is required.
  4. Floored division rounds toward negative infinity, so -5 // 2 equals -3 rather than -2.
  5. Do not confuse floored division with truncation toward zero.

Key Takeaways

  • Python 3's / operator always returns a floating-point result.
  • The fact that operands are integers does not make / produce an integer.
  • The // operator provides floored division and is used when an integer result is needed.
  • Floored division moves toward negative infinity, not toward zero.
  • Choose / or // according to the result your program requires.