prePEP:十进制数据类型 [英] prePEP: Decimal data type

查看:93
本文介绍了prePEP:十进制数据类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发送给它。


建议和各种推荐不仅仅是欢迎。


如果一切顺利,那就是它当我写完/修改代码时,我将成为PEP。


谢谢。


.. Facundo

----------------------------------------------- -------------------------


PEP:XXXX

标题:十进制数据类型

版本:$修订版:0.1 $

最后修改日期:$ Date:2003/10/31 15:25:00 $

作者:Facundo Batista< fb ****** @ unifon.com.ar>

状态:草稿

类型:标准追踪

内容类型:text / x-rst

创建时间:2003年10月17日

Python-Version:2.3.3

摘要

========


我们的想法是拥有一个Decimal数据类型,适用于小数位数为$ / $的每个用途需要b $ b但是浮点太不精确了。


十进制数据类型应该支持Python标准函数和

操作并且必须遵守十进制算术ANSI标准X3.274-1996。

理由

=========


我必须将两个部分分开。第一种是遵守ANSI标准

。所有需要都在

Mike Cowlishaw的工作中指定 http://www2.hursley.ibm.com/decimal/ 。 Cowlishaw的

也提供了** **测试用例。第二部分requeriments

(标准Python函数支持,可用性等)详见

`要求`_部分。


在这里,我将包括所做出的所有决定以及为什么,所有主题仍在讨论中。这些要求将被编号,以简化每一点的讨论




这项工作基于Eric Price,Aahz撰写的代码和测试函数/>


Tim Peters。实际上我会处理沙盒中的Decimal.py代码(在SourceForge中的
python / nondist / sandbox / decimal)。这个PEP的一些解释来自Cowlishaw的作品。

讨论中的项目

------ -------------


在类似``Decimal op otherType``的情况下(参见Requirements_
$ b $中的第12点) b详细信息),会发生什么?


如果otherType是int或long:


a。提出异常

b。 otherType转换为十进制

c。十进制转换为int或long(使用``int()``或

``long()``)


如果otherType是浮点数:


d。提出异常

e。 otherType转换为十进制(舍入?参见

讨论中的下一项)

f。十进制转换为float(带``float()``)


如果otherType是一个字符串:


g。提出异常

h。 otherType转换为Decimal

i。十进制被转换为字符串(奇怪,是吗?)

当将浮点数传递给构造函数时,会发生什么?


j。 ``十进制(1.1)==十进制(''1.1'')``

k。 ``十进制(1.1)==

