[Tutor] Precision of floating-point number representation (was: Count for loops)

Ben Finney ben+python at benfinney.id.au
Tue Apr 11 16:43:01 EDT 2017


Rafael Knuth <rafael.knuth at gmail.com> writes:

> I tested this approach, and I noticed one weird thing:
>
> Pi_Number = str(3.14159265358979323846264338327950288419716939)
> Pi_Number = "3" + Pi_Number[2:]
> print(Pi_Number)

The mistake is in assuming such a precise number would survive
representation as a ‘float’ object. Instead, a ‘float’ object has only a
limited precision::

    >>> 3.14159265358979323846264338327950288419716939 == \
        3.141592653589793238462643383279502884
    True

See the discussion of floating-point numbers in the tutorial
<URL:https://docs.python.org/3/tutorial/floatingpoint.html>.

In fact, you should work through the entire tutorial
<URL:https://docs.python.org/3/tutorial/>, try all the exercises to
understand each section before moving to the next.

-- 
 \      “If you fell down yesterday, stand up today.” —_The Anatomy of |
  `\                                   Frustration_, H. G. Wells, 1936 |
_o__)                                                                  |
Ben Finney



More information about the Tutor mailing list