在Java中处理四倍精度浮点数(128位) [英] Handling a quadruple precision floating point (128-bit) number in java
问题描述
我需要利用来自另一个系统的数字,这些数字是Java中的128位(四精度)浮点数.
I need to make use of numbers coming from another system that are 128-bit (quadruple-precision) floating point numbers in java.
考虑到Java中没有等效类型,我想使用Java代码降低数字的精度,以便将它们存储在Java double中.可以在c或使用汇编中相当容易地完成此操作,但我只想在java中完成.
Considering that there is no equivalent type in java, I would like to reduce the precision of the numbers using java code so they can be stored in a java double. This can be done fairly easily in c or using assembly but I would like to do it purely in java.
公平地假设四精度数存储在Java中的128位字节数组中.
It is fair to assume that the quadruple-precision number is stored in a 128-bit byte array in java.
仅使用java是否有一个好的解决方案?谢谢.
Is there a good solution, using only java? Thanks.
推荐答案
这个问题让我很感兴趣,以至于我不得不写一个库来处理IEEE-754浮点数.使用该库,您可以使用以下内容:
I was so intrigued by this question that I was compelled to write a library to handle IEEE-754 floating point numbers. With the library, you can use the following:
byte[] quadBytes; // your quad-floating point number in 16 bytes
IEEE754 quad = IEEE754.decode(IEEE754Format.QUADRUPLE,
BitUtils.wrapSource(quadBytes));
// IEEE754 holds the number in a 'lossless' format
从那里,您可以:
ByteBuffer doubleBuffer = ByteBuffer.allocateDirect(8);
quad.toBits(IEEE754Format.DOUBLE, BitUtils.wrapSink(doubleBuffer));
doubleBuffer.rewind();
double converted = doubleBuffer.asDoubleBuffer().get();
但是上面的代码片段只是为了说明一般用法...为double提供了简写:
But the above snippet is just to illustrate general usage... a shorthand is provided for double:
double converted = quad.doubleValue();
该代码可从 kerbaya.com/ieee754lib 获得.
这篇关于在Java中处理四倍精度浮点数(128位)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!