I need to make use of numbers coming from another system that are 128-bit (quadruple-precision) floating point numbers in 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.
It is fair to assume that the quadruple-precision number is stored in a 128-bit byte array in java.
Is there a good solution, using only java? Thanks.
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
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 converted = quad.doubleValue();
The code is available at ieee754lib.kerbaya.org.