繁体   English   中英

如何在python中使用整数运算将分数转换为浮点数?

[英]How do I use integer arithmetic to convert fractions into floating point numbers in python?

我需要做的是使用整数运算将分数转换为浮点数。 所需的小数位数被指定为变量DECIMALS 每个分数包含在整数元组中,例如(1, 3) 第一项是分子,第二项是分母。 元组包含在名为fractions的列表中。

到目前为止这是我的代码:

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位小数; 其余的都是错的。 我考虑将其分开,以便我可以单独计算分数以使它们更容易格式化,但我不知道如何做到这一点。 问题是所有分数都需要由相同的代码处理。 我也希望它能够正确地完成,但现在这不是什么大问题。

你的意思是这样的:

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(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

Fraction模块是默认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

首先避免使用浮点数。

>>> 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']

如果严格格式化,正如您的问题所暗示的那样,当然,您可以单独使用整数和字符串来执行此操作:

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',除以零等)。

我想是在紧要关头工作。

为什么不使用包括的电池,如十进制或分数?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM