float和double之间的区别 [英] Difference between float and double

查看:191
本文介绍了float和double之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道,我读过有关双precision和单precision等,但他们应该给在大多数情况下,对吧?

I know, I've read about the difference between double precision and single precision, etc. But they should give the same results on most cases right?

我解决一个编程竞赛问题,有浮动是不是真正的大点数计算,所以我决定,而不是使用双精度浮点,我查了 - 我得到正确的结果。但是,当我送解决方案,它说,只有1 10的测试是正确的。我一次又一次的检查,直到我发现,使用浮动不采用双一样的。我把两倍计算和双为输出,程序给出了相同的结果,但这次它正确地通过了所有的10个测试。

I was solving a problem on a programming contest and there were calculations with floating point numbers that were not really big, so I decided to use float instead of double, and I checked it - I was getting the correct results. But when I send the solution, it said only 1 of 10 tests was correct. I checked again and again, until I found that using float is not the same using double. I put double for the calculations and double for the output, and the program gave the SAME results, but this time it passed all the 10 tests correctly.

我再说一遍,输出是一样的,结果都是一样的,但把浮动没有工作 - 双人床。该值没有这么大过,节目介绍了都与float和double相同的测试相同的结果,但网上法官只接受双提供的解决方案。

I repeat, the output was the SAME, the results were the SAME, but putting float didn't work - only double. The values were not so big too, and the program gave the same results on the same tests both with float and double, but the online judge accepted only the double-provided solution.

为什么呢?有什么区别?

Why? What is the difference?

推荐答案

巨大差异。

顾名思义,一个 双击 具有2倍的<一的precision href=\"http://en.wikipedia.org/wiki/Single_$p$pcision_floating-point_format\"><$c$c>float[1].一般来说双有precision 15至16十进制数字,而浮动只有7个。

As the name implies, a double has 2x the precision of float[1]. In general a double has 15 to 16 decimal digits of precision, while float only has 7.

这precision的损失可能会导致截断误差更容易飘起来,例如

This precision loss could lead to truncation errors much easier to float up, e.g.

    float a = 1.f / 81;
    float b = 0;
    for (int i = 0; i < 729; ++ i)
            b += a;
    printf("%.7g\n", b);   // prints 9.000023

,而

    double a = 1.0 / 81;
    double b = 0;
    for (int i = 0; i < 729; ++ i)
            b += a;
    printf("%.15g\n", b);   // prints 8.99999999999996

此外,浮动的最大值仅为 3e38 ,但双约 1.7e308 ,所以用浮动可以打无限的两倍更容易为一些简单的如计算60!

Also, the maximum value of float is only about 3e38, but double is about 1.7e308, so using float can hit Infinity much easier than double for something simple e.g. computing 60!.

也许他们的测试案例包含这会导致你的程序失败,这些庞大的数字。

Maybe the their test case contains these huge numbers which causes your program to fail.

当然有时甚至双击不够准确,因此我们有长双 [1 ] (上面的例子给出了在Mac 9.000000000000000066),但所有这些浮点类型的舍入误差吃亏,所以如果precision是非常重要的(如金钱处理),你应该使用 INT 或分数类。

Of course sometimes even double isn't accurate enough, hence we have long double[1] (the above example gives 9.000000000000000066 on Mac), but all these floating point types suffer from round-off errors, so if precision is very important (e.g. money processing) you should use int or a fraction class.

BTW,不要使用 + = 来总结大量的浮点数的错误迅速积累的。如果你正在使用Python,使用 FSUM 。否则,请尝试执行 Kahan的求和算法

BTW, don't use += to sum lots of floating point numbers as the errors accumulate quickly. If you're using Python, use fsum. Otherwise, try to implement the Kahan summation algorithm.

[1]:C和C ++标准不指定浮动双重新presentation 长双。这可能是所有这三个实施为IEEE双链precision。然而,对于大多数的架构(GCC,MSVC; 86,64,ARM)浮动的确实是一个IEEE单precision浮点数(binary32)和双击的一个IEEE双precision浮点数(binary64)。

[1]: The C and C++ standards do not specify the representation of float, double and long double. It is possible that all three implemented as IEEE double-precision. Nevertheless, for most architectures (gcc, MSVC; x86, x64, ARM) float is indeed a IEEE single-precision floating point number (binary32), and double is a IEEE double-precision floating point number (binary64).

这篇关于float和double之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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