久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

C++ 中的 For 循環使用雙突破提前一步,未達到邊

For-loop in C++ using double breaking out one step early, boundary value not reached(C++ 中的 For 循環使用雙突破提前一步,未達到邊界值)
本文介紹了C++ 中的 For 循環使用雙突破提前一步,未達到邊界值的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我有一個在 32 位 Ubuntu 8.04 上使用 gcc 4.2.4 編譯的簡單 C++ 程序.它有一個 for 循環,其中 double 變量以特定步長從 0 遞增到 1.當步長為 0.1 時,行為正是我所期望的.但是當步長為'0.05'時,循環在0.95后退出.誰能告訴我為什么會這樣?輸出遵循下面的源代碼.

I have a simple C++ program compiled using gcc 4.2.4 on 32-bit Ubuntu 8.04. It has a for-loop in which a double variable is incremented from zero to one with a certain step size. When the step size is 0.1, the behavior is what I expected. But when the step size is '0.05', the loop exits after 0.95. Can anyone tell me why this is happening? The output follows the source code below.

#include <iostream>

using namespace std;

int main()
{
    double rangeMin = 0.0;
    double rangeMax = 1.0;
    double stepSize = 0.1;

    for (double index = rangeMin; index <= rangeMax; index+= stepSize)
    {
        cout << index << endl;
    }
    cout << endl; 

    stepSize = 0.05;
    for (double index = rangeMin; index <= rangeMax; index+= stepSize)
    {
        cout << index << endl;
    }

    return 0;
}

輸出

sarva@savija-dev:~/code/scratch$ ./a.out 
0
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1

0
0.05
0.1
0.15
0.2
0.25
0.3
0.35
0.4
0.45
0.5
0.55
0.6
0.65
0.7
0.75
0.8
0.85
0.9
0.95
sarva@savija-dev:~/code/scratch$

推薦答案

當使用浮點值時,并非每個值都可以精確表示,0.95+0.05 >1 因為 0.95 不能完全用 double 值表示.

When using floating point values not every value is exactly representable, 0.95+0.05 > 1 because 0.95 is not exactly representable by a double value.

看看維基百科對浮點精度的看法.

See what Wikipedia has to say about floating point accuracy.

如果您查看 IEEE 浮點轉換器,您會看到0.95 在 64 位浮點數 (double) 中的值是 0-01111111110-1110011001100110011001100110011001100110011000 輸入這個http://www.ajdesigner.com/fl_ieee_754_word/ieee_32_bit_word.php" rel="nofollow noreferrer">浮點計算器 你得到的值為 0.95000016 并添加 0.05 將帶您超越 1.0 標記.

If you look at the IEEE floating point converter you'll see that the value of 0.95 in 64 bit floating point (double) is 0-01111111110-1110011001100110011001100110011001100110011001100110 by entering this in a floating point calculator you get the value is 0.95000016 and adding 0.05 to that takes you over the 1.0 mark.

這就是為什么你永遠不應該在循環中使用浮點數(或者更一般地說,將浮點計算的結果與精確值進行比較).

This is why you should never use floating points in loops (or more generally compare the result of floating point calculation to an exact value).

這篇關于C++ 中的 For 循環使用雙突破提前一步,未達到邊界值的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

What do compilers do with compile-time branching?(編譯器如何處理編譯時分支?)
Can I use if (pointer) instead of if (pointer != NULL)?(我可以使用 if (pointer) 而不是 if (pointer != NULL) 嗎?)
Checking for NULL pointer in C/C++(在 C/C++ 中檢查空指針)
Math-like chaining of the comparison operator - as in, quot;if ( (5lt;jlt;=1) )quot;(比較運算符的數學式鏈接-如“if((5<j<=1)))
Difference between quot;if constexpr()quot; Vs quot;if()quot;(“if constexpr()之間的區別與“if())
C++, variable declaration in #39;if#39; expression(C++,if 表達式中的變量聲明)
主站蜘蛛池模板: 99小视频| 天堂中文字幕免费一区 | 欧美黄色一级 | 精品国产一区二区三区四区 | 欧美日韩免费看 | 99re视频 | 狠狠干2021 | 国产无遮挡又黄又爽又色 | 亚洲国产一区在线 | 免费毛片观看 | 婷婷六月综合 | 午夜免费小视频 | 午夜激情小视频 | 艳妇臀荡乳欲伦交换h漫 | 色综合久久88色综合天天 | 精品国产乱码久久久久久蜜臀网站 | 成人永久免费视频 | 国产欧美精品 | 依人久久 | 在线看片a | 一级毛片免费播放视频 | 可以看av的网站 | 69av在线播放 | 国产高清视频在线播放 | 免费看a级片 | 免费91 | 特黄aaaaaaaaa真人毛片 | av免费在线观看网站 | 欧美日韩a | 蜜桃成人在线 | 青青五月天 | 精品视频免费 | 国产精品高潮呻吟 | 午夜在线影院 | 亚洲成人免费网站 | 免费性爱视频 | 天天做天天爱 | 深夜福利在线播放 | www欧美| 午夜美女福利 | 亚洲视频免费观看 |