我正在维护一个程序,该程序从 PDP-11(模拟!)程序中获取数据并将其放入基于 Windows 的现代系统中。我们遇到了一些数据值被报告为“1.#QNAN”和“1.#QNB”的问题。客户最近透露,PDP-11 程序中的“坏”值由 2 个 16 位字表示,除第一个外,所有位均设置。我认为正是当我们尝试将这些转换为 IEEE 浮点数时,我们才会遇到错误。
我发现下面的代码用于将 PDP-11 值转换为 IEEE。我不太了解浮点表示的复杂性,但这对我来说似乎有点简单!这真的能可靠地将 PDP-11 浮点数转换为 IEEE 吗?
// ---------------------------------------------------------------- cnvPDPfloat
// CNVPDPFLOAT
// ----------------------------------------------------------------------------
//
// Converts PDP11 float (two 16-bit words) into IEEE float
//
// PDP11 and IEEE floats have same layout so can be mapped onto eachother.
// But PDP11 exponent must have 2 subtracted for IEEE. Or just divide by 4.
//
float cnvPDPfloat( PDP11Float input )
{
union
{
unsigned long pdp11;
float ieee;
} uFloat;
uFloat.pdp11 = (input.word[0] << 16) + input.word[1];
return (uFloat.ieee / (float) 4.0);
}
--- 阿利斯泰尔。
最佳答案
该代码不检查未定义的值、干净零和脏零,但除以 4(在其他答案中讨论)是好的。 OP 可能知道这一点,因为他们会发现结果是否总是错误的。
指数偏差今天也让我感到困惑,所以我将引用我刚刚在这份精美文档中读到的内容:Binary floats with hidden bit :
At first the hidden bit is given another position. IEEE assumes this bit before the fractional period and Digital assumes it immediately after that period. According to IEEE the visible part of the mantissa ('visman') starts immediately after the period, whilst according to Digital it starts behind the hidden bit. Thus the value range of the total mantissa is:
IEEE: 1.0 =< (1.visman) < 2.0 Digital: 0.5 =< (0.1 visman) < 1.0
At second the excess-biases in the notation of the exponent differ. [by 1 ...]
Both effects together make that the bit pattern in an IEEE- float represents a number four times in size of the value the same bit pattern in a Digital-float stands for.
这也解释了为什么一些引用文献指出 IEEE 偏差是 126。
关于floating-point - 这个 C++ 会将 PDP-11 float 转换为 IEEE 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2250635/