标签:c c-3 floating-point ieee-754
正在为我们正在使用的旧系统寻找IEEE浮点数到IBM浮点格式的方法.
我们可以在C#中使用通用公式吗?
最佳答案:
// http://en.wikipedia.org/wiki/IBM_Floating_Point_Architecture
// float2ibm(-118.625F) == 0xC276A000
// 1 100 0010 0111 0110 1010 0000 0000 0000
// IBM/370 single precision, 4 bytes
// xxxx.xxxx xxxx.xxxx xxxx.xxxx xxxx.xxxx
// s|-exp--| |--------fraction-----------|
// (7) (24)
// value = (-1)**s * 16**(e - 64) * .f range = 5E-79 ... 7E+75
static int float2ibm(float from)
{
byte[] bytes = BitConverter.GetBytes(from);
int fconv = (bytes[3] << 24) | (bytes[2] << 16) | (bytes[1] << 8)| bytes[0];
if (fconv == 0) return 0;
int fmant = (0x007fffff & fconv) | 0x00800000;
int t = (int)((0x7f800000 & fconv) >> 23) - 126;
while (0 != (t & 0x3)) { ++t; fmant >>= 1; }
fconv = (int)(0x80000000 & fconv) | (((t >> 2) + 64) << 24) | fmant;
return fconv; // big endian order
}
我更改了一段名为static void float_to_ibm的代码(int从[],int到[],int n,int endian)
上面的代码可以在PC上正确运行
来自小端的浮点数.
返回值是big endian ibm float number但存储在int类型中.
标签:c,c-3,floating-point,ieee-754 来源: https://codeday.me/bug/20190515/1109971.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。