python成绩转换,Python中的浮点数到分数转换

While doing exercise on the topic of float type to Fraction type conversion in Python 3.52, I found the difference between the two different ways of conversion.

The first method is:

>>> from fractions import Fraction

>>> x = 1232.23

>>> f = Fraction(*x.as_integer_ratio())

>>> print(f)

2709702426188841/2199023255552 #Answer

The second method is:

>>> from fractions import Fraction

>>> x = 1232.23

>>> f = Fraction(str(x))

>>> print(f)

123223/100 #Answer

I want to know the reason behind these two different answers? Sorry if this is a stupid question , I am new to programming and Python.

Edited: I found a way to convert inaccurate fraction obtained by first method to accurate by limit_denominator method:

>>> from fractions import Fraction

>>> x = 1232.23

>>> f = Fraction(*x.as_integer_ratio())

>>> f = f.limit_denominator(100)

>>> print(f)

123223/100

解决方案

Yet again it's because floating point numbers aren't stored in base-10 (decimal), but in base-2 (binary).

A number that is finite length in base-10 might be a repeating decimal in base-2. And because floats are a fixed size, that repeating decimal gets truncated, resulting in inaccuracies.

When you use as_integer_ratio for a number that's a repeating decimal in base-2, you will get you a somewhat silly fraction as a result of the slight inaccuracies in the base-10 to base-2 conversion. If you divide those two numbers, the value will be very close to to your original number.

For instance, while 1/10 = 0.1 in base-10 and is not a repeating decimal, it is in fact a repeating decimal in base-2. Just like 1/3 = 0.333... in base-10.

>>> (0.1).as_integer_ratio()

(3602879701896397, 36028797018963968)

If Python's output was exact, you would see this even when you enter just 0.1 in the prompt, by getting something like 1.00000...01 as the output. But Python hides this inaccuracy from you in the general case, leading to confusion.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值