简体   繁体   English

在 java 中处理四倍精度浮点(128 位)数

[英]Handling a quadruple precision floating point (128-bit) number in java

I need to make use of numbers coming from another system that are 128-bit (quadruple-precision) floating point numbers in java.我需要使用来自另一个系统的数字,这些数字是 java 中的 128 位(四倍精度)浮点数。

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.考虑到java中没有等效类型,我想使用java代码降低数字的精度,以便它们可以存储在java double中。 This can be done fairly easily in c or using assembly but I would like to do it purely in java.这可以用 c 或使用汇编很容易地完成,但我想纯粹用 java 来完成。

It is fair to assume that the quadruple-precision number is stored in a 128-bit byte array in java.可以公平地假设四倍精度数存储在 java 中的 128 位字节数组中。

Is there a good solution, using only java?有没有好的解决方案,只使用java? Thanks.谢谢。

I was so intrigued by this question that I was compelled to write a library to handle IEEE-754 floating point numbers.我对这个问题很感兴趣,以至于我不得不编写一个库来处理 IEEE-754 浮点数。 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

From there, you can:从那里,您可以:

ByteBuffer doubleBuffer = ByteBuffer.allocateDirect(8);
quad.toBits(IEEE754Format.DOUBLE, BitUtils.wrapSink(doubleBuffer));
doubleBuffer.rewind();
double converted = doubleBuffer.asDoubleBuffer().get();

But the above snippet is just to illustrate general usage... a shorthand is provided for double:但上面的代码片段只是为了说明一般用法……为 double 提供了一个速记:

double converted = quad.doubleValue();

The code is available at kerbaya.com/ieee754lib .该代码可在kerbaya.com/ieee754lib 上获得

Depending on the size of the data set BigDecimal instantiated from an imported String representation might be an easy and accurate option.根据从导入的String表示实例化的数据集的大小, BigDecimal可能是一个简单而准确的选项。 I assume one can export string representations of those numbers from any programming language.我假设可以从任何编程语言导出这些数字的字符串表示。

Although the question was asked rather long ago, perhaps it may still be of interest for someone.虽然这个问题是很久以前提出的,但也许有人仍然感兴趣。 There is a Java class for 128-bit floating point arithmetic, that has methods for converting 128-bit IEEE-754 floating-point values into its own internal representation without any loss of precision.有一个用于 128 位浮点运算的Java 类该类具有将 128 位 IEEE-754 浮点值转换为它自己的内部表示而不损失任何精度的方法。 It can perform arithmetic operations on such values, and convert them back to IEEE-754 binary128 , as well as to other common numeric types like BidDecimal , double and long .它可以对这些值执行算术运算,并将它们转换回IEEE-754 binary128以及其他常见的数字类型,如BidDecimaldoublelong It can also parse strings containing decimal representations of such values and convert them back to strings.它还可以解析包含此类值的十进制表示的字符串并将它们转换回字符串。 Internally, it stores 128 bits of the mantissa, so that the relative error of the calculations does not exceed 1.47e-39.在内部,它存储了 128 位的尾数,因此计算的相对误差不超过 1.47e-39。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM