<small id='NPPpC'></small><noframes id='NPPpC'>

    1. <i id='NPPpC'><tr id='NPPpC'><dt id='NPPpC'><q id='NPPpC'><span id='NPPpC'><b id='NPPpC'><form id='NPPpC'><ins id='NPPpC'></ins><ul id='NPPpC'></ul><sub id='NPPpC'></sub></form><legend id='NPPpC'></legend><bdo id='NPPpC'><pre id='NPPpC'><center id='NPPpC'></center></pre></bdo></b><th id='NPPpC'></th></span></q></dt></tr></i><div id='NPPpC'><tfoot id='NPPpC'></tfoot><dl id='NPPpC'><fieldset id='NPPpC'></fieldset></dl></div>
        <bdo id='NPPpC'></bdo><ul id='NPPpC'></ul>
      <legend id='NPPpC'><style id='NPPpC'><dir id='NPPpC'><q id='NPPpC'></q></dir></style></legend>

      <tfoot id='NPPpC'></tfoot>

    2. 为什么比较 double 和 float 会导致意外结果?

      时间:2023-10-07
          <legend id='6VE0Z'><style id='6VE0Z'><dir id='6VE0Z'><q id='6VE0Z'></q></dir></style></legend>
          <i id='6VE0Z'><tr id='6VE0Z'><dt id='6VE0Z'><q id='6VE0Z'><span id='6VE0Z'><b id='6VE0Z'><form id='6VE0Z'><ins id='6VE0Z'></ins><ul id='6VE0Z'></ul><sub id='6VE0Z'></sub></form><legend id='6VE0Z'></legend><bdo id='6VE0Z'><pre id='6VE0Z'><center id='6VE0Z'></center></pre></bdo></b><th id='6VE0Z'></th></span></q></dt></tr></i><div id='6VE0Z'><tfoot id='6VE0Z'></tfoot><dl id='6VE0Z'><fieldset id='6VE0Z'></fieldset></dl></div>

          • <small id='6VE0Z'></small><noframes id='6VE0Z'>

            <tfoot id='6VE0Z'></tfoot>

                <tbody id='6VE0Z'></tbody>

                <bdo id='6VE0Z'></bdo><ul id='6VE0Z'></ul>
                本文介绍了为什么比较 double 和 float 会导致意外结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                可能的重复:
                浮点与浮点文字比较的奇怪输出

                float f = 1.1;
                double d = 1.1;
                if(f == d) // returns false!
                

                为什么会这样?

                推荐答案

                floatdouble 数字考虑的重要因素是:
                精度 &四舍五入

                The important factors under consideration with float or double numbers are:
                Precision & Rounding

                精度:
                浮点数的精度是它可以表示多少位而不丢失它包含的任何信息.

                考虑分数 1/3.这个数字的十进制表示是0.33333333333333…,其中3 向无穷大.一个无限长的数字需要无限的内存来精确地描述,但是 floatdouble 数据类型通常只有 48 字节.因此浮点数 &双数只能存储一定数量的数字,其余的肯定会丢失.因此,没有明确准确的方法来表示浮点数或双精度数需要比变量所能容纳的精度更高的数字.

                Consider the fraction 1/3. The decimal representation of this number is 0.33333333333333… with 3′s going out to infinity. An infinite length number would require infinite memory to be depicted with exact precision, but float or double data types typically only have 4 or 8 bytes. Thus Floating point & double numbers can only store a certain number of digits, and the rest are bound to get lost. Thus, there is no definite accurate way of representing float or double numbers with numbers that require more precision than the variables can hold.

                四舍五入:
                binarydecimal (base 10) 数字之间存在不明显的差异.
                考虑分数 1/10.在decimal 中,这可以很容易地表示为0.1,而0.1 可以被认为是一个易于表示的数字.但是,在二进制中,0.1 用无限序列表示:0.00011001100110011…

                Rounding:
                There is a non-obvious differences between binary and decimal (base 10) numbers.
                Consider the fraction 1/10. In decimal, this can be easily represented as 0.1, and 0.1 can be thought of as an easily representable number. However, in binary, 0.1 is represented by the infinite sequence: 0.00011001100110011…

                示例:

                #include <iomanip>
                int main()
                {
                    using namespace std;
                    cout << setprecision(17);
                    double dValue = 0.1;
                    cout << dValue << endl;
                }
                

                这个输出是:

                0.10000000000000001
                

                没有

                0.1.
                

                这是因为 double 由于内存有限而不得不截断近似值,这导致数字不完全是 0.1.这种情况称为舍入误差.

                This is because the double had to truncate the approximation due to it’s limited memory, which results in a number that is not exactly 0.1. Such an scenario is called a Rounding error.

                每当比较两个接近的浮点数和双精度数时,就会出现这种舍入错误,最终比较会产生不正确的结果,这就是您永远不应该使用 == 比较浮点数或双精度数的原因.

                Whenever comparing two close float and double numbers such rounding errors kick in and eventually the comparison yields incorrect results and this is the reason you should never compare floating point numbers or double using ==.

                你能做的最好的事情就是计算它们的差异并检查它是否小于一个 epsilon.

                The best you can do is to take their difference and check if it is less than an epsilon.

                abs(x - y) < epsilon
                

                这篇关于为什么比较 double 和 float 会导致意外结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:C++ 类实例上的静态成员方法调用 下一篇:首次编译错误时自动停止 Visual C++ 2008 构建?

                相关文章

                最新文章

                <small id='1GkTt'></small><noframes id='1GkTt'>

                  • <bdo id='1GkTt'></bdo><ul id='1GkTt'></ul>

                    <tfoot id='1GkTt'></tfoot>
                    <legend id='1GkTt'><style id='1GkTt'><dir id='1GkTt'><q id='1GkTt'></q></dir></style></legend>

                    <i id='1GkTt'><tr id='1GkTt'><dt id='1GkTt'><q id='1GkTt'><span id='1GkTt'><b id='1GkTt'><form id='1GkTt'><ins id='1GkTt'></ins><ul id='1GkTt'></ul><sub id='1GkTt'></sub></form><legend id='1GkTt'></legend><bdo id='1GkTt'><pre id='1GkTt'><center id='1GkTt'></center></pre></bdo></b><th id='1GkTt'></th></span></q></dt></tr></i><div id='1GkTt'><tfoot id='1GkTt'></tfoot><dl id='1GkTt'><fieldset id='1GkTt'></fieldset></dl></div>