C++ 类型的十进制表示长度的近似值

C++ 类型的十进制表示长度的近似值,c++,c,git,algorithm,C++,C,Git,Algorithm,当我阅读git的源代码时,我被这个宏弄糊涂了 /* Approximation of the length of the decimal representation of this type. */ #define decimal_length(x) ((int)(sizeof(x) * 2.56 + 0.5) + 1) 但当我试图计算一个类型的十进制表示长度的近似值时,我认为答案是 ((int)(sizeof(x) * 8 * ln2 ) + 1) 可以写成

当我阅读git的源代码时,我被这个宏弄糊涂了

    /* Approximation of the length of the decimal representation of this type. */
    #define decimal_length(x)   ((int)(sizeof(x) * 2.56 + 0.5) + 1)

但当我试图计算一个类型的十进制表示长度的近似值时,我认为答案是

    ((int)(sizeof(x) * 8 * ln2 ) + 1)
可以写成

    ((int)(sizeof(x) * 2.41 ) + 1)
你能告诉我为什么git计算长度时使用“(sizeof(x)*2.56+0.5)”而不是“(sizeof(x)*2.41)”?

非常感谢。

似乎是一个烤面包的机会!以下是我从四种不同数字大小得到的结果:

A = (sizeof(x) * 2.56 + 0.5) + 1
B = (sizeof(x) * 2.41) + 1
C = (sizeof(x) * 2.41 + 1.65)

strlen  A   B   C   Number (bytes)

4       4   3   4   -127 (1)
6       6   5   6   -32767 (2)
11      11  10  11  -2147483647 (4)
20      21  20  20  -9223372036854775807 (8)
感谢用户3386109。所有这些方案都试图估计最大可能长度,而不是实际长度(即,它们不关心“x”包含什么值)。下面是我用来生成上表的代码。我的系统中没有包含
long
,因为它的大小与
long
相同

#include <stdio.h>
#include <string.h>

#define decimal_length1(x) ((int)(sizeof(x) * 2.56 + 0.5) + 1)

#define decimal_length2(x) ((int)(sizeof(x) * 2.41) + 1)

#define decimal_length3(x) ((int)(sizeof(x) * 2.41 + 1.65))

int main() {

    char buffer[1024];

    char a = -127;
    short b = -32767;
    int c = -2147483647;
    long int d = -9223372036854775807L;

    printf("A = (sizeof(x) * 2.56 + 0.5) + 1\n");
    printf("B = (sizeof(x) * 2.41) + 1\n");
    printf("C = (sizeof(x) * 2.41 + 1.65)\n\n");

    printf("strlen\tA\tB\tC\tNumber (bytes)\n\n");

    sprintf(buffer, "%hhd", a);
    printf("%lu\t%d\t%d\t%d\t%s (%lu)\n", strlen(buffer), decimal_length1(a), decimal_length2(a), decimal_length3(a), buffer, sizeof(a));

    sprintf(buffer, "%hd", b);
    printf("%lu\t%d\t%d\t%d\t%s (%lu)\n", strlen(buffer), decimal_length1(b), decimal_length2(b), decimal_length3(b), buffer, sizeof(b));

    sprintf(buffer, "%d", c);
    printf("%lu\t%d\t%d\t%d\t%s (%lu)\n", strlen(buffer), decimal_length1(c), decimal_length2(c), decimal_length3(c), buffer, sizeof(c));

    sprintf(buffer, "%ld", d);
    printf("%lu\t%d\t%d\t%d\t%s (%lu)\n", strlen(buffer), decimal_length1(d), decimal_length2(d), decimal_length3(d), buffer, sizeof(d));

    return 0;
}
#包括
#包括
#定义十进制长度1(x)((int)(sizeof(x)*2.56+0.5)+1)
#定义十进制长度2(x)((int)(sizeof(x)*2.41)+1)
#定义十进制长度3(x)((int)(sizeof(x)*2.41+1.65))
int main(){
字符缓冲区[1024];
字符a=-127;
短b=-32767;
int c=-2147483647;
长整数d=-9223372036854775807L;
printf(“A=(sizeof(x)*2.56+0.5)+1\n”);
printf(“B=(sizeof(x)*2.41)+1\n”);
printf(“C=(sizeof(x)*2.41+1.65)\n\n”);
printf(“strlen\tA\tB\tC\t数字(字节)\n\n”);
sprintf(缓冲区,“%hhd”,a);
printf(“%lu\t%d\t%d\t%d\t%s(%lu)\n)”,strlen(缓冲区),十进制长度1(a),十进制长度2(a),十进制长度3(a),缓冲区,大小(a));
sprintf(缓冲区,“%hd”,b);
printf(“%lu\t%d\t%d\t%d\t%s(%lu)\n)”,strlen(缓冲区),十进制长度1(b),十进制长度2(b),十进制长度3(b),缓冲区,大小(b));
sprintf(缓冲区,“%d”,c);
printf(“%lu\t%d\t%d\t%d\t%s(%lu)\n)”,strlen(缓冲区),十进制长度1(c),十进制长度2(c),十进制长度3(c),缓冲区,大小(c));
sprintf(缓冲区,“%ld”,d);
printf(“%lu\t%d\t%d\t%d\t%s(%lu)\n)”,strlen(缓冲区),十进制长度1(d),十进制长度2(d),十进制长度3(d),缓冲区,大小(d));
返回0;
}

StackOverflow通常不会回答“为什么要这样设计”的问题,因为只有作者才能给出明确的答案。我假设这是用来计算打印给定类型的有符号整数所需的最大字符数。你的推理是正确的。但只要当
sizeof(x)
为1、2、4或8时,计算给出了正确的答案,那么它实际上并不重要。应该是
((int)(sizeof(x)*2.41+1.65))
以获得从8位到128位的正确答案。当
sizeof(x)
大于4时,宏高估了数字计数,因为坡度太高了。对@RyanBemrose来说,我认为你是对的。我搜索了源代码,发现这个宏从未被使用过。我想只有作者才能回答这个问题:)非常感谢。我忽略了负数。