【发布时间】:2012-12-31 12:09:39
【问题描述】:
当我用一个应该在 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-01-17 14:24:17.512 MyProject[1798:c07] 1.000000 2.000000 0.500000 0.000000 0.500000
我不知道发生了什么。
【问题讨论】:
-
你可以做 1.f/2, 1.0/2, 1/2.0000f, 1/(float)2... 基本上除了 2 个整数之外的任何东西
-
嘿,等一下……你怎么把
0.500000换成了e是4 / 2?应该是2.000000。 -
@Pang:他的裤子着火了。
标签: objective-c math integer division