Numerical precision and performance
Chapter 101 of 132 · Level 6 · Advanced Python & concurrency · Python demonstrations with recorded outputs. Learn through reading, predicting and testing real Python code.
Course disclaimer · independent educational content
01 · Why this concept matters
Different number representations make different tradeoffs: binary floats are fast approximations, Decimal is decimal arithmetic, and Fraction maintains exact rational values.
Learning goal: Numerical precision and performance. Start by reading what the program does, then inspect its exact output. Where an example illustrates only a prerequisite, study the named advanced feature separately before using it in production.
02 · The important subtopics
Study these connected ideas as you work through the demonstration:
- Decimal versus float
- Vectorization concepts
- numpy array layout basics
- Accurate measurement and tradeoffs
03 · Pinpoint: read the code line by line
- Line 1
from decimal import DecimalDefine or import. Notice what values enter this statement and which names it affects. - Line 2
from fractions import FractionDefine or import. Notice what values enter this statement and which names it affects. - Line 3
print(Decimal('0.1') + Decimal('0.2'))Display a result. Notice what values enter this statement and which names it affects. - Line 4
print(Fraction(1, 3) * 3)Display a result. Notice what values enter this statement and which names it affects.
Find the literal input values and arguments, follow each operation in execution order, and verify the result below. This habit turns a code listing into a testable explanation.
04 · Runnable Python example and verified output
Numerical precision and performance: working demonstration
Python · runnablefrom decimal import Decimal
from fractions import Fraction
print(Decimal('0.1') + Decimal('0.2'))
print(Fraction(1, 3) * 3)
0.3 1
Why it works: Different number representations make different tradeoffs: binary floats are fast approximations, Decimal is decimal arithmetic, and Fraction maintains exact rational values.
Worked example 2 · Numerical Precision And Performance in practice
Python · executed and checkedfrom decimal import Decimal, ROUND_HALF_UP
amount = Decimal('2.675')
print(amount.quantize(Decimal('0.01'), rounding=ROUND_HALF_UP))
2.68
Pinpoint learning: This practical numerical precision and performance demonstration uses a small input you can inspect. Run it exactly as shown, compare the displayed result, then change one value and predict how the result will change.
Understand the code line by line
1: from decimal import Decimal, ROUND_HALF_UP— Load a standard-library feature needed by the example.2: amount = Decimal('2.675')— Store or update the value on the left using the expression on the right.3: print(amount.quantize(Decimal('0.01'), rounding=ROUND_HALF_UP))— Display the value so you can compare it with the verified output.
05 · Independent practice and answer guidance
Exercise A · Adapt the code
Compare 0.1 + 0.2 with Decimal and explain the unexpected floating-point representation.
Exercise B · Predict
Before pressing Run, predict the first displayed line. Next, change one literal value and record the new observed output. Explain the difference in one clear sentence.
Exercise C · Debug
Make a temporary invalid change, read the exception or failed assertion if one occurs, restore the working code, and explain the repair.
Worked explanation for the original example
Different number representations make different tradeoffs: binary floats are fast approximations, Decimal is decimal arithmetic, and Fraction maintains exact rational values.
0.3 1
06 · Build, verify and advance
Real-world task: Analyze arithmetic accuracy and throughput for a dataset.
Run the original code; explain the important lines; change one input and verify the new outcome; use a test or recorded result to justify your conclusion.
