美文网首页
Int64/UInt64 转 Double 精度丢失问题

Int64/UInt64 转 Double 精度丢失问题

作者: devVector | 来源:发表于2021-07-09 17:08 被阅读0次
    uint64_t max = UINT64_MAX;
    double d = (double)max;
    uint64_t n = (uint64_t)d;
    printf("max:%llu, n:%llu, d:%lf, equal: %s\n", max, n, d, max == n ? "true" : "false");

输出结果

max:18446744073709551615, n:0, d:18446744073709551616.000000, equal: false

double 只有52位尾数位, 最多表示53位有效位,
uint64_t 转成 double的时候, 丢失了11位的精度
int64_t 转成 double的时候, 会丢失10位的精度

相关文章

网友评论

      本文标题:Int64/UInt64 转 Double 精度丢失问题

      本文链接:https://www.haomeiwen.com/subject/ookoyltx.html