简体   繁体   English

为什么numpy.nextafter(0。,1。)!= numpy.finfo(float).tiny?

[英]Why is numpy.nextafter(0., 1.) != numpy.finfo(float).tiny?

Inspired by this answer , I wonder why numpy.nextafter gives different results for the smallest positive float number from numpy.finfo(float).tiny and sys.float_info.min : 这个答案的启发,我想知道为什么numpy.nextafternumpy.finfo(float).tinysys.float_info.min最小的正浮点数给出不同的结果:

import numpy, sys

nextafter = numpy.nextafter(0., 1.) # 5e-324
tiny = numpy.finfo(float).tiny # 2.2250738585072014e-308
info = sys.float_info.min # 2.2250738585072014e-308

According to the documentations: 根据文件:

numpy.nextafter numpy.nextafter

Return the next floating-point value after x1 towards x2, element-wise. 将x1之后的下一个浮点值返回x2(元素方向)。

finfo(float).tiny FINFO(浮点)。微

The smallest positive usable number. 最小的正可用数字。 Type of tiny is an appropriate floating point type. tiny类型是适当的浮点类型。

sys.float_info sys.float_info

A structseq holding information about the float type. structseq保存有关float类型的信息。 It contains low level information about the precision and internal representation. 它包含有关精度和内部表示的低级信息。 Please study your system's :file: float.h for more information. 请研究您的系统:file: float.h以获取更多信息。

Does someone have an explanation for this? 有人对此有解释吗?

The documentation 's wording on this is bad; 文件对此的措辞不好; “usable” is colloquial and not defined. “可用”是口语,没有定义。 Apparently tiny is meant to be the smallest positive normal number. 显然tiny意味着最小的正常数。

nextafter is returning the actual next representable value after zero, which is subnormal . nextafter返回零后的实际下一个可表示值,这是次正规的

Python does not rigidly specify its floating-point properties. Python没有严格指定其浮点属性。 Python implementations commonly inherit them from underlying hardware or software, and use of IEEE-754 formats (but not full conformance to IEEE-754 semantics) is common. Python实现通常从底层硬件或软件继承它们,并且使用IEEE-754格式(但不完全符合IEEE-754语义)是常见的。 In IEEE-754, numbers are represented with an implicit leading one bit in the significand 1 until the exponent reaches its minimum value for the format, after which the implicit bit is zero instead of one and smaller values are representable only by reducing the significand instead of reducing the exponent. 在IEEE-754,号码与在有效数1的隐式龙头一个比特表示,直到指数达到的格式其最小值,在此之后,隐式比特是零,而不是一个和更小的值可表示仅通过减小有效数,而不是减少指数。 These numbers with the implicit leading zero are the subnormal numbers. 具有隐式前导零的这些数字是次正规数。 They serve to preserve some useful arithmetic properties, such as xy == 0 if and only if x == y . 它们用于保留一些有用的算术属性,例如xy == 0当且仅当x == y (Without subnormal numbers, two very small numbers might be different, but their even smaller difference might not be representable because it was below the exponent limit, so computing xy would round to zero, resulting in code like if (x != y) quotient = t / (xy) getting a divide-by-zero error.) (如果没有次正规数,两个非常小的数字可能会有所不同,但它们的差异甚至可能无法表示,因为它低于指数限制,因此计算xy将舍入为零,导致代码如if (x != y) quotient = t / (xy)得到除零误差。)

Note 注意

1 “Significand” is the term preferred by experts for the fraction portion of a floating-point representation. 1 “重要”是专家对浮点表示的小数部分所优选的术语。 “Mantissa” is an old term for the fraction portion of a logarithm. “尾数”是对数的小数部分的旧术语。 Mantissas are logarithmic, while significands are linear. Mantissas是对数的,而有效数是线性的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM