Engineers Who Ignore History... Continued

This page provides extra context on topics raised by the repeating-zeros post for readers who wish to dig a bit deeper.

More on Software Requirements

For more on the subject of what it takes to be proficient in software development, see the related posts here, here, here, and here. A few go back a decade (yes, this site has been sounding the alarm that long).

More on Floating-Point Math

If you're new to the problems of floating-point math, here's a very brief primer. Though full fidelity requires some knowledge of computer architecture and IEEE standards, the consequences are straightforward: due to its limited number of bits, floating-point math cannot exactly represent certain values in computer memory. In the Python language:

>>> 0.1 + 0.1 + 0.1 - 0.3    # Close to 0.0, but not exactly
5.551115123125783e-17

>>> 0.1 + 0.2                # Close to 0.3, but not exactly
0.30000000000000004

When it matters, programmers must accommodate this constraint by limiting the number of digits in a value, either when it is stored in memory, or when it is displayed (in a web page, for example):

>>> round(0.1 + 0.2, 2)      # Limit digits in memory
0.3

>>> '%.2f' % (0.1 + 0.2)     # Limit digits for display
'0.30'

The issue can also be sidestepped by using fixed-precision math tools. Python's Decimal type, for instance, lets coders fix digits using literals significance and calculations precision:

>>> from decimal import Decimal
>>> Decimal('0.10') + Decimal('0.20')
Decimal('0.30')

>>> import decimal
>>> decimal.getcontext().prec = 2
>>> Decimal(0.10) + Decimal(0.20)
Decimal('0.30')

Regardless of the fix, this issue goes well beyond display—unless you understand the underlying cause, you're likely to commit mistakes that generate wrong paychecks, send orbiters careening off course, and other fun stuff:

>>> 0.1 + 0.2 == 0.3              # This is where you'll really get burned...
False

>>> round(0.1 + 0.2, 1) == 0.3    # Unless you're aware of the issue
True
>>> round(0.1 + 0.2, 1) == round(0.3, 1)
True

For much more on floating-point oddness, try the web, or Chapter 5 of a large Python fundamentals book near you.



[Home page] Books Code Blog Python Author Train Find ©M.Lutz