无法映射到double的64位无符号整数 [英] 64-bit unsigned integers which cannot map onto a double

查看:268
本文介绍了无法映射到double的64位无符号整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否存在不能用双精度浮点类型表示的64位无符号整数值? (因为double也是64位宽,所以必须有一些。)如果是这样,我如何计算它们的 all ? (也许以一种不蛮力的方式?)

Are there any 64-bit unsigned integer values which cannot be represented with a double-precision floating-point type? (As a double is also 64-bit wide there must be some.) If so, how can I calculate all of them? (In a not brute force way, maybe?)

推荐答案

从0到2 ^ 52(包括0和2)的每个整数都可以精确表示,从仅每个偶数2的整数从2 ^ 52到2 ^ 53(最低有效位0),然后是每四个整数,直到2 ^ 64-2 ^ 12。

Every integer from 0 to 2^52 inclusive is representable exactly, from 2^52 to 2^53 only every even integer (lowest significant bit of 0), then every fourth integer, up to 2^64-2^12.

我们可以用一些代码来概括,

We could generalise with a bit of code,

采用m = 52:

    for (i=0; i<(64-m+1); i++) {
            start = i ? 1ULL << (i+m) : 0;
            end = ((1ULL << m+1)-1) << i;
            step = 1ULL << i;
    }

产生:

0000000000000000 to 001fffffffffffff step 1
0020000000000000 to 003ffffffffffffe step 2
0040000000000000 to 007ffffffffffffc step 4
0080000000000000 to 00fffffffffffff8 step 8
0100000000000000 to 01fffffffffffff0 step 16
0200000000000000 to 03ffffffffffffe0 step 32
0400000000000000 to 07ffffffffffffc0 step 64
0800000000000000 to 0fffffffffffff80 step 128
1000000000000000 to 1fffffffffffff00 step 256
2000000000000000 to 3ffffffffffffe00 step 512
4000000000000000 to 7ffffffffffffc00 step 1024
8000000000000000 to fffffffffffff800 step 2048

示例:

将0x0020000000000000分配给一个双将9007199254740992.0(在IEEE754中为0x0x4340000000000000)

Assigning 0x0020000000000000 to a double gives 9007199254740992.0 (0x0x4340000000000000 in IEEE754)

将0x0020000000000001分配给一个双将9007199254740992.0(相同的值)

Assigning 0x0020000000000001 to a double gives 9007199254740992.0 (same value)

将0x0020000000000002分配给双精度g ives 9007199254740994.0(0x0x4340000000000001,这是下一个可表示的值)

Assigning 0x0020000000000002 to a double gives 9007199254740994.0 (0x0x4340000000000001 , which is the next representable value)

这篇关于无法映射到double的64位无符号整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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