Java中的x86 80位浮点类型 [英] x86 80-bit floating point type in Java

查看:188
本文介绍了Java中的x86 80位浮点类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我可以尝试使用BigDecimal来实现它,但覆盖所有关于NaNs,无限和演员的特殊情况都可能是一个单调乏味的任务。我知道一些库提供了更高的精度比其他浮动类型,但我希望有相同的精度,x86的80位浮点数。



是有一个Java库提供这样一个浮点类型?如果不是的话,你能提供一些其他的提示,可以用比定制的BigDecimal解决方案更少的努力来实现这样的数据类型吗? 解决方案

一个80位的值最好保持为 long (尾数)和 int 指数和符号。对于许多操作来说,将长整数的上下半部分放在单独的long值中可能是最实际的,因此添加两个具有匹配符号和指数的数字的代码可能类似于:

  long resultLo =(num1.mant& 0xFFFFFFFFL)+(num2.mant& 0xFFFFFFFFL); 
long resultHi =(num1.mant>>> 32)+(num2.mant>>> 32)+(resultLo>>> 32);
result.exp = num1.exp; //应该匹配num2.exp
if(resultHi> 0xFFFFFFFFL){
exponent ++;
resultHi =(resultHi +((resultHi& 2)>>> 1))>>> 1; //将结果舍入
}
rest.mant =(resultHi <+32)+ resultLo;

有点麻烦,但不完全不可行。关键是
把数字分成小块,所以你可以把所有的数学做成
类型的long。

顺便说一下如果其中一个数字本来不具有相同的指数
,那么当
向左或向右移动以匹配指数的时候,有必要跟踪是否有任何比特掉下来第一个数字,以便
能够正确地将结果放在后面。


I want to emulate the x86 extended precision type and perform arithmetic operations and casts to other types in Java.

I could try to implement it using BigDecimal, but covering all the special cases around NaNs, infinity, and casts would probably a tedious task. I am aware of some libraries that provide other floating types with a higher precision than double, but I want to have the same precision as the x86 80-bit float.

Is there a Java library that provides such a floating point type? If not, can you provide other hints that would allow to implement such a data type with less effort than coming up with a custom BigDecimal solution?

解决方案

An 80-bit value should be best held as combination of a long (for the mantissa) and an int for the exponent and sign. For many operations, it will probably be most practical to place the upper and lower halves of the long into separate "long" values, so the code for addition of two numbers with matching signs and exponents would probably be something like:

long resultLo = (num1.mant & 0xFFFFFFFFL)+(num2.mant & 0xFFFFFFFFL);
long resultHi = (num1.mant >>> 32)+(num2.mant >>> 32)+(resultLo >>> 32);
result.exp = num1.exp; // Should match num2.exp
if (resultHi > 0xFFFFFFFFL) {
  exponent++;
  resultHi = (resultHi + ((resultHi & 2)>>>1)) >>> 1; // Round the result
}
rest.mant = (resultHi << 32) + resultLo;

A bit of a nuisance all around, but not completely unworkable. The key is to break numbers into pieces small enough that you can do all your math as type "long".

BTW, note that if one of the numbers did not originally have the same exponent, it will be necessary to keep track of whether any bits "fell off the end" when shifting it left or right to match the exponent of the first number, so as to be able to properly round the result afterward.

这篇关于Java中的x86 80位浮点类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