Lines Matching +full:python +full:- +full:format
5 .. _tut-fp-issues:
14 Floating-point numbers are represented in computer hardware as base 2 (binary)
22 fractions. A consequence is that, in general, the decimal floating-point
23 numbers you enter are only approximated by the binary floating-point numbers
57 displayed. Python only prints a decimal approximation to the true decimal
59 Python were to print the true decimal value of the binary approximation stored
65 That is more digits than most people find useful, so Python keeps the number
82 Historically, the Python prompt and built-in :func:`repr` function would choose
84 Python 3.1, Python (on most systems) is now able to choose the shortest of
87 Note that this is in the very nature of binary floating-point: this is not a bug
88 in Python, and it is not a bug in your code either. You'll see the same kind of
89 thing in all languages that support your hardware's floating-point arithmetic
95 >>> format(math.pi, '.12g') # give 12 significant digits
98 >>> format(math.pi, '.2f') # give 2 digits after the point
115 0.3 cannot get any closer to the exact value of 3/10, then pre-rounding with
122 the :func:`round` function can be useful for post-rounding so that results
128 Binary floating-point arithmetic holds many surprises like this. The problem
131 <https://jvns.ca/blog/2023/01/13/examples-of-floating-point-problems/>`_ for
132 a pleasant summary of how binary floating-point works and the kinds of
138 wary of floating-point! The errors in Python float operations are inherited
139 from the floating-point hardware, and on most machines are on the order of no
144 While pathological cases do exist, for most casual use of floating-point
147 :func:`str` usually suffices, and for finer control see the :meth:`str.format`
148 method's format specifiers in :ref:`formatstrings`.
152 accounting applications and high-precision applications.
158 If you are a heavy user of floating-point operations you should take a look
162 Python provides tools that may help on those rare occasions when you really
190 across different versions of Python (platform independence) and exchanging
191 data with other languages that support the same format (such as Java and C99).
194 loss-of-precision during summation. It tracks "lost digits" as values are
204 .. _tut-fp-error:
211 floating-point representation is assumed.
215 This is the chief reason why Python (or Perl, C, C++, Java, Fortran, and many
219 least 2000, almost all machines use IEEE 754 binary floating-point arithmetic,
220 and almost all platforms map Python floats to IEEE 754 binary64 "double
280 older versions of Python), round the result to 17 significant digits::
282 >>> format(0.1, '.17f')
300 >>> format(Decimal.from_float(0.1), '.17')