容易产生无限和NaN [英] Portably generating infinity and NaN
问题描述
什么是可移植地生成二进制浮点无限的最佳方式
和NaNs?我只知道两个解决方案:
1.使用IEEE 754中提出的fpconst模块,我相信它会转移
位。
2.使用扩展模块(例如,numarray.ieeespecial将执行此操作)。
我想使用float(十进制(nan)) ,但显然是
十进制.__ float __(self)是float(str(self)),这是不可移植的。
-
Michael Hoffman
What''s the best way to portably generate binary floating point infinity
and NaNs? I only know two solutions:
1. Using the fpconst module proposed in IEEE 754, which I believe shifts
bits around.
2. Using an extension module (for example, numarray.ieeespecial will do it).
I thought of using float(Decimal("nan")), but apparently
Decimal.__float__(self) is float(str(self)), which isn''t portable.
--
Michael Hoffman
推荐答案
Michael Hoffman写道:
Michael Hoffman wrote:
2.使用扩展模块(例如,numarray.ieeespecial会做
)。
2. Using an extension module (for example, numarray.ieeespecial will do
it).
我想我也可以一直使用ctypes,并说,从-b $ b获取-inf libc.log(ctypes.c_double(0.0) ))。虽然我们冒险远离
便携式领域,但由于指定如何加载libc将在不同平台上有所不同。
- -
Michael Hoffman
I guess I could always use ctypes as well, and say, get -inf from
libc.log(ctypes.c_double(0.0)). Although we''re venturing away from
portable territory then, since specifying how to load libc will be
different on different platforms.
--
Michael Hoffman
Michael Hoffman写道:
Michael Hoffman wrote:
什么是可移植地生成二进制浮点无穷大的最佳方法
和NaNs?我只知道两个解决方案:
1.使用IEEE 754中提出的fpconst模块,我相信它会转移
位。
2.使用扩展模块(例如,numarray.ieeespecial将执行此操作)。
我想使用float(十进制(nan)) ,但显然是
十进制.__ float __(self)是float(str(self)),这是不可移植的。
What''s the best way to portably generate binary floating point infinity
and NaNs? I only know two solutions:
1. Using the fpconst module proposed in IEEE 754, which I believe shifts
bits around.
2. Using an extension module (for example, numarray.ieeespecial will do it).
I thought of using float(Decimal("nan")), but apparently
Decimal.__float__(self) is float(str(self)), which isn''t portable.
这就是numpy所做的(从C翻译):
mul = 1e10
inf = mul
tmp = 0.0
而True:
inf * = mul
if inf == tmp:
休息
tmp = inf
nan = inf / inf
-
Robert Kern
我开始相信整个世界都是一个谜,一个无害的谜团
由于我们疯狂地试图解释它而使它变得可怕,好像它有一个潜在的真相。
- Umberto Eco
>
Michael Hoffman schrieb:
Michael Hoffman schrieb:
什么是可移植地生成二进制浮点无穷大的最佳方法
和NaNs?我只知道两个解决方案:
1.使用IEEE 754中提出的fpconst模块,我相信它会转移
位。
2.使用扩展模块(例如,numarray.ieeespecial将执行
)。
What''s the best way to portably generate binary floating point infinity
and NaNs? I only know two solutions:
1. Using the fpconst module proposed in IEEE 754, which I believe shifts
bits around.
2. Using an extension module (for example, numarray.ieeespecial will do
it).
使用struct模块打包/解包double值。在IEEE 754
系统上,这应该可行 - 在非IEEE-754系统上,不清楚
+/- inf和NaN实际存在。
问候,
马丁
Use the struct module to pack/unpack a double value. On an IEEE 754
system, this should work - on a non-IEEE-754 system, it''s not clear
that +/-inf and NaN actually exist.
Regards,
Martin
这篇关于容易产生无限和NaN的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!