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

Python浮動(dòng)比率

Python float to ratio(Python浮動(dòng)比率)
本文介紹了Python浮動(dòng)比率的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問題描述

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

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

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

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

以及為什么它適用于 2.5

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

推薦答案

使用 fractions 模塊 簡(jiǎn)化分?jǐn)?shù):

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)

來(lái)自 Fraction.limit_denominator() 函數(shù)一個(gè)>:

From the Fraction.limit_denominator() function:

查找并返回與 self 最接近且分母最多為 max_denominator 的 Fraction.此方法對(duì)于找到給定浮點(diǎn)數(shù)的有理逼近很有用

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

浮點(diǎn)數(shù)精度有限,不能精確表示;你看到的是一個(gè)四舍五入的表示,但實(shí)數(shù)是:

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'

因?yàn)楦↑c(diǎn)數(shù)表示為二進(jìn)制小數(shù)的總和;1/5 只能通過將 1/8 + 1/16 + 1/128 + 更多的二進(jìn)制分?jǐn)?shù)相加來(lái)表示,以增加 2 的指數(shù).

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.

這篇關(guān)于Python浮動(dòng)比率的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

相關(guān)文檔推薦

Python 3 Float Decimal Points/Precision(Python 3 浮點(diǎn)小數(shù)點(diǎn)/精度)
Converting Float to Dollars and Cents(將浮點(diǎn)數(shù)轉(zhuǎn)換為美元和美分)
What are some possible calculations with numpy or scipy that can return a NaN?(numpy 或 scipy 有哪些可能的計(jì)算可以返回 NaN?)
How to manage division of huge numbers in Python?(如何在 Python 中管理大量數(shù)字的除法?)
mean from pandas and numpy differ(pandas 和 numpy 的意思不同)
Function to determine if two numbers are nearly equal when rounded to n significant decimal digits(用于確定兩個(gè)數(shù)字在舍入到 n 個(gè)有效十進(jìn)制數(shù)字時(shí)是否幾乎相等的函數(shù))
主站蜘蛛池模板: 国产免费视频 | 一区二区三区精品视频 | 国产福利在线播放麻豆 | 亚洲黄色av | 欧美激情国产日韩精品一区18 | 久久九九99| 中文字幕精品一区二区三区精品 | 久久久久久成人 | avhd101在线成人播放 | 日日做夜夜爽毛片麻豆 | 国产小视频在线观看 | 国产精品欧美一区二区三区不卡 | 在线播放中文字幕 | 亚洲欧洲精品成人久久奇米网 | 黄色一级大片在线免费看产 | 99国产精品视频免费观看一公开 | 国产激情三区 | 神马影院一区二区三区 | 成人av一区 | 男女午夜激情视频 | av免费网站在线观看 | 在线观看av网站永久 | www.天天干.com | 亚洲网址 | 亚洲精品一区二区另类图片 | 欧美综合国产精品久久丁香 | 国产精品自拍视频 | 天天色天天射天天干 | 天天久| 亚洲精品视频在线播放 | 成人av播放| 日本一区高清 | 亚洲成人免费在线观看 | 免费一区二区三区 | 超碰成人免费观看 | 国产精品久久久久久久一区二区 | 国产在线一区二 | 欧美中文在线 | 欧美中文字幕一区二区三区亚洲 | 国产欧美精品一区二区 | 人和拘一级毛片c |