打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
PEP 754 -- IEEE 754 Floating Point Special Values
PEP: 754
Title: IEEE 754 Floating Point Special Values
Version: 928634d80fc6
Last-Modified: 2008-01-18 08:36:51 +0000 (Fri, 18 Jan 2008)
Author: Gregory R. Warnes <gregory_r_warnes at groton.pfizer.com> (Pfizer, Inc.)
Status: Rejected
Type: Standards Track
Content-Type: text/x-rst
Created: 28-Mar-2003
Python-Version: 2.3
Post-History:

Rejection Notice

This PEP has been rejected. After sitting open for four years, it hasfailed to generate sufficient community interest.

Several ideas of this PEP were implemented for Python 2.6. float('inf')and repr(float('inf')) are now guaranteed to work on every supportedplatform with IEEE 754 semantics. However the eval(repr(float('inf')))roundtrip is still not supported unless you define inf and nan yourself:

>>> inf = float('inf')>>> inf, 1E400(inf, inf)>>> neginf = float('-inf')>>> neginf, -1E400(-inf, -inf)>>> nan = float('nan')>>> nan, inf * 0.(nan, nan)

The math and the sys module also have gained additional features,sys.float_info, math.isinf, math.isnan, math.copysign.

Abstract

This PEP proposes an API and a provides a reference module thatgenerates and tests for IEEE 754 double-precision special values:positive infinity, negative infinity, and not-a-number (NaN).

Rationale

The IEEE 754 standard defines a set of binary representations andalgorithmic rules for floating point arithmetic. Included in thestandard is a set of constants for representing special values,including positive infinity, negative infinity, and indeterminate ornon-numeric results (NaN). Most modern CPUs implement theIEEE 754 standard, including the (Ultra)SPARC, PowerPC, and x86processor series.

Currently, the handling of IEEE 754 special values in Python dependson the underlying C library. Unfortunately, there is littleconsistency between C libraries in how or whether these values arehandled. For instance, on some systems "float('Inf')" will properlyreturn the IEEE 754 constant for positive infinity. On many systems,however, this expression will instead generate an error message.

The output string representation for an IEEE 754 special value alsovaries by platform. For example, the expression "float(1e3000)",which is large enough to generate an overflow, should return a stringrepresentation corresponding to IEEE 754 positive infinity. Python2.1.3 on x86 Debian Linux returns "inf". On Sparc Solaris 8 withPython 2.2.1, this same expression returns "Infinity", and onMS-Windows 2000 with Active Python 2.2.1, it returns "1.#INF".

Adding to the confusion, some platforms generate one string onconversion from floating point and accept a different string forconversion to floating point. On these systems

float(str(x))

will generate an error when "x" is an IEEE special value.

In the past, some have recommended that programmers use expressionslike:

PosInf = 1e300**2NaN = PosInf/PosInf

to obtain positive infinity and not-a-number constants. However, thefirst expression generates an error on current Python interpreters. Apossible alternative is to use:

PosInf = 1e300000NaN = PosInf/PosInf

While this does not generate an error with current Pythoninterpreters, it is still an ugly and potentially non-portable hack.In addition, defining NaN in this way does solve the problem ofdetecting such values. First, the IEEE 754 standard provides for anentire set of constant values for Not-a-Number. Second, the standardrequires that

NaN != X

for all possible values of X, including NaN. As a consequence

NaN == NaN

should always evaluate to false. However, this behavior also is notconsistently implemented. [e.g. Cygwin Python 2.2.2]

Due to the many platform and library inconsistencies in handling IEEEspecial values, it is impossible to consistently set or detect IEEE754 floating point values in normal Python code without resorting todirectly manipulating bit-patterns.

This PEP proposes a standard Python API and provides a referencemodule implementation which allows for consistent handling of IEEE 754special values on all supported platforms.

API Definition

Constants

NaN
Non-signalling IEEE 754 "Not a Number" value
PosInf
IEEE 754 Positive Infinity value
NegInf
IEEE 754 Negative Infinity value

Functions

isNaN(value)
Determine if the argument is a IEEE 754 NaN (Not a Number) value.
isPosInf(value)
Determine if the argument is a IEEE 754 positive infinity value.
isNegInf(value)
Determine if the argument is a IEEE 754 negative infinity value.
isFinite(value)
Determine if the argument is an finite IEEE 754 value (i.e., isnot NaN, positive, or negative infinity).
isInf(value)
Determine if the argument is an infinite IEEE 754 value (positiveor negative infinity)

Example

(Run under Python 2.2.1 on Solaris 8.)

>>> import fpconst>>> val = 1e30000 # should be cause overflow and result in "Inf">>> valInfinity>>> fpconst.isInf(val)1>>> fpconst.PosInfInfinity>>> nval = val/val # should result in NaN>>> nvalNaN>>> fpconst.isNaN(nval)1>>> fpconst.isNaN(val)0

Implementation

The reference implementation is provided in the module "fpconst" [1],which is written in pure Python by taking advantage of the "struct"standard module to directly set or test for the bit patterns thatdefine IEEE 754 special values. Care has been taken to generateproper results on both big-endian and little-endian machines. Thecurrent implementation is pure Python, but some efficiency could begained by translating the core routines into C.

Patch 1151323 "New fpconst module" [2] on SourceForge adds thefpconst module to the Python standard library.

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
IEEE Standard 754 Floating Point Numbers
js中typeof的用法
成功解决ValueError: Input contains NaN, infinity or a value too large for dtype('float64').
Java中float/double取值范围与精度
C++ limits头文件的用法(numeric_limits)
可能你还不懂浮点数
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服