2012-06-11 37 views
1

我需要做的是使用整數算術將分數轉換爲浮點數。所需的小數位數被指定爲變量DECIMALS。每個分數都包含在一個整數元組中,例如(1, 3)。第一項是分子,第二項是分母。這些元組包含在名爲fractions的列表中。如何使用整數算術將分數轉換爲python中的浮點數?

這是到目前爲止我的代碼:

fractions = [(1,7), (2,3), (22,7), (7001,7), (9,3), (611951,611953), (1,11), (1,7689585)] 

DECIMALS = 10**40 # 40 decimal places 

for fraction in fractions: 
    x = DECIMALS*fraction[0]/fraction[1] 
    print x 

當我運行的代碼,這是我所得到的:

1428571428571428571428571428571428571428 
6666666666666666666666666666666666666666 
31428571428571428571428571428571428571428 
10001428571428571428571428571428571428571428 
30000000000000000000000000000000000000000 
9999967317751526669531810449495304377950 
909090909090909090909090909090909090909 
1300460297922449651053990559958697 

的問題是,我需要這種格式到正確的十進制格式。我試過的是

print "0.%.40d" % (x) 

但當然,這隻會幫助我的前2位小數;其餘的將是錯誤的。我想分割它,所以我可以單獨計算分數,使它們更容易格式化,但我不知道如何做到這一點。問題在於所有的分數都需要用相同的代碼進行處理。我也想讓它四捨五入,但那不是什麼大事。

+2

很難確定問題在這裏。可能http://docs.python.org/library/decimal.html做你想做的事情? – msw

+0

你到底想要做什麼? –

+0

好了,現在編輯它,希望你們這一次能理解這個問題。 – 321

回答

0

如果嚴格的格式,如暗示你的問題,你可以,當然,與整數和字符串單獨做到這一點:

def intF(n, d, l=40): 
    s=str(n*10**l/d) 
    if len(s) < l: 
     return '0.{:0>{width}}'.format(s,width=l) 
    if len(s) > l: 
     return s[0:len(s)-l]+'.'+s[len(s)-l:] 

    return '0.'+s 


for f in [(1,7), (2,3), (22,7), (7001,7), (9,3), 
      (611951,611953), (1,11),(1,7689585)]: 
    print intF(*f) 
    print float(f[0])/f[1] 
    print 

輸出:

0.1428571428571428571428571428571428571428 
0.142857142857 

0.6666666666666666666666666666666666666666 
0.666666666667 

3.1428571428571428571428571428571428571428 
3.14285714286 

1000.1428571428571428571428571428571428571428 
1000.14285714 

3.0000000000000000000000000000000000000000 
3.0 

0.9999967317751526669531810449495304377950 
0.999996731775 

0.0909090909090909090909090909090909090909 
0.0909090909091 

0.0000001300460297922449651053990559958697 
1.30046029792e-07 

最後數字將不能正確舍入,並且不能正確處理邊緣情況('inf','NaN',零除等)。

工作在一個捏我想。

爲什麼不使用包含的電池,如十進制或分數?

+0

即時通訊不知道我明白這個功能和這部分是怎麼回事:print intF(* f)。你能解釋一下嗎?還有什麼是電池?即時通訊實際上是新的Python和編程,所以我不知道它是什麼。 – 321

+0

實際上你不需要解釋打印intF(* f)了。我明白這一部分,但我仍然會很感激它,如果你能解釋這個函數,更具體地說是什麼在i循環中發生。 thx – 321

+1

@ 321我認爲他提到的'電池'是許多選擇專門執行任務的Python模塊(如Decimal和Fraction)。據說*電池包括*因爲Python有很多高質量的模塊來完成專門的任務。作爲你學習語言的一部分,學習這些模塊。除非你真的很好,否則你應該想知道爲什麼你做的事情不同於其中一個分配模塊要你做的任務。該函數沒有循環。它只是格式化你建議的數學結果的字符串。這裏唯一的循環是通過元組遍歷你的列表 – dawg

