可能重複:
What is the behavior of integer division in C?司與1和0之間預期的結果總是給出0
當我做這應該是1和0之間回答的除法計算,它總是返回0
試試這個:
float a = 1;
float b = 2;
float c = a/b; //variable divide by variable gives 0.5 as it should
float d = 1/2; //number divide by number gives 0
float e = 4/2;
NSLog(@"%f %f %f %f %f", a,b,c, d, e);
我得到這個從控制檯:
2013年1月17日14:24:17.512 MyProject的[1798:C07] 1.000000 2.000000 0.500000 0.000000 0.500000
我不知道是怎麼回事上。
見[此](http://stackoverflow.com/q/3602827/1402846),[此](http://stackoverflow.com/q/2976011/1402846 ),[this](http://stackoverflow.com/q/2345902/1402846),[this](http://stackoverflow.com/q/4197841/1402846),[this](http:// stackoverflow。 COM/q /一百四十〇萬二千八百四十六分之八百四十五萬九千三百五十一)。 – Pang
你可以做1.f/2,1.0/2,1/2.0000f,1 /(float)2 ...基本上任何事情,但是2個整數 –
嘿,等一下......你怎麼得到0.500000呢? 'e'是'4/2'?它應該是'2.000000'。 – Pang