十进制('''110000000000000008881784197001252 ... e-51'')``

要求

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

1.语法应该是``十进制(值)``。


2.价值可以是以下类型:


- 另一个十进制

- int或long

- 浮动
- 字符串


3.存在上下文。上下文代表用户可选的

参数

以及控制算术运算结果的规则。在

上下文中,用户定义:

- 特殊情况会发生什么。

- 将使用的精度

- 将使用什么舍入方法


4.上下文必须无所不在,这意味着它的更改会影响所有

当前和未来的十进制实例。


5.特殊条件应分为信号,可以单独控制
。上下文应该包含一个标志和每个信号的

陷阱使能器。信号应该是:钳位,

除零,不精确,无效操作,溢出,舍入,

次正常

和下溢。


6.对于每个信号,当信号发生时,相应的标志应设置为1。它只能通过明确的用户操作重置为0.


7.对于每个信号,相应的陷阱启用器将指示

哪个动作是在信号发生时采取。如果为0,则应提供定义的

结果,并继续执行。如果为1,则操作的执行结束时应该结束并且应该引发异常。


8.精度(可以产生的最大有效位数)从

算术运算)必须是正数(大于0)。


9.进行不同类型的舍入;你可以通过

上下文选择算法:


- ``round-down`` :(向0舍入,截断)丢弃的数字是

被忽略;结果不变::


1.123 - > 1.12

1.128 - > 1.12

1.125 - > 1.12

1.135 - > 1.13


- ``round-half-up``:如果丢弃的数字代表大于



等于一半(0.5)然后结果应增加1

(四舍五入);否则丢弃的数字将被忽略::


1.123 - > 1.12

1.128 - > 1.13

1.125 - > 1.13

1.135 - > 1.14


- ``round-half-even``:如果丢弃的数字代表大于

的一半(0.5)那么结果系数应该增加由1

(四舍五入);如果它们代表不到一半,那么结果就是没有调整(即丢弃的数字被忽略);否则

如果最右边的数字是偶数,则结果不会改变,或者如果最右边的数字是奇数,则结果是
递增1(向上舍入)(

make

偶数位数)::


1.123 - > 1.12

1.128 - > 1.13

1.125 - > 1.12

1.135 - > 1.14


- ``round-ceiling``:如果所有丢弃的数字都是零或者



的标志否定结果不变;否则,结果

应增加1(向上舍入)::


1.123 - > 1.13

1.128 - > 1.13

-1.123 - > -1.12

-1.128 - > -1.12


- ``round-floor``:如果所有丢弃的数字都为零或者

符号是正数,则结果不变;否则,结果的绝对值应该增加1 ::


1.123 - > 1.12

1.128 - > 1.12

-1.123 - > -1.13

-1.128 - > -1.13


- ``round-half-down``:如果丢弃的数字代表大于

half(0.5)那么结果应该增加乘以1(向上舍入);

否则忽略丢弃的数字::


1.123 - > 1.12

1.128 - > 1.13

1.125 - > 1.12

1.135 - > 1.13


- ``round-up`` :(圆满0)如果所有丢弃的数字

都是

为零,结果不变。否则,结果应该是

加1(向上舍入)::


1.123 - > 1.13

1.128 - > 1.13

1.125 - > 1.13

1.135 - > 1.14


10.支持带有工程符号浮点数的字符串。


11.调用repr()应该进行往返,这意味着::


m =十进制(...)

m == eval(repr(m))


12.支持基本的aritmetic(``+, - ,*,/,//,**,%,divmod``)和

比较(``==,!=,< ;,>,< =,> =,cmp``)以下运算符

情况:


- 十进制运算十进制
- 十进制op otherType

- otherType op十进制

- 十进制op =十进制

- 十进制op = otherType


检查讨论中的项目_以查看OtherType可以使用的类型,以及

每种情况下会发生什么。


13.支持一元运算符(`` - ,+,abs``)。


14.支持内置方法:


- 分钟,最大值

- 浮点数,整数,长

- str,repr

- 哈希

- 复制,深度复制

- 嘘l(0为假,否则为真)


15.为不可变。

参考实施

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


稍后加入:


- 代码

- 测试代码

- 文档

版权

=========

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

解决方案

修订版:0.1



Last-Modified:


日期:2003/10/31 15:25:00

Here I send it.

Suggestions and all kinds of recomendations are more than welcomed.

If it all goes ok, it''ll be a PEP when I finish writing/modifying the code.

Thank you.

.. Facundo
------------------------------------------------------------------------

PEP: XXXX
Title: Decimal data type
Version: $Revision: 0.1 $
Last-Modified: $Date: 2003/10/31 15:25:00 $
Author: Facundo Batista <fb******@unifon.com.ar>
Status: Draft
Type: Standards Track
Content-Type: text/x-rst
Created: 17-Oct-2003
Python-Version: 2.3.3
Abstract
========

The idea is to have a Decimal data type, for every use where decimals are
needed but floating point is too inexact.

The Decimal data type should support the Python standard functions and
operations and must comply the decimal arithmetic ANSI standard X3.274-1996.
Rationale
=========

I must separate the requeriments in two sections. The first is to comply
with the ANSI standard. All the needings for this are specified in the
Mike Cowlishaw''s work at http://www2.hursley.ibm.com/decimal/. Cowlishaw''s
also provided a **lot** of test cases. The second section of requeriments
(standard Python functions support, usability, etc) are detailed in the
`Requirements`_ section.

Here I''ll include all the decisions made and why, and all the subjects still
being discussed. The requirements will be numbered, to simplify discussion
on each point.

This work is based on code and test functions written by Eric Price, Aahz
and
Tim Peters. Actually I''ll work on the Decimal.py code in the sandbox (at
python/nondist/sandbox/decimal in SourceForge). Some of the explanations of
this PEP are taken from the Cowlishaw''s work.
Items In Discussion
-------------------

When in a case like ``Decimal op otherType`` (see point 12 in Requirements_
for details), what should happen?

if otherType is an int or long:

a. an exception is raised
b. otherType is converted to Decimal
c. Decimal is converted to int or long (with ``int()`` or
``long()``)

if otherType is a float:

d. an exception is raised
e. otherType is converted to Decimal (rounding? see next item in
discussion)
f. Decimal is converted to float (with ``float()``)

if otherType is a string:

g. an exception is raised
h. otherType is converted to Decimal
i. Decimal is converted to string (bizarre, huh?)
When passing floating point to the constructor, what should happen?

j. ``Decimal(1.1) == Decimal(''1.1'')``
k. ``Decimal(1.1) ==
Decimal(''110000000000000008881784197001252...e-51'')``
Requirements
============

1. The syntax should be ``Decimal(value)``.

2. The value could be of the type:

- another Decimal
- int or long
- float
- string

3. To exist a Context. The context represents the user-selectable
parameters
and rules which govern the results of arithmetic operations. In the
context the user defines:

- what will happen with the exceptional conditions.
- what precision will be used
- what rounding method will be used

4. The Context must be omnipresent, meaning that changes to it affects all
the current and future Decimal instances.

5. The exceptional conditions should be grouped into signals, which could be
controlled individually. The context should contain a flag and a
trap-enabler for each signal. The signals should be: clamped,
division-by-zero, inexact, invalid-operation, overflow, rounded,
subnormal
and underflow.

6. For each of the signals, the corresponding flag should be set to 1 when
the signal occurs. It is only reset to 0 by explicit user action.

7. For each of the signals, the corresponding trap-enabler will indicate
which action is to be taken when the signal occurs. If 0, a defined
result should be supplied, and execution should continue. If 1, the
execution of the operation should end and an exception should be raised.

8. The precision (maximum number of significant digits that can result from
an arithmetic operation) must be positive (greater than 0).

9. To have different kinds of rounding; you can choose the algorithm through
context:

- ``round-down``: (Round toward 0, truncate) The discarded digits are
ignored; the result is unchanged::

1.123 --> 1.12
1.128 --> 1.12
1.125 --> 1.12
1.135 --> 1.13

- ``round-half-up``: If the discarded digits represent greater than
or
equal to half (0.5) then the result should be incremented by 1
(rounded up); otherwise the discarded digits are ignored::

1.123 --> 1.12
1.128 --> 1.13
1.125 --> 1.13
1.135 --> 1.14

- ``round-half-even``: If the discarded digits represent greater than
half (0.5) then the result coefficient should be incremented by 1
(rounded up); if they represent less than half, then the result is
not adjusted (that is, the discarded digits are ignored); otherwise
the result is unaltered if its rightmost digit is even, or
incremented by 1 (rounded up) if its rightmost digit is odd (to
make
an even digit)::

1.123 --> 1.12
1.128 --> 1.13
1.125 --> 1.12
1.135 --> 1.14

- ``round-ceiling``: If all of the discarded digits are zero or if
the
sign is negative the result is unchanged; otherwise, the result
should be incremented by 1 (rounded up)::

1.123 --> 1.13
1.128 --> 1.13
-1.123 --> -1.12
-1.128 --> -1.12

- ``round-floor``: If all of the discarded digits are zero or if the
sign is positive the result is unchanged; otherwise, the absolute
value of the result should be incremented by 1::

1.123 --> 1.12
1.128 --> 1.12
-1.123 --> -1.13
-1.128 --> -1.13

- ``round-half-down``: If the discarded digits represent greater than
half (0.5) then the result should be incremented by 1 (rounded up);
otherwise the discarded digits are ignored::

1.123 --> 1.12
1.128 --> 1.13
1.125 --> 1.12
1.135 --> 1.13

- ``round-up``: (Round away from 0) If all of the discarded digits
are
zero the result is unchanged. Otherwise, the result should be
incremented by 1 (rounded up)::

1.123 --> 1.13
1.128 --> 1.13
1.125 --> 1.13
1.135 --> 1.14

10. Strings with floats in engineering notation will be supported.

11. Calling repr() should do round trip, meaning that::

m = Decimal(...)
m == eval(repr(m))

12. To support the basic aritmetic (``+, -, *, /, //, **, %, divmod``) and
comparison (``==, !=, <, >, <=, >=, cmp``) operators in the following
cases:

- Decimal op Decimal
- Decimal op otherType
- otherType op Decimal
- Decimal op= Decimal
- Decimal op= otherType

Check `Items In Discussion`_ to see what types could OtherType be, and
what happens in each case.

13. To support unary operators (``-, +, abs``).

14. To support the built-in methods:

- min, max
- float, int, long
- str, repr
- hash
- copy, deepcopy
- bool (0 is false, otherwise true)

15. To be immutable.
Reference Implementation
========================

To be included later:

- code
- test code
- documentation
Copyright
=========

This document has been placed in the public domain.

解决方案

Revision: 0.1



Last-Modified:


Date: 2003/10/31 15:25:00


这篇关于prePEP:十进制数据类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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