objective-c除总是返回0

时间:2021-12-02 07:28:16

Something really weird is happening.

一些非常奇怪的事情正在发生。

float p1 = (6 / 100);
NSLog(@"p1 = %f", p1);

With those two lines of code I get the following output:

通过这两行代码,我得到以下输出:

p1 = 0.000000

p1 = 0.000000

Why is a simple devide with static numbers not working! I have so much work to do to deal with divide not working! What he heck, am I crazy?

为什么一个带静态数字的简单偏差不能工作!我有那么多的工作要做,以应付分不工作!管他呢,我疯了吗?

4 个解决方案

#1


35  

Those constants are integers, so the math is done with integer math. Try

这些常数都是整数,所以数学是用整数来做的。试一试

float p1 = (6.0 / 100.0);

edit — @Stephen Canon wisely points out that since "p1" is a float, there's no reason not to do the whole computation as float:

@Stephen Canon明智地指出,由于“p1”是一个浮点数,所以没有理由不将整个计算作为浮点数:

float p1 = (6.0f / 100.0f);

Now, since those things are both constants, I'd imagine there's a really good chance that the work is going to be done by the compiler anyway. It's also true that because on some modern machines (ie, Intel architecture), the floating-point processor instruction set is sufficiently weird that something that seems like an obvious "optimization" may or may not work out that way. Finally I suppose it might be the case that doing the operation with float constants could (in some cases) give a different result that doing the operation with double values and then casting to float, which if true would probably be the best argument for deciding one way or the other.

既然这些都是常量,我想编译器很有可能完成这些工作。同样正确的是,因为在一些现代机器(例如,英特尔架构)上,浮点处理器指令集非常怪异,以至于一些看似明显的“优化”可能会,也可能不会。最后我想这可能是做手术的情况下浮点数常量(在某些情况下)能给一个不同的结果,做手术用双值然后铸造浮动,如果真正的决定可能是最好的理由或另一种方式。

#2


12  

Since both of these numbers are considered to be integers by the computer, integer math is performed and an integer is returned with no decimals which is 0 (0.06), then converted to float and stored in the variable p1.

由于计算机认为这两个数字都是整数,因此执行整数数学运算,返回一个整数,没有小数,即0(0.06),然后转换为浮点数并存储在变量p1中。

A least one number (or both) has to be float to do floating point math, when you append a decimal to a number you tell the computer that the constant is a floating point number.

至少有一个数字(或两个数字)必须是浮点数,当你将一个小数附加到一个数字时,你告诉计算机这个常数是一个浮点数。

float p1 = (6.0 / 100);

Or you could typecast it

或者你也可以排版

float p1 = ((float)6 / 100);

#3


9  

Your assignment contains an integer divide, which returns zero if the number you are dividing by is greater. You probably meant to do:

你的赋值包含一个整数除法,如果你除以的数大于零,这个除法将返回零。你可能想说:

float p1 = (6.0f / 100.0f);

#4


1  

A work around to divide two int variables and get float result.

一个关于划分两个int变量并得到浮点结果的工作。

int x = 5;
int y = 7;

float z = x / y;  // always 0

The fix:

解决办法:

float z = 1.0 * x / y;

Note: if you change the order this solution won't work.

注意:如果你改变顺序,这个解决方案将不起作用。

Edit: According to this answer previous answer You can use this

编辑:根据这个答案你可以使用这个

float z = (float) x / y;

#1


35  

Those constants are integers, so the math is done with integer math. Try

这些常数都是整数,所以数学是用整数来做的。试一试

float p1 = (6.0 / 100.0);

edit — @Stephen Canon wisely points out that since "p1" is a float, there's no reason not to do the whole computation as float:

@Stephen Canon明智地指出,由于“p1”是一个浮点数,所以没有理由不将整个计算作为浮点数:

float p1 = (6.0f / 100.0f);

Now, since those things are both constants, I'd imagine there's a really good chance that the work is going to be done by the compiler anyway. It's also true that because on some modern machines (ie, Intel architecture), the floating-point processor instruction set is sufficiently weird that something that seems like an obvious "optimization" may or may not work out that way. Finally I suppose it might be the case that doing the operation with float constants could (in some cases) give a different result that doing the operation with double values and then casting to float, which if true would probably be the best argument for deciding one way or the other.

既然这些都是常量,我想编译器很有可能完成这些工作。同样正确的是,因为在一些现代机器(例如,英特尔架构)上,浮点处理器指令集非常怪异,以至于一些看似明显的“优化”可能会,也可能不会。最后我想这可能是做手术的情况下浮点数常量(在某些情况下)能给一个不同的结果,做手术用双值然后铸造浮动,如果真正的决定可能是最好的理由或另一种方式。

#2


12  

Since both of these numbers are considered to be integers by the computer, integer math is performed and an integer is returned with no decimals which is 0 (0.06), then converted to float and stored in the variable p1.

由于计算机认为这两个数字都是整数,因此执行整数数学运算,返回一个整数,没有小数,即0(0.06),然后转换为浮点数并存储在变量p1中。

A least one number (or both) has to be float to do floating point math, when you append a decimal to a number you tell the computer that the constant is a floating point number.

至少有一个数字(或两个数字)必须是浮点数,当你将一个小数附加到一个数字时,你告诉计算机这个常数是一个浮点数。

float p1 = (6.0 / 100);

Or you could typecast it

或者你也可以排版

float p1 = ((float)6 / 100);

#3


9  

Your assignment contains an integer divide, which returns zero if the number you are dividing by is greater. You probably meant to do:

你的赋值包含一个整数除法,如果你除以的数大于零,这个除法将返回零。你可能想说:

float p1 = (6.0f / 100.0f);

#4


1  

A work around to divide two int variables and get float result.

一个关于划分两个int变量并得到浮点结果的工作。

int x = 5;
int y = 7;

float z = x / y;  // always 0

The fix:

解决办法:

float z = 1.0 * x / y;

Note: if you change the order this solution won't work.

注意:如果你改变顺序,这个解决方案将不起作用。

Edit: According to this answer previous answer You can use this

编辑:根据这个答案你可以使用这个

float z = (float) x / y;