# Copyright (C) 2001-2024, Python Software Foundation # This file is distributed under the same license as the Python package. # # Translators: # yichung279, 2018 # cypevan, 2018 # Adrian Liaw , 2018 # Steven Hsu , 2021 msgid "" msgstr "" "Project-Id-Version: Python 3.13\n" "Report-Msgid-Bugs-To: \n" "POT-Creation-Date: 2024-09-03 11:11+0800\n" "PO-Revision-Date: 2023-06-22 14:43+0800\n" "Last-Translator: Matt Wang \n" "Language-Team: Chinese - TAIWAN (https://github.com/python/python-docs-zh-" "tw)\n" "Language: zh_TW\n" "MIME-Version: 1.0\n" "Content-Type: text/plain; charset=UTF-8\n" "Content-Transfer-Encoding: 8bit\n" "Plural-Forms: nplurals=1; plural=0;\n" "X-Generator: Poedit 3.3.1\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 "" "在計算機架構中,浮點數 (floating-point number) 是以基數為 2(二進位)的小數表" "示。例如說,在\\ **十進位**\\ 小數中 ``0.625`` 可被分為 6/10 + 2/100 + " "5/1000,同樣的道理,**二進位**\\ 小數 ``0.101`` 可被分為 1/2 + 0/4 + 1/8。這" "兩個小數有相同的數值,而唯一真正的不同在於前者以十進位表示,後者以二進位表" "示。" #: ../../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 "" "同樣的道理,不論你願意以多少位數表示二進位小數,十進位小數 0.1 都無法被二進位" "小數精準地表達。在二進位小數中,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 "" "只要你停在任何有限的位數,你就只會得到近似值。而現在大多數的計算機中,浮點數" "是透過二進位分數近似的,其中分子是從最高有效位元開始,用 53 個位元表示,分母" "則是以二為底的指數。在 1/10 的例子中,二進位分數為 ``3602879701896397 / 2 ** " "55``,而這樣的表示十分地接近,但不完全等同於 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,但真正儲存的數值是能表示的二進" "位分數中,最接近精準數值的數。" #: ../../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 的提示字元 (prompt) 與內建的 :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 的錯誤 (bug),更不是你程" "式碼的錯誤。只要程式語言有支援硬體的浮點數運算,你將會看到同樣的事情出現在其" "中(雖然有些程式語言預設不會\\ *顯示*\\ 該差異,有些甚至是在所有的輸出模式中" "都不會顯示。)" #: ../../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 "" "為求更優雅的輸出,你可能想要使用字串的格式化 (string formatting) 產生限定的有" "效位數:" #: ../../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 "" "二進位浮點數架構下還有很多這樣的意外。底下的「表示法誤差 (Representation " "Error)」章節,詳細地解釋了「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 的 float(浮點數)運算中的誤差,是從浮點硬體繼承而來,而在大多數的" "計算機上,每次運算的誤差範圍不會大於 2\\*\\*53 分之 1。這對大多數的任務來說已" "經相當足夠,但你需要記住,它並非十進位運算,且每一次 float 運算都可能會承受新" "的捨入誤差。" #: ../../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` method(方法)的格式規範。" #: ../../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 "" "在罕見情況下,當你\\ *真的*\\ 想知道一個 float 的精準值,Python 提供的工具可" "協助達成。:meth:`float.as_integer_ratio` method 可將一個 float 的值表示為分" "數:" #: ../../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` method 以十六進位(基數為 16)表示 float,一樣可以給出你的" "電腦所儲存的精準值:" #: ../../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 "這種精確的十六進位表示法可用於精準地重建 float 值:" #: ../../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` 函式,能在計算總和時幫忙減少精確度的損失。當數" "值被加到運行中的總計值時,它會追蹤「失去的位數 (lost digits)」。這可以明顯改" "善總體準確度 (overall accuracy),使得誤差不至於累積到影響最終總計值的程度:" #: ../../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 "表示法誤差 (Representation Error)" #: ../../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:`Representation error`\\ (表示法誤差)是指在真實情況中,有些(實際上是" "大多數)十進位小數並不能精準地以二進位(基數為 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 浮點數運算標準,並且幾乎所有的平台都以 IEEE " "754 binary64 標準中的「雙精度 (double precision)」來作為 Python 的 float。" "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 "" "意即,要使 *J* 正好有 53 位元,則 56 會是 *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 double 最佳近似值: ::" #: ../../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'"