标题:请问大牛们循环计数器换成bouble型为什么会陷入死循环????
只看楼主
蓝色的blue
Rank: 1
等 级:新手上路
帖 子:35
专家分:7
注 册:2013-10-19
结帖率:54.55%
已结贴  问题点数:10 回复次数:3 
请问大牛们循环计数器换成bouble型为什么会陷入死循环????
#include <iostream>
using namespace std;
int main()
{
    double i;
    for(i=0;i!=10;i+=0.1)
    {
        cout<<i<<endl;
        //break;
    }
    return 0;
}
一直都是死循环!!!
搜索更多相关主题的帖子: include 计数器 double 
2014-04-02 20:53
zklhp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:china
等 级:贵宾
威 望:254
帖 子:11485
专家分:33241
注 册:2007-7-10
得分:4 
浮点数因为有精度问题 不能直接比较 可以用这种方法 设定一个精度

程序代码:
    const double MIN = 1E-5;
    for (i = 0; i - 10.0 < MIN; i += 0.1)
    {
        cout << i << endl;
    }

2014-04-03 08:22
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:507
帖 子:8890
专家分:53117
注 册:2011-1-18
得分:4 
知不知浮点数的“浮点”是什么意思?
知不知“十进制的有限不循环小数”用二进制表示可能是“循环小数”?

0.0 + 0.1 + 0.1 + …… 一种可能是:
程序代码:
0.0
0.1000000000000000055511151231257827021181583404541015625
0.200000000000000011102230246251565404236316680908203125
0.3000000000000000444089209850062616169452667236328125
0.40000000000000002220446049250313080847263336181640625
0.5
0.59999999999999997779553950749686919152736663818359375
0.6999999999999999555910790149937383830547332763671875
0.79999999999999993338661852249060757458209991455078125
0.899999999999999911182158029987476766109466552734375
0.99999999999999988897769753748434595763683319091796875
1.0999999999999998667732370449812151491641998291015625
1.1999999999999999555910790149937383830547332763671875
1.3000000000000000444089209850062616169452667236328125
1.4000000000000001332267629550187848508358001708984375
1.5000000000000002220446049250313080847263336181640625
1.6000000000000003108624468950438313186168670654296875
1.7000000000000003996802888650563545525074005126953125
1.8000000000000004884981308350688777863979339599609375
1.9000000000000005773159728050814010202884674072265625
2.000000000000000444089209850062616169452667236328125
2.10000000000000053290705182007513940334320068359375
2.200000000000000621724893790087662637233734130859375
2.300000000000000710542735760100185871124267578125
2.400000000000000799360577730112709105014801025390625
2.50000000000000088817841970012523233890533447265625
2.600000000000000976996261670137755572795867919921875
2.7000000000000010658141036401502788066864013671875
2.800000000000001154631945610162802040576934814453125
2.90000000000000124344978758017532527446746826171875
3.000000000000001332267629550187848508358001708984375
3.10000000000000142108547152020037174224853515625
3.200000000000001509903313490212894976139068603515625
3.30000000000000159872115546022541821002960205078125
3.400000000000001687538997430237941443920135498046875
3.5000000000000017763568394002504646778106689453125
3.600000000000001865174681370262987911701202392578125
3.70000000000000195399252334027551114559173583984375
3.800000000000002042810365310288034379482269287109375
3.900000000000002131628207280300557613372802734375
4.0000000000000017763568394002504646778106689453125
4.10000000000000142108547152020037174224853515625
4.2000000000000010658141036401502788066864013671875
4.300000000000000710542735760100185871124267578125
4.4000000000000003552713678800500929355621337890625
4.5
4.5999999999999996447286321199499070644378662109375
4.699999999999999289457264239899814128875732421875
4.7999999999999989341858963598497211933135986328125
4.89999999999999857891452847979962825775146484375
4.9999999999999982236431605997495353221893310546875
5.099999999999997868371792719699442386627197265625
5.1999999999999975131004248396493494510650634765625
5.2999999999999971578290569595992565155029296875
5.3999999999999968025576890795491635799407958984375
5.499999999999996447286321199499070644378662109375
5.5999999999999960920149533194489777088165283203125
5.69999999999999573674358543939888477325439453125
5.7999999999999953814722175593487918376922607421875
5.899999999999995026200849679298698902130126953125
5.9999999999999946709294817992486059665679931640625
6.099999999999994315658113919198513031005859375
6.1999999999999939603867460391484200954437255859375
6.299999999999993605115378159098327159881591796875
6.3999999999999932498440102790482342243194580078125
6.49999999999999289457264239899814128875732421875
6.5999999999999925393012745189480483531951904296875
6.699999999999992184029906638897955417633056640625
6.7999999999999918287585387588478624820709228515625
6.8999999999999914734871708787977695465087890625
6.9999999999999911182158029987476766109466552734375
7.099999999999990762944435118697583675384521484375
7.1999999999999904076730672386474907398223876953125
7.29999999999999005240169935859739780426025390625
7.3999999999999896971303314785473048686981201171875
7.499999999999989341858963598497211933135986328125
7.5999999999999889865875957184471189975738525390625
7.69999999999998863131622783839702606201171875
7.7999999999999882760448599583469331264495849609375
7.899999999999987920773492078296840190887451171875
7.9999999999999875655021241982467472553253173828125
8.09999999999998721023075631819665431976318359375
8.1999999999999868549593884381465613842010498046875
8.299999999999986499688020558096468448638916015625
8.3999999999999861444166526780463755130767822265625
8.4999999999999857891452847979962825775146484375
8.5999999999999854338739169179461896419525146484375
8.699999999999985078602549037896096706390380859375
8.7999999999999847233311811578460037708282470703125
8.89999999999998436805981327779591083526611328125
8.9999999999999840127884453977458178997039794921875
9.099999999999983657517077517695724964141845703125
9.1999999999999833022457096376456320285797119140625
9.299999999999982946974341757595539093017578125
9.3999999999999825917029738775454461574554443359375
9.499999999999982236431605997495353221893310546875
9.5999999999999818811602381174452602863311767578125
9.69999999999998152588887023739516735076904296875
9.7999999999999811706175023573450744152069091796875
9.899999999999980815346134477294981479644775390625
9.9999999999999804600747665972448885440826416015625
10.0999999999999801048033987171947956085205078125
10.1999999999999797495320308371447026729583740234375

2014-04-03 09:30
hubinyes
Rank: 7Rank: 7Rank: 7
等 级:贵宾
威 望:11
帖 子:104
专家分:557
注 册:2014-2-4
得分:4 
浮点数不能直接比较大小
2014-04-03 11:31



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-429591-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.790091 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved