如何通过最小的增量来改变双倍 [英] How to alter double by its smallest increment

查看:91
本文介绍了如何通过最小的增量来改变双倍的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有坏事或我不明白发生了什么?

  static String getRealBinary(double val){
long tmp = Double.doubleToLongBits(val);
StringBuilder sb = new StringBuilder();

(long n = 64; - n> 0; tmp>> = 1)
if((tmp& 1)== 0)
sb.insert(0,('0'));
else
sb.insert(0,('1'));

sb.insert(0,'[').insert(2,] [).insert(16,] [).append(']');
return sb.toString();
}

public static void main(String [] argv){
for(int j = 3; --j> = 0;){
double d = j; (int i = 3; - i> = 0;){
d + = Double.MIN_VALUE;

System.out.println(d + getRealBinary(d));
}
}
}

带有输出:

  2.0 [1] [00000000000] [000000000000000000000000000000000000000000000000000] 
2.0 [1] [00000000000] [000000000000000000000000000000000000000000000000000]
2.0 [000000000000000000000000000000000000000000000000000]
1.0 [0] [11111111110] [000000000000000000000000000000000000000000000000000]
1.0 [0] [11111111110] [000000000000000000000000000000000000000000000000000]
1.0 [0] [11111111110] [000000000000000000000000000000000000000000000000000 ]
4.9E-324 [0] [00000000000] [000000000000000000000000000000000000000000000000001]
1.0E-323 [0] [00000000000] [000000000000000000000000000000000000000000000000010]
1.5E-323 [0] [00000000000] 0000000000000000000000000000000000000000000000000000011]


解决方案

它的长表示(使用 doubleToLo ngBits ,如您在 getRealBinary 中所做的那样),将其长度增加1,最后将新的long返回到通过 longBitsToDouble



编辑:Java(从1.5开始)提供 Math.ulp(double) code>,我猜你可以用它来直接计算下一个更高的值: x + Math.ulp(x)


Is something broken or I fail to understand what is happening?

static String getRealBinary(double val) {
    long tmp = Double.doubleToLongBits(val);
    StringBuilder sb = new StringBuilder();

    for (long n = 64; --n > 0; tmp >>= 1)
        if ((tmp & 1) == 0)
            sb.insert(0, ('0'));
        else
            sb.insert(0, ('1'));

    sb.insert(0, '[').insert(2, "] [").insert(16, "] [").append(']');
    return sb.toString();
}

public static void main(String[] argv) {
    for (int j = 3; --j >= 0;) {
        double d = j;
        for (int i = 3; --i >= 0;) {
            d += Double.MIN_VALUE;
            System.out.println(d +getRealBinary(d));
        }
    }
}

With output:

2.0[1] [00000000000] [000000000000000000000000000000000000000000000000000]
2.0[1] [00000000000] [000000000000000000000000000000000000000000000000000]
2.0[1] [00000000000] [000000000000000000000000000000000000000000000000000]
1.0[0] [11111111110] [000000000000000000000000000000000000000000000000000]
1.0[0] [11111111110] [000000000000000000000000000000000000000000000000000]
1.0[0] [11111111110] [000000000000000000000000000000000000000000000000000]
4.9E-324[0] [00000000000] [000000000000000000000000000000000000000000000000001]
1.0E-323[0] [00000000000] [000000000000000000000000000000000000000000000000010]
1.5E-323[0] [00000000000] [000000000000000000000000000000000000000000000000011]

解决方案

The general idea is first convert the double to its long representation (using doubleToLongBits as you have done in getRealBinary), increment that long by 1, and finally convert the new long back to the double it represents via longBitsToDouble.

EDIT: Java (since 1.5) provides Math.ulp(double), which I'm guessing you can use to compute the next higher value directly thus: x + Math.ulp(x).

这篇关于如何通过最小的增量来改变双倍的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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