liveBook · Manning

Floating-point arithmetic

As explained in Python Workout: 50 ten-minute exercises

A floating-point value is a numeric value represented using a finite-precision significand and exponent, allowing fractional values and a wide magnitude range.

A floating-point number represents fractional values and a wide range of magnitudes, but finite precision can make its stored value inexact. Use it for ordinary measurements when small representation differences are acceptable; choose another representation for sensitive calculations. section 1.12 section 1.13

Practical Python patterns include:

  • Convert user input with float, and catch ValueError when the input may be invalid. section 1.13
  • Format output with an f-string when a controlled number of decimal places is more useful than the raw representation. section 1.13
  • Use integers for fixed units such as cents when exact monetary values matter; explore Decimal for decimal-oriented arithmetic. section 1.13 section 1.16

Run-time average

Accumulate several 10-kilometer run times as floating-point inputs, stop when the user presses Enter, and display the average. For example, 15, 20, and 10 produce an average of 15.0 over three runs. section 1.12

How Python Workout: 50 ten-minute exercises covers it

How deep it goes
Explains conversion, accumulation, formatting, and representation error, but stops short of floating-point internals.
Examples
Uses run-time averages, invalid-input handling, 0.1 plus 0.7, formatted output, integer cents, and Decimal sums.
What it assumes
Assumes familiarity with Python programs and input while reinforcing numeric-type distinctions and conversions from strings.
What you can do after
Apply it via Python's float conversion, averaging, validation, and f-string formatting while recognizing when precision alternatives are needed.
How it connects
Connects floating-point numbers with integers, strings, input validation, f-strings, and the Decimal class.

Also explained in

Compare how 20 books explain floating-point arithmetic →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Python Workout: 50 ten-minute exercises. The section links point to the parts of the book it draws on.