使用分数而不是浮点数 [英] Using fractions instead of floats

查看:118
本文介绍了使用分数而不是浮点数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Python进行一些编程,我想到了这个想法:

在使用2/5时使用分数而不是浮点数。


当你试图表示一些数字时会出现问题,例如
a浮点数。你会说,它会告诉你它等于0.40000000000000002。

这很容易修复。 你只需使用decimal.Decimal

class!。好吧,首先,我会需要多余的打字

来计算0.4 + 0.6:


从十进制导入十进制

打印十进制(0.4)+十进制(0.6)


其次,如果我需要求和1/3和0.4,会发生什么?我可以使用

十进制来精确地表示0.4,但是1/3呢?当然,我可以

使用_another_类,它在基数(二进制,十进制,八进制,

十六进制)中工作,其中1/3可以准确表示...提到

使用这两个不同类操作的问题...


所以我认为解决方案是使用分数类型/类,类似于

在Common Lisp中找到。如果你之前使用过CLisp,你只需要输入



(+ 1/3 6/10)

来获得确切的结果结果。 (是的,我也讨厌(运算符arg1 arg2)

语法,但它只是一个例子)。我希望在Python中有类似的东西

,其中除以两个数字给你一个分数,

而不是整数(python 2.x)或浮点数(决定使用python

3.x)。


一个实现可能是这样的:

class frac(object):# PS:这个(对象)的东西将在

python 3.0中删除,对吧?

def __init __(自我,分子,分母):

通过

def __add __(自我,其他):

通过

#...


(我已经完成了压裂类的实现(这意味着,它对我来说很有价值),虽然它很脏,但我很乐意发布它
如果你需要,可以在这里
。)

我的想法,总的来说这个python shell会话是真的:


>> 5/2



2 1/2


>> f = 5/2
f .__ class__



< type''frac''>


>>#或< class''frac''>
(0.6).__class__#将十进制文字视为分数,这样......



< type''frac' >


>> 1/3 + 0.6



14/15


>>#这很简单。 ;)



我想得到一些关于这个想法的反馈。这是否已发布

之前?如果是这样,它被拒绝了吗?什么?

另外,我想知道你是否对最初的

设计有所改进,以及是否适合将其作为PEP发送。

解决方案

andresj< an ********** @ gmail.comwrites:


当你试图表示某个数字时会出现问题,比如
a浮点数中的0.4。



这是一个普遍问题的具体情况,对于任何给定的数字基数,某些非整数数字不能准确

表示为分数。


其次,如果我需要求和1/3和0.4,会发生什么?我可以使用

十进制来精确地表示0.4,但是1/3呢?



总和是多少? (pi)和a ?? 2(sqrt(2))?


所以我认为解决方案是使用分数类型/类


如上所述,小数类只移动确切的

表示。问题,它没有解决它。


-

\事实不会因为被忽略而不复存在。 - |

