![](/img/trans.png)
[英]How to compute total floating-point rounding error of a set of arithmetic computations in java using Math.ulp(double)?
[英]ULP calculation floating-point
我不确定Java是否可以使用别名来获取浮点数的位模式(有足够接近的替代方法,如答案的第二部分所示),但是如果这样做,则e是位23到30(符号为31)减去某个常数(如该格式的Wikipedia描述,除非数字为次正规,否则为127),而n是固定的(在这种情况下为23位,如果包含隐式1,则为24)。
建议您使用一个可以为您正确完成此工作的库。
我在(而不是间接地)的注释中得到通知的另一种选择是将浮点数转换为int。 我将直接编写一段代码。 我不精通Java(由于缺少包/类说明符,代码可能无法立即工作(floatToIntBits和intToFloatBits是类java.lang.Float的静态方法)。这与上面的建议不同,因为它是有点非常规,但是它比您在问题本身中建议的代码具有更好的性能。
float ulp(float x) {
int repr;
float next;
if (Float.isNaN(x)) return Float.NaN; //special handling, to be safe
repr = Float.floatToIntBits(x);
x++; //will work correctly independently of sign
next = Float.intBitsToFloat(repr)
return next-x;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.