# SOME DESCRIPTIVE TITLE. # Copyright (C) 2001-2025, Python Software Foundation # This file is distributed under the same license as the Python package. # FIRST AUTHOR , YEAR. # # Translators: # Rafael Fontenelle , 2025 # #, fuzzy msgid "" msgstr "" "Project-Id-Version: Python 3.12\n" "Report-Msgid-Bugs-To: \n" "POT-Creation-Date: 2025-07-25 16:03+0000\n" "PO-Revision-Date: 2025-07-18 19:59+0000\n" "Last-Translator: Rafael Fontenelle , 2025\n" "Language-Team: Chinese (China) (https://app.transifex.com/python-doc/teams/5390/zh_CN/)\n" "MIME-Version: 1.0\n" "Content-Type: text/plain; charset=UTF-8\n" "Content-Transfer-Encoding: 8bit\n" "Language: zh_CN\n" "Plural-Forms: nplurals=1; plural=0;\n" #: ../../tutorial/floatingpoint.rst:10 msgid "Floating-Point Arithmetic: Issues and Limitations" msgstr "浮点算术:争议和限制" #: ../../tutorial/floatingpoint.rst:16 msgid "" "Floating-point numbers are represented in computer hardware as base 2 " "(binary) fractions. For example, the **decimal** fraction ``0.625`` has " "value 6/10 + 2/100 + 5/1000, and in the same way the **binary** fraction " "``0.101`` has value 1/2 + 0/4 + 1/8. These two fractions have identical " "values, the only real difference being that the first is written in base 10 " "fractional notation, and the second in base 2." msgstr "" "浮点数在计算机硬件中是以基数为 2 (二进制) 的小数来表示的。 例如,**十进制** 小数 ``0.625`` 的值为 6/10 + 2/100 + " "5/1000,而同样的 **二进制** 小数 ``0.101`` 的值为 1/2 + 0/4 + 1/8。 " "这两个小数具有相同的值,唯一的区别在于第一个写成了基数为 10 的小数形式,而第二个则写成的基数为 2 的小数形式。" #: ../../tutorial/floatingpoint.rst:23 msgid "" "Unfortunately, most decimal fractions cannot be represented exactly as " "binary fractions. A consequence is that, in general, the decimal floating-" "point numbers you enter are only approximated by the binary floating-point " "numbers actually stored in the machine." msgstr "" "不幸的是,大多数的十进制小数都不能精确地表示为二进制小数。这导致在大多数情况下,你输入的十进制浮点数都只能近似地以二进制浮点数形式储存在计算机中。" #: ../../tutorial/floatingpoint.rst:28 msgid "" "The problem is easier to understand at first in base 10. Consider the " "fraction 1/3. You can approximate that as a base 10 fraction::" msgstr "用十进制来理解这个问题显得更加容易一些。考虑分数 1/3 。我们可以得到它在十进制下的一个近似值 ::" #: ../../tutorial/floatingpoint.rst:31 msgid "0.3" msgstr "0.3" #: ../../tutorial/floatingpoint.rst:33 ../../tutorial/floatingpoint.rst:37 msgid "or, better, ::" msgstr "或者,更近似的,::" #: ../../tutorial/floatingpoint.rst:35 msgid "0.33" msgstr "0.33" #: ../../tutorial/floatingpoint.rst:39 msgid "0.333" msgstr "0.333" #: ../../tutorial/floatingpoint.rst:41 msgid "" "and so on. No matter how many digits you're willing to write down, the " "result will never be exactly 1/3, but will be an increasingly better " "approximation of 1/3." msgstr "以此类推。结果是无论你写下多少的数字,它都永远不会等于 1/3 ,只是更加更加地接近 1/3 。" #: ../../tutorial/floatingpoint.rst:45 msgid "" "In the same way, no matter how many base 2 digits you're willing to use, the" " decimal value 0.1 cannot be represented exactly as a base 2 fraction. In " "base 2, 1/10 is the infinitely repeating fraction ::" msgstr "" "同样的道理,无论你使用多少位以 2 为基数的数码,十进制的 0.1 都无法精确地表示为一个以 2 为基数的小数。 在以 2 为基数的情况下, 1/10 " "是一个无限循环小数 ::" #: ../../tutorial/floatingpoint.rst:49 msgid "0.0001100110011001100110011001100110011001100110011..." msgstr "0.0001100110011001100110011001100110011001100110011..." #: ../../tutorial/floatingpoint.rst:51 msgid "" "Stop at any finite number of bits, and you get an approximation. On most " "machines today, floats are approximated using a binary fraction with the " "numerator using the first 53 bits starting with the most significant bit and" " with the denominator as a power of two. In the case of 1/10, the binary " "fraction is ``3602879701896397 / 2 ** 55`` which is close to but not exactly" " equal to the true value of 1/10." msgstr "" "在任何一个位置停下,你都只能得到一个近似值。因此,在今天的大部分架构上,浮点数都只能近似地使用二进制小数表示,对应分数的分子使用每 8 字节的前 53 " "位表示,分母则表示为 2 的幂次。在 1/10 这个例子中,相应的二进制分数是 ``3602879701896397 / 2 ** 55`` ,它很接近" " 1/10 ,但并不是 1/10 。" #: ../../tutorial/floatingpoint.rst:58 msgid "" "Many users are not aware of the approximation because of the way values are " "displayed. Python only prints a decimal approximation to the true decimal " "value of the binary approximation stored by the machine. On most machines, " "if Python were to print the true decimal value of the binary approximation " "stored for 0.1, it would have to display::" msgstr "" "由于值的显示方式大多数用户都不会意识到这个差异的存在。 Python 只会打印计算机中存储的二进制值的十进制近似值。 在大部分计算机中,如果 " "Python 要把 0.1 的二进制值对应的准确的十进制值打印出来,将会显示成这样::" #: ../../tutorial/floatingpoint.rst:64 msgid "" ">>> 0.1\n" "0.1000000000000000055511151231257827021181583404541015625" msgstr "" ">>> 0.1\n" "0.1000000000000000055511151231257827021181583404541015625" #: ../../tutorial/floatingpoint.rst:67 msgid "" "That is more digits than most people find useful, so Python keeps the number" " of digits manageable by displaying a rounded value instead:" msgstr "这比大多数人认为有用的数位更多,因此 Python 通过改为显示舍入值来保留可管理的数位:" #: ../../tutorial/floatingpoint.rst:70 msgid "" ">>> 1 / 10\n" "0.1" msgstr "" ">>> 1 / 10\n" "0.1" #: ../../tutorial/floatingpoint.rst:75 msgid "" "Just remember, even though the printed result looks like the exact value of " "1/10, the actual stored value is the nearest representable binary fraction." msgstr "牢记,即使输出的结果看起来好像就是 1/10 的精确值,实际储存的值只是最接近 1/10 的计算机可表示的二进制分数。" #: ../../tutorial/floatingpoint.rst:78 msgid "" "Interestingly, there are many different decimal numbers that share the same " "nearest approximate binary fraction. For example, the numbers ``0.1`` and " "``0.10000000000000001`` and " "``0.1000000000000000055511151231257827021181583404541015625`` are all " "approximated by ``3602879701896397 / 2 ** 55``. Since all of these decimal " "values share the same approximation, any one of them could be displayed " "while still preserving the invariant ``eval(repr(x)) == x``." msgstr "" "有趣的是,有许多不同的十进制数共享相同的最接近的近似二进制小数。例如, ``0.1`` 、 ``0.10000000000000001`` 、 " "``0.1000000000000000055511151231257827021181583404541015625`` 全都近似于 " "``3602879701896397 / 2 ** 55`` 。由于所有这些十进制值都具有相同的近似值,因此可以显示其中任何一个,同时仍然保留不变的 " "``eval(repr(x)) == x`` 。" #: ../../tutorial/floatingpoint.rst:86 msgid "" "Historically, the Python prompt and built-in :func:`repr` function would " "choose the one with 17 significant digits, ``0.10000000000000001``. " "Starting with Python 3.1, Python (on most systems) is now able to choose the" " shortest of these and simply display ``0.1``." msgstr "" "在历史上,Python 提示符和内置的 :func:`repr` 函数会选择具有 17 位有效数字的来显示,即 " "``0.10000000000000001``。 从 Python 3.1 开始,Python(在大多数系统上)现在能够选择这些表示中最短的并简单地显示" " ``0.1`` 。" #: ../../tutorial/floatingpoint.rst:91 msgid "" "Note that this is in the very nature of binary floating point: this is not a" " bug in Python, and it is not a bug in your code either. You'll see the " "same kind of thing in all languages that support your hardware's floating-" "point arithmetic (although some languages may not *display* the difference " "by default, or in all output modes)." msgstr "" "请注意这种情况是二进制浮点数的本质特性:它不是 Python 的错误,也不是你代码中的错误。 " "你会在所有支持你的硬件中的浮点运算的语言中发现同样的情况(虽然某些语言在默认状态或所有输出模块下都不会 *显示* 这种差异)。" #: ../../tutorial/floatingpoint.rst:97 msgid "" "For more pleasant output, you may wish to use string formatting to produce a" " limited number of significant digits:" msgstr "想要更美观的输出,你可能会希望使用字符串格式化来产生限定长度的有效位数:" #: ../../tutorial/floatingpoint.rst:100 msgid "" ">>> format(math.pi, '.12g') # give 12 significant digits\n" "'3.14159265359'\n" "\n" ">>> format(math.pi, '.2f') # give 2 digits after the point\n" "'3.14'\n" "\n" ">>> repr(math.pi)\n" "'3.141592653589793'" msgstr "" ">>> format(math.pi, '.12g') # 有 12 个有效数位\n" "'3.14159265359'\n" "\n" ">>> format(math.pi, '.2f') # 小数点后有 2 个数位\n" "'3.14'\n" "\n" ">>> repr(math.pi)\n" "'3.141592653589793'" #: ../../tutorial/floatingpoint.rst:111 msgid "" "It's important to realize that this is, in a real sense, an illusion: you're" " simply rounding the *display* of the true machine value." msgstr "必须重点了解的是,这在实际上只是一个假象:你只是将真正的机器码值进行了舍入操作再 *显示* 而已。" #: ../../tutorial/floatingpoint.rst:114 msgid "" "One illusion may beget another. For example, since 0.1 is not exactly 1/10," " summing three values of 0.1 may not yield exactly 0.3, either:" msgstr "一个假象还可能导致另一个假象。 例如,由于这个 0.1 并非真正的 1/10,将三个 0.1 的值相加也无法恰好得到 0.3:" #: ../../tutorial/floatingpoint.rst:117 msgid "" ">>> 0.1 + 0.1 + 0.1 == 0.3\n" "False" msgstr "" ">>> 0.1 + 0.1 + 0.1 == 0.3\n" "False" #: ../../tutorial/floatingpoint.rst:122 msgid "" "Also, since the 0.1 cannot get any closer to the exact value of 1/10 and 0.3" " cannot get any closer to the exact value of 3/10, then pre-rounding with " ":func:`round` function cannot help:" msgstr "" "而且,由于这个 0.1 无法精确表示 1/10 而这个 0.3 也无法精确表示 3/10 的值,使用 :func:`round` " "函数进行预先舍入也是没用的:" #: ../../tutorial/floatingpoint.rst:126 msgid "" ">>> round(0.1, 1) + round(0.1, 1) + round(0.1, 1) == round(0.3, 1)\n" "False" msgstr "" ">>> round(0.1, 1) + round(0.1, 1) + round(0.1, 1) == round(0.3, 1)\n" "False" #: ../../tutorial/floatingpoint.rst:131 msgid "" "Though the numbers cannot be made closer to their intended exact values, the" " :func:`math.isclose` function can be useful for comparing inexact values:" msgstr "虽然这些数字无法精确表示其所要代表的实际值,但是可以使用 :func:`math.isclose` 函数来进行不精确的值比较:" #: ../../tutorial/floatingpoint.rst:134 msgid "" ">>> math.isclose(0.1 + 0.1 + 0.1, 0.3)\n" "True" msgstr "" ">>> math.isclose(0.1 + 0.1 + 0.1, 0.3)\n" "True" #: ../../tutorial/floatingpoint.rst:139 msgid "" "Alternatively, the :func:`round` function can be used to compare rough " "approximations:" msgstr "或者,也可以使用 :func:`round` 函数来大致地比较近似程度:" #: ../../tutorial/floatingpoint.rst:142 msgid "" ">>> round(math.pi, ndigits=2) == round(22 / 7, ndigits=2)\n" "True" msgstr "" ">>> round(math.pi, ndigits=2) == round(22 / 7, ndigits=2)\n" "True" #: ../../tutorial/floatingpoint.rst:147 msgid "" "Binary floating-point arithmetic holds many surprises like this. The " "problem with \"0.1\" is explained in precise detail below, in the " "\"Representation Error\" section. See `Examples of Floating Point Problems " "`_ for" " a pleasant summary of how binary floating point works and the kinds of " "problems commonly encountered in practice. Also see `The Perils of Floating" " Point `_ for a more complete " "account of other common surprises." msgstr "" "二进制浮点运算会有许多这样令人惊讶的情况。 有关 \"0.1\" 的问题会在下面 \"表示性错误\" 一节中更精确详细地描述。 请参阅 " "`Examples of Floating Point Problems " "`_ " "获取针对二进制浮点运算机制及在实践中各种常见问题的概要说明。 还可参阅 `The Perils of Floating Point " "`_ 获取其他常见意外现象的更完整介绍。" #: ../../tutorial/floatingpoint.rst:156 msgid "" "As that says near the end, \"there are no easy answers.\" Still, don't be " "unduly wary of floating point! The errors in Python float operations are " "inherited from the floating-point hardware, and on most machines are on the " "order of no more than 1 part in 2\\*\\*53 per operation. That's more than " "adequate for most tasks, but you do need to keep in mind that it's not " "decimal arithmetic and that every float operation can suffer a new rounding " "error." msgstr "" "正如那篇文章的结尾所言,“对此问题并无简单的答案。” 但是也不必过于担心浮点数的问题! Python " "浮点运算中的错误是从浮点运算硬件继承而来,而在大多数机器上每次浮点运算得到的 2\\*\\*53 数码位都会被作为 1 个整体来处理。 " "这对大多数任务来说都已足够,但你确实需要记住它并非十进制算术,且每次浮点运算都可能会导致新的舍入错误。" #: ../../tutorial/floatingpoint.rst:163 msgid "" "While pathological cases do exist, for most casual use of floating-point " "arithmetic you'll see the result you expect in the end if you simply round " "the display of your final results to the number of decimal digits you " "expect. :func:`str` usually suffices, and for finer control see the " ":meth:`str.format` method's format specifiers in :ref:`formatstrings`." msgstr "" "虽然病态的情况确实存在,但对于大多数正常的浮点运算使用来说,你只需简单地将最终显示的结果舍入为你期望的十进制数值即可得到你期望的结果。 " ":func:`str` 通常已足够,对于更精度的控制可参看 :ref:`formatstrings` 中 :meth:`str.format` " "方法的格式描述符。" #: ../../tutorial/floatingpoint.rst:169 msgid "" "For use cases which require exact decimal representation, try using the " ":mod:`decimal` module which implements decimal arithmetic suitable for " "accounting applications and high-precision applications." msgstr "对于需要精确十进制表示的使用场景,请尝试使用 :mod:`decimal` 模块,该模块实现了适合会计应用和高精度应用的十进制运算。" #: ../../tutorial/floatingpoint.rst:173 msgid "" "Another form of exact arithmetic is supported by the :mod:`fractions` module" " which implements arithmetic based on rational numbers (so the numbers like " "1/3 can be represented exactly)." msgstr "" "另一种形式的精确运算由 :mod:`fractions` 模块提供支持,该模块实现了基于有理数的算术运算(因此可以精确表示像 1/3 这样的数值)。" #: ../../tutorial/floatingpoint.rst:177 msgid "" "If you are a heavy user of floating-point operations you should take a look " "at the NumPy package and many other packages for mathematical and " "statistical operations supplied by the SciPy project. See " "." msgstr "" "如果你是浮点运算的重度用户那么你应当了解一下 NumPy 包以及由 SciPy 项目所提供的许多其他数学和统计运算包。 参见 " "。" #: ../../tutorial/floatingpoint.rst:181 msgid "" "Python provides tools that may help on those rare occasions when you really " "*do* want to know the exact value of a float. The " ":meth:`float.as_integer_ratio` method expresses the value of a float as a " "fraction:" msgstr "" "Python 还提供了一些工具可能在你 *确实* 想要知道一个浮点数的精确值的少数情况下提供帮助。 例如 " ":meth:`float.as_integer_ratio` 方法会将浮点数值表示为一个分数:" #: ../../tutorial/floatingpoint.rst:186 msgid "" ">>> x = 3.14159\n" ">>> x.as_integer_ratio()\n" "(3537115888337719, 1125899906842624)" msgstr "" ">>> x = 3.14159\n" ">>> x.as_integer_ratio()\n" "(3537115888337719, 1125899906842624)" #: ../../tutorial/floatingpoint.rst:192 msgid "" "Since the ratio is exact, it can be used to losslessly recreate the original" " value:" msgstr "由于这个比值是精确的,它可以被用来无损地重建原始值:" #: ../../tutorial/floatingpoint.rst:195 msgid "" ">>> x == 3537115888337719 / 1125899906842624\n" "True" msgstr "" ">>> x == 3537115888337719 / 1125899906842624\n" "True" #: ../../tutorial/floatingpoint.rst:200 msgid "" "The :meth:`float.hex` method expresses a float in hexadecimal (base 16), " "again giving the exact value stored by your computer:" msgstr ":meth:`float.hex` 方法会以十六进制(以 16 为基数)来表示浮点数,同样能给出保存在你的计算机中的精确值:" #: ../../tutorial/floatingpoint.rst:203 msgid "" ">>> x.hex()\n" "'0x1.921f9f01b866ep+1'" msgstr "" ">>> x.hex()\n" "'0x1.921f9f01b866ep+1'" #: ../../tutorial/floatingpoint.rst:208 msgid "" "This precise hexadecimal representation can be used to reconstruct the float" " value exactly:" msgstr "这种精确的十六进制表示形式可被用来精确地重建浮点数值:" #: ../../tutorial/floatingpoint.rst:211 msgid "" ">>> x == float.fromhex('0x1.921f9f01b866ep+1')\n" "True" msgstr "" ">>> x == float.fromhex('0x1.921f9f01b866ep+1')\n" "True" #: ../../tutorial/floatingpoint.rst:216 msgid "" "Since the representation is exact, it is useful for reliably porting values " "across different versions of Python (platform independence) and exchanging " "data with other languages that support the same format (such as Java and " "C99)." msgstr "" "由于这种表示法是精确的,它适用于跨越不同版本(平台无关)的 Python 移植数值,以及与支持相同格式的其他语言(例如 Java 和 C99)交换数据." #: ../../tutorial/floatingpoint.rst:220 msgid "" "Another helpful tool is the :func:`sum` function which helps mitigate loss-" "of-precision during summation. It uses extended precision for intermediate " "rounding steps as values are added onto a running total. That can make a " "difference in overall accuracy so that the errors do not accumulate to the " "point where they affect the final total:" msgstr "" "另一个有用的工具是 :func:`sum` 函数,它能够帮助减少求和过程中的精度损失。 它会在数值被添加到总计值的时候为中间舍入步骤使用扩展的精度。 " "这可以更好地保持总体精确度,使得错误不会积累到能够影响最终总计值的程度:" #: ../../tutorial/floatingpoint.rst:226 msgid "" ">>> 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 == 1.0\n" "False\n" ">>> sum([0.1] * 10) == 1.0\n" "True" msgstr "" ">>> 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 == 1.0\n" "False\n" ">>> sum([0.1] * 10) == 1.0\n" "True" #: ../../tutorial/floatingpoint.rst:233 msgid "" "The :func:`math.fsum` goes further and tracks all of the \"lost digits\" as " "values are added onto a running total so that the result has only a single " "rounding. This is slower than :func:`sum` but will be more accurate in " "uncommon cases where large magnitude inputs mostly cancel each other out " "leaving a final sum near zero:" msgstr "" ":func:`math.fsum` 函数进一步追踪在累加过程中“丢失的数位”,因此结果只会经过一次舍入。 相比于 :func:`sum` " "它的执行速度较慢,但在一些不常见的情况下会更加准确,尤其是当大数值输入彼此几乎相互抵消,最终结果接近零时:" #: ../../tutorial/floatingpoint.rst:239 msgid "" ">>> arr = [-0.10430216751806065, -266310978.67179024, 143401161448607.16,\n" "... -143401161400469.7, 266262841.31058735, -0.003244936839808227]\n" ">>> float(sum(map(Fraction, arr))) # Exact summation with single rounding\n" "8.042173697819788e-13\n" ">>> math.fsum(arr) # Single rounding\n" "8.042173697819788e-13\n" ">>> sum(arr) # Multiple roundings in extended precision\n" "8.042178034628478e-13\n" ">>> total = 0.0\n" ">>> for x in arr:\n" "... total += x # Multiple roundings in standard precision\n" "...\n" ">>> total # Straight addition has no correct digits!\n" "-0.0051575902860057365" msgstr "" ">>> arr = [-0.10430216751806065, -266310978.67179024, 143401161448607.16,\n" "... -143401161400469.7, 266262841.31058735, -0.003244936839808227]\n" ">>> float(sum(map(Fraction, arr))) # 精确求和,结果经过一次四舍五入\n" "8.042173697819788e-13\n" ">>> math.fsum(arr) # 一次四舍五入\n" "8.042173697819788e-13\n" ">>> sum(arr) # 多次四舍五入,扩展精度\n" "8.042178034628478e-13\n" ">>> total = 0.0\n" ">>> for x in arr:\n" "... total += x # 多次四舍五入,标准精度\n" "...\n" ">>> total # 直接加法没有一个正确的数字!\n" "-0.0051575902860057365" #: ../../tutorial/floatingpoint.rst:260 msgid "Representation Error" msgstr "表示性错误" #: ../../tutorial/floatingpoint.rst:262 msgid "" "This section explains the \"0.1\" example in detail, and shows how you can " "perform an exact analysis of cases like this yourself. Basic familiarity " "with binary floating-point representation is assumed." msgstr "本小节将详细解释 \"0.1\" 的例子,并说明你可以怎样亲自对此类情况进行精确分析。 假定前提是已基本熟悉二进制浮点表示法。" #: ../../tutorial/floatingpoint.rst:266 msgid "" ":dfn:`Representation error` refers to the fact that some (most, actually) " "decimal fractions cannot be represented exactly as binary (base 2) " "fractions. This is the chief reason why Python (or Perl, C, C++, Java, " "Fortran, and many others) often won't display the exact decimal number you " "expect." msgstr "" ":dfn:`表示性错误` 是指某些(其实是大多数)十进制小数无法以二进制(以 2 为基数的计数制)精确表示这一事实造成的错误。 这就是为什么 " "Python(或者 Perl、C、C++、Java、Fortran 以及许多其他语言)经常不会显示你所期待的精确十进制数值的主要原因。" #: ../../tutorial/floatingpoint.rst:271 msgid "" "Why is that? 1/10 is not exactly representable as a binary fraction. Since" " at least 2000, almost all machines use IEEE 754 binary floating-point " "arithmetic, and almost all platforms map Python floats to IEEE 754 binary64 " "\"double precision\" values. IEEE 754 binary64 values contain 53 bits of " "precision, so on input the computer strives to convert 0.1 to the closest " "fraction it can of the form *J*/2**\\ *N* where *J* is an integer containing" " exactly 53 bits. Rewriting ::" msgstr "" "为什么会这样? 1/10 是无法用二进制小数精确表示的。 至少从 2000 年起,几乎所有机器都使用 IEEE 754 " "二进制浮点运算标准,而几乎所有系统平台都将 Python 浮点数映射为 IEEE 754 binary64 \"双精度\" 值。 IEEE 754 " "binary64 值包含 53 位精度,因此在输入时计算机会尽量将 0.1 转换为以 *J*/2**\\ *N* 形式所能表示的最接近的小数,其中 " "*J* 为恰好包含 53 比特位的整数。 重新将 ::" #: ../../tutorial/floatingpoint.rst:280 msgid "1 / 10 ~= J / (2**N)" msgstr "1 / 10 ~= J / (2**N)" #: ../../tutorial/floatingpoint.rst:282 msgid "as ::" msgstr "写为 ::" #: ../../tutorial/floatingpoint.rst:284 msgid "J ~= 2**N / 10" msgstr "J ~= 2**N / 10" #: ../../tutorial/floatingpoint.rst:286 msgid "" "and recalling that *J* has exactly 53 bits (is ``>= 2**52`` but ``< " "2**53``), the best value for *N* is 56:" msgstr "并且由于 *J* 恰好有 53 位 (即 ``>= 2**52`` 但 ``< 2**53``),*N* 的最佳值为 56:" #: ../../tutorial/floatingpoint.rst:289 msgid "" ">>> 2**52 <= 2**56 // 10 < 2**53\n" "True" msgstr "" ">>> 2**52 <= 2**56 // 10 < 2**53\n" "True" #: ../../tutorial/floatingpoint.rst:294 msgid "" "That is, 56 is the only value for *N* that leaves *J* with exactly 53 bits." " The best possible value for *J* is then that quotient rounded:" msgstr "也就是说,56 是唯一能使 *J* 恰好有 53 位的 *N* 值。 这样 *J* 可能的最佳就是舍入之后的商:" #: ../../tutorial/floatingpoint.rst:297 msgid "" ">>> q, r = divmod(2**56, 10)\n" ">>> r\n" "6" msgstr "" ">>> q, r = divmod(2**56, 10)\n" ">>> r\n" "6" #: ../../tutorial/floatingpoint.rst:303 msgid "" "Since the remainder is more than half of 10, the best approximation is " "obtained by rounding up:" msgstr "由于余数超于 10 的一半,所以最佳近似值可通过向上舍入获得:" #: ../../tutorial/floatingpoint.rst:306 msgid "" ">>> q+1\n" "7205759403792794" msgstr "" ">>> q+1\n" "7205759403792794" #: ../../tutorial/floatingpoint.rst:313 msgid "" "Therefore the best possible approximation to 1/10 in IEEE 754 double " "precision is::" msgstr "因此在 IEEE 754 双精度下可能达到的 1/10 的最佳近似值为::" #: ../../tutorial/floatingpoint.rst:316 msgid "7205759403792794 / 2 ** 56" msgstr "7205759403792794 / 2 ** 56" #: ../../tutorial/floatingpoint.rst:318 msgid "" "Dividing both the numerator and denominator by two reduces the fraction to::" msgstr "分子和分母都除以二则结果小数为::" #: ../../tutorial/floatingpoint.rst:320 msgid "3602879701896397 / 2 ** 55" msgstr "3602879701896397 / 2 ** 55" #: ../../tutorial/floatingpoint.rst:322 msgid "" "Note that since we rounded up, this is actually a little bit larger than " "1/10; if we had not rounded up, the quotient would have been a little bit " "smaller than 1/10. But in no case can it be *exactly* 1/10!" msgstr "" "请注意由于我们做了向上舍入,这个结果实际上略大于 1/10;如果我们没有向上舍入,则商将会略小于 1/10。 但无论如何它都不会是 *精确的* " "1/10!" #: ../../tutorial/floatingpoint.rst:326 msgid "" "So the computer never \"sees\" 1/10: what it sees is the exact fraction " "given above, the best IEEE 754 double approximation it can get:" msgstr "因此计算机永远不会 \"看到\" 1/10: 它实际看到的就是上面所给出的小数,即它能达到的最佳 IEEE 754 双精度近似值:" #: ../../tutorial/floatingpoint.rst:329 msgid "" ">>> 0.1 * 2 ** 55\n" "3602879701896397.0" msgstr "" ">>> 0.1 * 2 ** 55\n" "3602879701896397.0" #: ../../tutorial/floatingpoint.rst:334 msgid "" "If we multiply that fraction by 10\\*\\*55, we can see the value out to 55 " "decimal digits:" msgstr "如果我们将该小数乘以 10\\*\\*55,我们可以看到该值输出 55 个十进制数位:" #: ../../tutorial/floatingpoint.rst:337 msgid "" ">>> 3602879701896397 * 10 ** 55 // 2 ** 55\n" "1000000000000000055511151231257827021181583404541015625" msgstr "" ">>> 3602879701896397 * 10 ** 55 // 2 ** 55\n" "1000000000000000055511151231257827021181583404541015625" #: ../../tutorial/floatingpoint.rst:342 msgid "" "meaning that the exact number stored in the computer is equal to the decimal" " value 0.1000000000000000055511151231257827021181583404541015625. Instead of" " displaying the full decimal value, many languages (including older versions" " of Python), round the result to 17 significant digits:" msgstr "" "这意味着存储在计算机中的确切数字等于十进制数值 " "0.1000000000000000055511151231257827021181583404541015625。 许多语言(包括较旧版本的 " "Python)都不会显示这个完整的十进制数值,而是将结果舍入到 17 位有效数字:" #: ../../tutorial/floatingpoint.rst:347 msgid "" ">>> format(0.1, '.17f')\n" "'0.10000000000000001'" msgstr "" ">>> format(0.1, '.17f')\n" "'0.10000000000000001'" #: ../../tutorial/floatingpoint.rst:352 msgid "" "The :mod:`fractions` and :mod:`decimal` modules make these calculations " "easy:" msgstr ":mod:`fractions` 和 :mod:`decimal` 模块使得这样的计算更为容易:" #: ../../tutorial/floatingpoint.rst:355 msgid "" ">>> from decimal import Decimal\n" ">>> from fractions import Fraction\n" "\n" ">>> Fraction.from_float(0.1)\n" "Fraction(3602879701896397, 36028797018963968)\n" "\n" ">>> (0.1).as_integer_ratio()\n" "(3602879701896397, 36028797018963968)\n" "\n" ">>> Decimal.from_float(0.1)\n" "Decimal('0.1000000000000000055511151231257827021181583404541015625')\n" "\n" ">>> format(Decimal.from_float(0.1), '.17')\n" "'0.10000000000000001'" msgstr "" ">>> from decimal import Decimal\n" ">>> from fractions import Fraction\n" "\n" ">>> Fraction.from_float(0.1)\n" "Fraction(3602879701896397, 36028797018963968)\n" "\n" ">>> (0.1).as_integer_ratio()\n" "(3602879701896397, 36028797018963968)\n" "\n" ">>> Decimal.from_float(0.1)\n" "Decimal('0.1000000000000000055511151231257827021181583404541015625')\n" "\n" ">>> format(Decimal.from_float(0.1), '.17')\n" "'0.10000000000000001'"