` \ Aldous Huxley |

_o__)|

Ben Finney


9月30日下午6:48,Ben Finney< bignose + hates-s ... @ benfinney.id.au>

写道:


andresj< andres.j .... @ gmail.comwrites:


当你试图代表一些数字时出现问题,比如0.4 in

a浮动。



这是一个普遍问题的具体情况,对于任何给定数字的b / b $ b $,一些非整数数字不能完全准确

表示为分数。



是的。这就是我的意思,举个例子。


其次,如果我需要加1会发生什么? 3和0.4?我可以使用

十进制来精确地表示0.4,但是1/3呢?



(pi)和2(sqrt(2))之和怎么样?



嗯......老实说,在我写完

最后一篇文章的那一刻,我没有想到这一点......

但是我认为那样,我会更具体,所以我的建议只有

处理*有理数*,而不是非理性。
< blockquote class =post_quotes>
>


所以我认为解决方案是使用分数类型/类



如上所述,分数-number类仅移动确切的

表示问题,它没有解决它。



我完全不明白你说的话,但我认为你是

说它没有解决问题无理数。

这正是这个提案无法解决的问题。 (主要是因为我/ b $ b不知道在正常数学中做任何方式)。


(当我写这篇文章时,我的思绪提醒我根据2

(sqrt(2))进行操作,我认为可以用另一种类似方式解决,

虽然它有一些更具体的用例...但无论如何,

这不属于本提案的范围,因为它们是非理性的

数字。)


9月30日,8:35?pm,andresj< andres.j .... @ gmail.comwrote:


我正在使用Python进行一些编程,我想到了这个想法:

当使用2/5时使用分数而不是浮点数。


当你试图表示一些数字时会出现问题,例如
a浮点数。你会说,它会告诉你它等于0.40000000000000002。

这很容易修复。 你只需使用decimal.Decimal

class!。好吧,首先,我会需要多余的打字

来计算0.4 + 0.6:


从十进制导入十进制

打印十进制(0.4)+十进制(0.6)


其次,如果我需要求和1/3和0.4,会发生什么?我可以使用

十进制来精确地表示0.4,但是1/3呢?当然,我可以

使用_another_类,它在基数(二进制,十进制,八进制,

十六进制)中工作,其中1/3可以准确表示...提到

使用这两个不同类操作的问题...


所以我认为解决方案是使用分数类型/类,类似于

在Common Lisp中找到。如果你之前使用过CLisp,你只需要输入



(+ 1/3 6/10)

来获得确切的结果结果。 (是的,我也讨厌(运算符arg1 arg2)

语法,但它只是一个例子)。我希望在Python中有类似的东西

,其中除以两个数字给你一个分数,

而不是整数(python 2.x)或浮点数(决定使用python

3.x)。


一个实现可能是这样的:

class frac(object):# PS:这个(对象)的东西将在

python 3.0中删除,对吧?

def __init __(自我,分子,分母):

通过

def __add __(自我,其他):

通过

#...


(我已经完成了压裂类的实现(这意味着,它对我来说很有价值),虽然它很脏,但我很乐意发布它

如果你想要它。)


我的想法,总的来说这个python shell会话是真的:


> 5/2



2 1/2


> f = 5/2
f .__ class__



< ;输入''frac''>


>#或< class''frac''>
(0.6).__ class__#Treat decimal文字为分数,因此...



< type''frac''>


> 1/3 + 0.6



14/15


>#这很简单。 ;)



我想得到一些关于这个想法的反馈。这是否已发布

之前?如果是这样,它被拒绝了吗?什么?

另外,我想知道你是否对最初的

设计有所改进,以及是否适合将其作为PEP发送。



gmpy模块具有无限的精确定额。

工作得非常好。


I was doing some programming in Python, and the idea came to my mind:
using fractions instead of floats when doing 2/5.

The problem arises when you try to represent some number, like 0.4 in
a float. It will tell you that it''s equal to 0.40000000000000002.
"This is easy to fix", you may say. "You just use the decimal.Decimal
class!". Well, firsly, there would be an excess of typing I would need
to do to calculate 0.4+0.6:

from decimal import Decimal
print Decimal("0.4")+Decimal("0.6")

Secondly, what happens if I need to sum 1/3 and 0.4? I could use
Decimal to represent 0.4 precisely, but what about 1/3? Sure, I could
use _another_ class which works in a base (binary, decimal, octal,
hexadecimal) in which 1/3 can be represented exactly... Not to mention
the problem of operating with those two different classes...

So the solution I think is using a fraction type/class, similar to the
one found in Common Lisp. If you have used CLisp before, you only need
to type:
(+ 1/3 6/10)
to get the exact result. (Yes, I also hate the (operator arg1 arg2)
syntax, but it''s just an example). I would like to have something
similar in Python, in which dividing two numbers gives you a fraction,
instead of an integer (python 2.x) or a float (decided for python
3.x).

an implementation could be like this:
class frac(object): # PS: This (object) thing will be removed in
python 3.0, right?
def __init__(self, numerator, denominator):
pass
def __add__(self, other):
pass
#...

(I have an implementation of the frac class done (this meaning, it
works for me), and although it''s pretty dirty, I''d be happy to post it
here if you want it.)

My idea, in summary would be that this python shell session is true:

>>5/2

2 1/2

>>f = 5/2
f.__class__

<type ''frac''>

>># or <class ''frac''>
(0.6).__class__ # Treat decimal literals as fractions, so that...

<type ''frac''>

>>1/3+0.6

14/15

>># That was easy. ;)

I would like to get some feedback on this idea. Has this been posted
before? If so, was it rejected? and for what?
Also, I would like to know if you have improvements on the initial
design, and if it would be appropiate to send it as a PEP.

解决方案

andresj <an**********@gmail.comwrites:

The problem arises when you try to represent some number, like 0.4 in
a float.

Which is really a specific case of the general problem that, for any
given number base, some non-integer numbers cannot be exactly
represented as fractions.

Secondly, what happens if I need to sum 1/3 and 0.4? I could use
Decimal to represent 0.4 precisely, but what about 1/3?

What about the sum of ?? (pi) and a??2 (sqrt(2))?

So the solution I think is using a fraction type/class

As explained above, a fractional-number class only shifts the "exact
representation" problem, it doesn''t solve it.

--
\ "Facts do not cease to exist because they are ignored." -- |
`\ Aldous Huxley |
_o__) |
Ben Finney


