ULP(最后一个单位)与量子(IEEE 754)之间的差异 [英] Difference between ULP (unit in the last place) and quantum (IEEE 754)

查看:116
本文介绍了ULP(最后一个单位)与量子(IEEE 754)之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

来自 ULP Wikipedia的页面:

约翰·哈里森(John Harrison)提出的另一种定义稍有不同:ULP(x)是两个最接近的跨浮点数a和b(即a≤x≤b且a≠b的浮点数)之间的距离,假设指数范围没有上限.

从IEEE 754 2008开始:

2.1.44量子:有限浮点表示形式的量子是单位在其有效位的最后位置的值.这等于升到指数q的基数,该指数在有效数被视为整数时使用.

问题: ULP (约翰·哈里森的定义)和 quantum (来自IEEE 754)之间有什么区别?

我是否正确理解 double x 的量子可以计算为:

  double ulp(double x){int exp;frexp(x,& exp);返回ldexp(0.5,exp-52);}双量子(双x){int exp;返回ulp(frexp(x,& exp));} 

解决方案

ULP(约翰·哈里森的定义)和量子(来自IEEE 754)之间有什么区别?

OP的 quantum()似乎不正确,对于所有有限的 x 始终返回 1.11022e-16 ,即使 x 是次普通的.

回答的其余部分假定 quantum()更像 quantum_alt(),在下面,每个[power-of-2 ... 2 * power-of-2).请注意 [).

基数的力量

x 是具有强大功能的这些定义仅在基数的有符号幂上有所不同

对于 binary64 ,请考虑何时 x 是2的幂.下一个较大的FP值是 x + u ,下一个较小的值是 x-u/2 .

约翰·哈里森(John Harrison):两个最接近的跨浮点数a和b(即a≤x≤b且a≠b的浮点数)之间的距离"表示 a 是较小的值, x == b ,ULP是 u/2 . 1

Quantum:表示是单位在其有效位的最后位置的值".表示 ULP u .

距离 b-a 是量子"距离的1/2.定义; a 处于小于 x 的指数子范围,并且其最后有效位为 x 的一半.


适用性

定义也不同,两者都适用于浮点值,但不适用于 quantum @Eric Postpischil


在某些情况下,这两种OP功能都是错误的.

如果 x 是2的幂,零或次法线,则John Harrison的

ulp()是不正确的.
备用

  #include< math.h>//使用约定ULP(x)== ULP(-x)//调整是否需要签名结果.double ulp_JH(double x){x =晶圆厂(x);如果(isfinite(x)){双低= nextafter(x,-1.0);//第一个FP编号小于x返回x-较低;}返回x;//NAN,无穷大} 

x 为零或次法线时,

OP的 quantum()不存在.

  doublequant_alt(double x){x =晶圆厂(x);如果(x< DBL_MAX){高一倍= nextafter(x,DBL_MAX);//第一个FP编号大于x返回更高-x;}如果(isfinite(x)){较低的两倍= nextafter(x,0.0);//DBL_MAX的特殊情况返回x-较低;}返回x;//NAN,无穷大} 


1 除外,当 x == DBL_TRUE_MIN 时.在这种情况下. ULP(DBL_TRUE_MIN) DBL_TRUE_MIN .

From ULP Wikipedia's page:

Another definition, suggested by John Harrison, is slightly different: ULP(x) is the distance between the two closest straddling floating-point numbers a and b (i.e., those with a ≤ x ≤ b and a ≠ b), assuming that the exponent range is not upper-bounded.

From IEEE 754 2008:

2.1.44 quantum: The quantum of a finite floating-point representation is the value of a unit in the last position of its significand. This is equal to the radix raised to the exponent q, which is used when the significand is regarded as an integer.

Question: What is the difference between ULP (John Harrison's definition) and quantum (from IEEE 754)?

Do I understand right that quantum of double x can be computed as:

double ulp(double x)
{
        int exp;
        frexp( x, &exp );
        return ldexp( 0.5, exp-52 );
}
double quantum(double x)
{
        int exp;
        return ulp(frexp( x, &exp ));
}

解决方案

What is the difference between ULP (John Harrison's definition) and quantum (from IEEE 754)?

[edit]

OP's quantum() appears incorrect, consistently returning 1.11022e-16 for all finite x, even when x is a sub-normal.

Rest of answer assumes quantum() is more like quantum_alt() below which has the same result for each [power-of-2 ... 2*power-of-2). Note the [).

Power of the radix

When x is a power-of-the-base These definitions differ only at signed powers of the radix

For binary64, consider when x is a power-of-2. The next larger FP value is x + u and the next smaller value is x - u/2.

John Harrison: "distance between the two closest straddling floating-point numbers a and b (i.e., those with a ≤ x ≤ b and a ≠ b)" implies a is the smaller value and x == b and ULP is u/2.1

Quantum: "representation is the value of a unit in the last position of its significand" implies the ULP is u.

The distance b-a is 1/2 the "quantum" definition; a being in the smaller exponent subrange than x and whose last position significant is half of x.


Applicability

Definitions also differ in that both apply to floating point values, but not quantum with real values like 1/7, √2, π. @Eric Postpischil


Both OP functions are wrong in select cases.

ulp() per John Harrison, is amiss when x is a power-of-2, zero or a sub-normal.
Alternate

#include <math.h>

// Using the convention ULP(x) == ULP(-x)
// Adjust if you want a signed result.
double ulp_JH(double x) {
  x = fabs(x);
  if (isfinite(x)) {
    double lower = nextafter(x, -1.0); // 1st FP number smaller than x   
    return x - lower;
  }
  return x; // NAN, infinity
}

OP's quantum() is amiss when x is a zero or a sub-normal.

double quantum_alt(double x) {
  x = fabs(x);
  if (x < DBL_MAX) {
    double higher = nextafter(x, DBL_MAX); // 1st FP number larger than x   
    return higher - x;
  }
  if (isfinite(x)) {
    double lower = nextafter(x, 0.0); // Special case for DBL_MAX
    return x - lower;
  }
  return x; // NAN, infinity
}


1 Except when x == DBL_TRUE_MIN. In that case. ULP(DBL_TRUE_MIN) is DBL_TRUE_MIN.

这篇关于ULP(最后一个单位)与量子(IEEE 754)之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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