本文介绍了为什么我需要17显著位(而不是16)重新present双?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

能有人给我一个浮点数(双precision),需要超过16显著小数位数重新present的例子吗?

Can someone give me an example of a floating point number (double precision), that needs more than 16 significant decimal digits to represent it?

我,有时你需要到17位,但我不能够找到这样一些例子(16似乎只要我)。

I have found in this thread that sometimes you need up to 17 digits, but I am not able to find an example of such a number (16 seems enough to me).

有人可以澄清这一点?

非常感谢!

推荐答案

我的对方的回答是大错特错了。

My other answer was dead wrong.

#include <stdio.h>

int
main(int argc, char *argv[])
{
    unsigned long long n = 1ULL << 53;
    unsigned long long a = 2*(n-1);
    unsigned long long b = 2*(n-2);
    printf("%llu\n%llu\n%d\n", a, b, (double)a == (double)b);
    return 0;
}

编译并运行看看:

Compile and run to see:

18014398509481982
18014398509481980
0

a和b都只是2 *(2 ^ 53-1)和2 *(2 ^ 53-2)。

a and b are just 2*(2^53-1) and 2*(2^53-2).

这些都是17位的基数为10号。当舍入为16位数字,它们是相同的。然而,a和b清楚仅需要precision的53位基-2-重新present。所以,如果你把b和扮演他们翻一番,你得到你的反例。

Those are 17-digit base-10 numbers. When rounded to 16 digits, they are the same. Yet a and b clearly only need 53 bits of precision to represent in base-2. So if you take a and b and cast them to double, you get your counter-example.

这篇关于为什么我需要17显著位(而不是16)重新present双?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 10:57