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

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

问题描述

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

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(含)的每个整数都可以精确表示,从 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 赋值给一个 double 会得到 9007199254740992.0(相同的值)

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

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

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

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

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