On Sep 30, 6:48 pm, Ben Finney <bignose+hates-s...@benfinney.id.au>
wrote:

andresj <andres.j....@gmail.comwrites:

The problem arises when you try to represent some number, like 0.4 in
a float.


Which is really a specific case of the general problem that, for any
given number base, some non-integer numbers cannot be exactly
represented as fractions.

Yes. That''s what I meant to say, by giving an example.

Secondly, what happens if I need to sum 1/3 and 0.4? I could use
Decimal to represent 0.4 precisely, but what about 1/3?


What about the sum of (pi) and 2 (sqrt(2))?

Mm... To be honest I hadn''t thought of that in the moment I wrote the
last post...
But I think then, that I''ll be more specific, so that my proposal only
deals with *rational numbers*, not irrationals.

>

So the solution I think is using a fraction type/class


As explained above, a fractional-number class only shifts the "exact
representation" problem, it doesn''t solve it.

I don''t understand completely what you said, but I think you are
saying that it doesn''t solve the problem with irrational numbers.
Which is exactly what this proposal doesn''t solve. (Mainly because I
do not know of any way of doing it in "normal" math).

(While I write this, my mind reminds me of operating with roots " 2
(sqrt(2))", which I think could be solved in another similar way,
although it has some more specific use cases... But in any case,
that''s not in the scope of this proposal, as they are irrational
numbers.)


On Sep 30, 8:35?pm, andresj <andres.j....@gmail.comwrote:

I was doing some programming in Python, and the idea came to my mind:
using fractions instead of floats when doing 2/5.

The problem arises when you try to represent some number, like 0.4 in
a float. It will tell you that it''s equal to 0.40000000000000002.
"This is easy to fix", you may say. "You just use the decimal.Decimal
class!". Well, firsly, there would be an excess of typing I would need
to do to calculate 0.4+0.6:

from decimal import Decimal
print Decimal("0.4")+Decimal("0.6")

Secondly, what happens if I need to sum 1/3 and 0.4? I could use
Decimal to represent 0.4 precisely, but what about 1/3? Sure, I could
use _another_ class which works in a base (binary, decimal, octal,
hexadecimal) in which 1/3 can be represented exactly... Not to mention
the problem of operating with those two different classes...

So the solution I think is using a fraction type/class, similar to the
one found in Common Lisp. If you have used CLisp before, you only need
to type:
(+ 1/3 6/10)
to get the exact result. (Yes, I also hate the (operator arg1 arg2)
syntax, but it''s just an example). I would like to have something
similar in Python, in which dividing two numbers gives you a fraction,
instead of an integer (python 2.x) or a float (decided for python
3.x).

an implementation could be like this:
class frac(object): # PS: This (object) thing will be removed in
python 3.0, right?
def __init__(self, numerator, denominator):
pass
def __add__(self, other):
pass
#...

(I have an implementation of the frac class done (this meaning, it
works for me), and although it''s pretty dirty, I''d be happy to post it
here if you want it.)

My idea, in summary would be that this python shell session is true:

>5/2

2 1/2

>f = 5/2
f.__class__

<type ''frac''>

># or <class ''frac''>
(0.6).__class__ # Treat decimal literals as fractions, so that...

<type ''frac''>

>1/3+0.6

14/15

># That was easy. ;)


I would like to get some feedback on this idea. Has this been posted
before? If so, was it rejected? and for what?
Also, I would like to know if you have improvements on the initial
design, and if it would be appropiate to send it as a PEP.

The gmpy module has unlimited precision rationals.
Works pretty good, too.


这篇关于使用分数而不是浮点数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