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

后端 未结 2 1534
醉梦人生
醉梦人生 2021-01-04 19:24

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 equivale

相关标签:
2条回答
  • 2021-01-04 19:59

    Depending on the size of the data set BigDecimal instantiated from an imported String representation might be an easy and accurate option. I assume one can export string representations of those numbers from any programming language.

    0 讨论(0)
  • 2021-01-04 20:17

    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 kerbaya.com/ieee754lib.

    0 讨论(0)
提交回复
热议问题