久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

Python浮動比率

Python float to ratio(Python浮動比率)
本文介紹了Python浮動比率的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我嘗試獲取變量的比例并得到意想不到的結果.有人可以解釋一下嗎?

I try get ration of variable and get unexpected result. Can somebody explain this?

>>> value = 3.2
>>> ratios = value.as_integer_ratio()
>>> ratios
(3602879701896397, 1125899906842624)
>>> ratios[0] / ratios[1]
3.2

我使用 python 3.3

I using python 3.3

但我認為 (16, 5) 是更好的解決方案

But I think that (16, 5) is much better solution

以及為什么它適用于 2.5

>>> value = 2.5
>>> value.as_integer_ratio()
(5, 2)

推薦答案

使用 fractions 模塊 簡化分數:

Use the fractions module to simplify fractions:

>>> from fractions import Fraction
>>> Fraction(3.2)
Fraction(3602879701896397, 1125899906842624)
>>> Fraction(3.2).limit_denominator()
Fraction(16, 5)

來自 Fraction.limit_denominator() 函數一個>:

From the Fraction.limit_denominator() function:

查找并返回與 self 最接近且分母最多為 max_denominator 的 Fraction.此方法對于找到給定浮點數的有理逼近很有用

Finds and returns the closest Fraction to self that has denominator at most max_denominator. This method is useful for finding rational approximations to a given floating-point number

浮點數精度有限,不能精確表示;你看到的是一個四舍五入的表示,但實數是:

Floating point numbers are limited in precision and cannot represent many numbers exactly; what you see is a rounded representation, but the real number is:

>>> format(3.2, '.50f')
'3.20000000000000017763568394002504646778106689453125'

因為浮點數表示為二進制小數的總和;1/5 只能通過將 1/8 + 1/16 + 1/128 + 更多的二進制分數相加來表示,以增加 2 的指數.

because a floating point number is represented as a sum of binary fractions; 1/5 can only be represented by adding up 1/8 + 1/16 + 1/128 + more binary fractions for increasing exponents of two.

這篇關于Python浮動比率的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Python 3 Float Decimal Points/Precision(Python 3 浮點小數點/精度)
Converting Float to Dollars and Cents(將浮點數轉換為美元和美分)
What are some possible calculations with numpy or scipy that can return a NaN?(numpy 或 scipy 有哪些可能的計算可以返回 NaN?)
How to manage division of huge numbers in Python?(如何在 Python 中管理大量數字的除法?)
mean from pandas and numpy differ(pandas 和 numpy 的意思不同)
Function to determine if two numbers are nearly equal when rounded to n significant decimal digits(用于確定兩個數字在舍入到 n 個有效十進制數字時是否幾乎相等的函數)
主站蜘蛛池模板: 欧美男人亚洲天堂 | 久草在线 | 欧美精品一区三区 | 日韩福利 | 一区二区三区视频在线 | 久久综合色综合 | 精品久| 久久综合九色综合欧美狠狠 | 久久久久亚洲精品国产 | 国产精品区一区二区三 | 91亚洲精 | 奇米影视首页 | 午夜欧美a级理论片915影院 | 蜜桃在线播放 | 欧美久久久久久久久 | 久久性色| 一区二区三区免费网站 | 亚洲毛片在线观看 | 国产亚洲第一页 | 7777精品伊人久久精品影视 | 精品国产一级片 | 六月婷婷久久 | 欧美国产日韩在线观看 | 二区成人 | 日韩中文不卡 | 在线观看特色大片免费网站 | 久热久热| 亚洲第一av | 免费国产成人av | 99亚洲精品 | 国产精品99久久久久久久久久久久 | 日韩成年人视频在线 | 欧美一级淫片免费视频黄 | 精品一区二区三区日本 | 国产精品欧美一区二区三区 | 激情六月天 | 人人鲁人人莫人人爱精品 | 欧美大片一区二区 | 欧美国产亚洲一区二区 | 精品免费国产 | 自拍偷拍av |