一个理性的提议 [英] A rational proposal

查看:79
本文介绍了一个理性的提议的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

PEP:XXX

标题:Python的有理数模块

版本:$修订版:1.4 $

最后修改:$日期:2003/09/22 04:51:50 $

作者:Mike Meyer< mw*@mired.org>

状态:草稿

类型:Staqndards

内容类型:text / x-rst

创建时间:2004年12月16日

Python-Version: 2.5

后历史:2002年8月30日

摘要

========

这个PEP提出了一个有理数的模块来添加到Python

标准库中。

理由

===== ====


Rationals是一个标准的数学概念,已经包含在各种编程语言中。包含''电池

包含''的Python不应该在这方面存在缺陷。当主题是在comp.lang.python上提出
时,有几个人提到

实现了一个有理数模块,一个人不止一次。在

中,有一个有理数的模块与Python一起分发为

示例模块。这样的重复表明在

标准库中需要这样的类。


目前有两个处理有理数的PEP - ''添加

Rational Type to Python''[#PEP-239]和''将一个Rational Literal添加到
Python''[#PEP-240],由Craig和Zadka提供。这个PEP与那些PEP竞争,但不会像那两个PEP那样改变Python语言。[#PEP-239-implicit]。因此,它应该更容易获得

接受。在未来的某个时间,PEP的239和240可能会取代

`` rational``模块。

规格

=== ==========


模块应为``rational``,类为``Rational``,为

按照十进制[#PEP-327]模块的例子。类

创建方法应接受分子和可选的

分母作为参数,默认为1。分子和

分母 - 如果存在 - 必须是整数类型。由于Python中的所有其他
数值类型都是不可变的,因此Rational对象将是不可变的
。在内部,该表示将确保

分子和分母具有1的最大公约数,并且

表示分母的符号为正。


``Rational``类应定义所有标准数学

操作:加法,减法,乘法,除法,模数

和幂。它还将提供方法:


- max(* args):返回最大的数字列表和自我。

- min(* args ):返回最小的数字列表和自我。

- decimal():在

当前上下文中返回小数近似值。

- inv():返回self的反转。


Rationals将与所有其他数字类型混合使用。当与

整数类型结合使用时,该整数将在

操作之前转换为有理数。当与浮动类型结合使用时 - 无论是复数还是

浮点数 - 在操作之前,有理数将被转换为浮点近似值b / b $ b $,并且将返回浮点数或复数。

的原因是浮点数 - 包括复数 -

已经不精确了。将它们转换为理性会给出一个

错误的印象,即操作的结果是精确的
。在

操作之前,小数将转换为有理数。 [公开提问:这是正确的做法吗?]


Rational可以通过float(理性)转换为浮点数,并通过int转换为
整数(理性)。


模块将定义并有时引发以下异常:


- DivisionByZero:除以零

- 溢出错误:溢出试图转换为浮点数。


实施

==============


目前有一个用Python分发的合理模块,以及在Python cvs源代码树中的第二个理性模块,它不是

分发。虽然其中一个可以选择并使其符合规范,但我希望有几个人能够自愿提供实施,以便做出最好的品种。 ''''实施可能是

选择。

参考资料

==========


... [#PEP-239]将一个Rational Type添加到Python,Craig,Zadka

http://www.python.org/peps/pep-0239.html

.. 。[#PEP-240]将Rational Literal添加到Python,Craig,Zadka

http://www.python.org/peps/pep-0240.html

... [#PEP-327]十进制数据类型,Batista

http:/ /www.python.org/peps/pep-0327.html

... [#PEP-239-implicit] PEP 240为Pytbon添加了一个新的文字类型,

PEP 239暗示整数除法将改为返回有效数据。

版权

==== =====


本文档已被置于公共领域。


...

局部变量:

模式:缩进文本

indent-tabs-mode:nil

句末 - 双空格:t

fill-column:70

结束:

PEP: XXX
Title: A rational number module for Python
Version: $Revision: 1.4 $
Last-Modified: $Date: 2003/09/22 04:51:50 $
Author: Mike Meyer <mw*@mired.org>
Status: Draft
Type: Staqndards
Content-Type: text/x-rst
Created: 16-Dec-2004
Python-Version: 2.5
Post-History: 30-Aug-2002
Abstract
========

This PEP proposes a rational number module to add to the Python
standard library.
Rationale
=========

Rationals are a standard mathematical concept, included in a variety
of programming languages already. Python, which comes with ''batteries
included'' should not be deficient in this area. When the subject was
brought up on comp.lang.python several people mentioned having
implemented a rational number module, one person more than once. In
fact, there is a rational number module distributed with Python as an
example module. Such repetition shows the need for such a class in the
standard library.

There are currently two PEPs dealing with rational numbers - ''Adding a
Rational Type to Python'' [#PEP-239] and ''Adding a Rational Literal to
Python'' [#PEP-240], both by Craig and Zadka. This PEP competes with
those PEPs, but does not change the Python language as those two PEPs
do [#PEP-239-implicit]. As such, it should be easier for it to gain
acceptance. At some future time, PEP''s 239 and 240 may replace the
``rational`` module.
Specification
=============

The module shall be ``rational``, and the class ``Rational``, to
follow the example of the decimal [#PEP-327] module. The class
creation method shall accept as arguments a numerator, and an optional
denominator, which defaults to one. Both the numerator and
denominator - if present - must be of integer type. Since all other
numeric types in Python are immutable, Rational objects will be
immutable. Internally, the representation will insure that the
numerator and denominator have a greatest common divisor of 1, and
that the sign of the denominator is positive.

The ``Rational`` class shall define all the standard mathematical
operations: addition, subtraction, multiplication, division, modulo
and power. It will also provide the methods:

- max(*args): return the largest of a list of numbers and self.
- min(*args): return the smallest of a list of numbers and self.
- decimal(): return the decimal approximation to the rational in the
current context.
- inv(): Return the inverse of self.

Rationals will mix with all other numeric types. When combined with an
integer type, that integer will be converted to a rational before the
operation. When combined with a floating type - either complex or
float - the rational will be converted to a floating approximation
before the operation, and a float or complex will be returned. The
reason for this is that floating point numbers - including complex -
are already imprecise. To convert them to rational would give an
incorrect impression that the results of the operation are
precise. Decimals will be converted to rationals before the
operation. [Open question: is this the right thing to do?]

Rationals can be converted to floats by float(rational), and to
integers by int(rational).

The module will define and at times raise the following exceptions:

- DivisionByZero: divide by zero
- OverflowError: overflow attempting to convert to a float.

Implementation
==============

There is currently a rational module distributed with Python, and a
second rational module in the Python cvs source tree that is not
distributed. While one of these could be chosen and made to conform
to the specification, I am hoping that several people will volunteer
implementatins so that a ''''best of breed'''' implementation may be
chosen.
References
==========

... [#PEP-239] Adding a Rational Type to Python, Craig, Zadka
(http://www.python.org/peps/pep-0239.html)
... [#PEP-240] Adding a Rational Literal to Python, Craig, Zadka
(http://www.python.org/peps/pep-0240.html)
... [#PEP-327] Decimal Data Type, Batista
(http://www.python.org/peps/pep-0327.html)
... [#PEP-239-implicit] PEP 240 adds a new literal type to Pytbon,
PEP 239 implies that division of integers would
change to return rationals.
Copyright
=========

This document has been placed in the public domain.

...
Local Variables:
mode: indented-text
indent-tabs-mode: nil
sentence-end-double-space: t
fill-column: 70
End:

推荐答案

修订:1.4




Last-Modified:

Last-Modified:


日期:2003/09/22 04:51:50
Date: 2003/09/22 04:51:50


这篇关于一个理性的提议的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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