2

你的意思是這樣的:

from fractions import Fraction 
import decimal 

decimal.getcontext().prec = 50 

fractions = [(1,7), (2,3), (22,7), (7001,7), (9,3), (611951,611953), (1,11),(1,7689585)] 

su=Fraction(0) 
for i, t in enumerate(fractions,1): 
    f=Fraction(*t) 
    su+=Fraction(*t) 
    d=decimal.Decimal(f.numerator)/decimal.Decimal(f.denominator) 
    print '{} becomes {}'.format(t,f) 
    print '\tfloat of that is: {}'.format(float(f)) 
    print '\tDecimal: {}'.format(d) 
    print '\t{} elements cum sum of the list: {}\n'.format(i,su) 

打印:

(1, 7) becomes 1/7 
    float of that is: 0.142857142857 
    Decimal: 0.14285714285714285714285714285714285714285714285714 
    1 elements cum sum of the list: 1/7 

(2, 3) becomes 2/3 
    float of that is: 0.666666666667 
    Decimal: 0.66666666666666666666666666666666666666666666666667 
    2 elements cum sum of the list: 17/21 

(22, 7) becomes 22/7 
    float of that is: 3.14285714286 
    Decimal: 3.1428571428571428571428571428571428571428571428571 
    3 elements cum sum of the list: 83/21 

(7001, 7) becomes 7001/7 
    float of that is: 1000.14285714 
    Decimal: 1000.1428571428571428571428571428571428571428571429 
    4 elements cum sum of the list: 21086/21 

(9, 3) becomes 3 
    float of that is: 3.0 
    Decimal: 3 
    5 elements cum sum of the list: 21149/21 

(611951, 611953) becomes 611951/611953 
    float of that is: 0.999996731775 
    Decimal: 0.99999673177515266695318104494953043779505942449829 
    6 elements cum sum of the list: 12955044968/12851013 

(1, 11) becomes 1/11 
    float of that is: 0.0909090909091 
    Decimal: 0.090909090909090909090909090909090909090909090909091 
    7 elements cum sum of the list: 142518345661/141361143 

(1, 7689585) becomes 1/7689585 
    float of that is: 1.30046029792e-07 
    Decimal: 1.3004602979224496510539905599586973809379829990825E-7 
    8 elements cum sum of the list: 121767437017889092/120778724977295 

fraction模塊可讓您與有理數工作(無轉換爲浮動)。一旦你把它們放入一個Fraction類,你可以做算術與他們(就像在小學)

像這樣:

>>> Fraction(1,3) + Fraction(3,4) 
Fraction(13, 12) 
>>> Fraction(1,3) + Fraction(1,6) 
Fraction(1, 2) 
>>> Fraction(1,2).numerator 
1 
>>> Fraction(1,2).denominator 
2 

的分數模塊是默認的Python發行版的一部分(自2.6) 。

要將其轉換爲浮點數,請執行float(Fraction(17,18))例如或在Decimal類變量中使用Fraction.numeratorFraction().denominator進行任意精度轉換。

像這樣:

>>> decimal.Decimal(su.numerator)/decimal.Decimal(su.denominator) 
Decimal('1008.186144047968368606384293') 

或:

>>> float(su.numerator)/su.denominator 
1008.1861440479684 
+0

srry我不能做浮動(#)。我需要使用純整數算術。就像我在問題中所做的一樣。 – 321

1

避免浮點數擺在首位。

>>> decimal.getcontext().prec = 50 
>>> [str((decimal.Decimal(x)/decimal.Decimal(y)).quantize(decimal.Decimal(10) ** -40)) for (x, y) in FRACTIONS] 
['0.1428571428571428571428571428571428571429', '0.6666666666666666666666666666666666666667', '3.1428571428571428571428571428571428571429', '1000.1428571428571428571428571428571428571429', '3.0000000000000000000000000000000000000000', '0.9999967317751526669531810449495304377951', '0.0909090909090909090909090909090909090909', '1.300460297922449651053990559958697E-7']