java 浮点数转字节,java-IBM-IEEE双精度浮点字节转换

博客内容讲述了如何在Java中进行IBM双精度浮点数到IEEE 754格式的转换。作者已经成功实现了单精度转换,但寻求双精度的解决方案。文中提供了一个算法,涉及将字节数组转换为64位长整型,然后进行浮点格式的转换,并返回字节数组。转换涉及符号位、指数和尾数的处理,以及精度可能的损失。
摘要由CSDN通过智能技术生成

I need to do IBM-IEEE floating point conversions of byte arrays in Java. I was able to successfully do conversions of single-precision float bytes using http://www.thecodingforums.com/threads/c-code-for-converting-ibm-370-floating-point-to-ieee-754.438469.

But I also need to convert bytes of double-precision doubles. Everywhere I look seems to only show the single-precision conversion. The closest I've gotten is the r8ibmieee function in http://spdf.sci.gsfc.nasa.gov/pub/documents/old/miscellaeous_documents_from_nssdc/b46645.txt, but it uses C unions/bitfields and only converts IBM to IEEE (and not the reverse). I was hoping for a solution similar to the first link that takes a byte array or hex string and outputs a byte array or hex string. Does anyone have an algorithm for converting double-precision bytes from IBM to IEEE and vice versa that will work in Java?

解决方案

I recommend that you work with long (64-bit integer) values; it will be much easier. You'll convert a byte array to a long, work with that to convert the floating-point format, and then convert the resulting long back to a byte array. I won't tell you how to convert between long and byte array values, since I don't know whether your byte arrays are little-endian or big-endian.

An IBM 64-bit float has the format

1 sign bit

7 exponent bits (power of 16), bias = 64

56 mantissa bits

If the exponent is X and the mantissa bits are bbbbb...bbbbb, then the value of the float (if the sign bit is 0) is 16^(X-64) * 0.bbbbb...bbbbb.

An IEEE 754 64-bit float has the format

1 sign bit

11 exponent bits (power of 2), bias = 1023

52 mantissa bits, with an implied a bit

If the exponent is X and the mantissa bits are bbbbb...bbbbb, the value of the float (if the sign bit is 0) is 2^(X-1023) * 1.bbbbb...bbbbb.

So now you need to figure out how to express the same value in the IEEE format (as close as possible; it's possible you might lose a few bits of precision, since the IBM mantissa is longer).

The sign bit will be the same in both formats.

To compute an initial value for the IEEE exponent: If the IBM exponent is X1, and the IEEE exponent is X2, we need to find X2 such that 16^(X1-64) = 2^(X2-1023). The left side will be equal to 2^(4*X1-256), so since the powers of 2 must be the same, this gives us X2 = 4*X1-256+1023 = 4*X1+767.

At this point, if the mantissa of the IBM float is bbbbb...bbbbb, the floating-point value is sign * 2^(X2-1023) * 0.bbbbb...bbbbb. However, for an IEEE float, we have to arrange the mantissa bits so that we're multiplying by 1.bbbbb...bbbbb. This means that we need to shift the mantissa bits of the IBM mantissa until we shift a 1 into the bit place to the left of the binary point. Every time we shift the mantissa, we double it, which means that we have to compensate by subtracting 1 from X2. Thus, suppose that after step 2, we have X2=1031 and mantissa = 0011 0111 1100 ... The number we're representing is 28 * 0.001101111100..... We will need to shift the mantissa left 3 places to get the 1 into the bit left of the binary point; the number is thus equal to 25 * 1.101111100.... Thus, X2 after this step will be 1031 - 3 = 1028; that's the value that will go into the exponent field of the IEEE float.

The 1 to the left of the binary point does not go into the IEEE float. It's an "implied 1". The mantissa bits of the IEEE float will be 101111100..... Note that the IBM mantissa has 56 bits; the value you're left with, not counting the implied 1, will still have 56 bits. You will need to chop off the lower 4 bits to get it down to 52 for the IEEE float, and you may want to round.

After all this, you now have the sign bit, the exponent field (X2), and the mantissa bits you need to construct the 64-bit IEEE float.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值