C语言 Objective-C 整数算法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4197841/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Objective-C Integer Arithmetic
提问by Stateful
I'm trying to calculate some numbers in an iPhone application.
我正在尝试在 iPhone 应用程序中计算一些数字。
int i = 12;
int o = (60 / (i * 50)) * 1000;
I would expect o to be 100 (that's milliseconds) in this example but it equals 0 as displayed by NSLog(@"%d", o).
在本例中,我希望 o 为 100(即毫秒),但它等于 0,如 NSLog(@"%d", o) 所示。
This also equals 0.
这也等于 0。
int o = 60 / (i * 50) * 1000;
This equals 250,000, which is straight left-to-right math.
这等于 250,000,这是从左到右的直接数学。
int o = 60 / i * 50 * 1000;
What's flying over my head here?
什么在我头上飞来飞去?
Thanks,
Nick
谢谢,
尼克
回答by High Performance Mark
In Objective-C /performs integer division on integer arguments, so 4/5 is truncated to 0, 3/2 is truncated to 1, and so on. You probably want to cast some of your numbers to floating-point forms before performing division.
在 Objective-C 中/对整数参数执行整数除法,因此 4/5 被截断为 0,3/2 被截断为 1,依此类推。在执行除法之前,您可能希望将某些数字转换为浮点形式。
You're also running in to issues with precedence. In the expression
您还遇到了优先级问题。在表达式中
60 / (i * 50) * 1000
the term inside the parentheses is calculated first, so 60 is divided by 600 which produces the result 0. In
首先计算括号内的项,因此 60 除以 600,结果为 0。
60 / i * 50 * 1000
the first operation is to divide 60 by 12 which gives the result 5 and then the multiplications are carried out.
第一个操作是将 60 除以 12,得到结果 5,然后进行乘法运算。
回答by Dave DeLong
An integer divided by an integer is an integer.
整数除以整数是整数。
so 60/600is not 0.1, it is 0.
所以60/600不是0.1,它是0。
Cast (or declare) some stuff as floatinstead.
铸造(或声明)一些东西float。
回答by R.. GitHub STOP HELPING ICE
Replace:
代替:
int o = (60 / (i * 50)) * 1000;
with:
和:
int o = 1200/i;
回答by Neth
It's doing integer math. 60 / (12 * 50) is 0.1, truncates to 0.
它正在做整数数学。60 / (12 * 50) 为 0.1,截断为 0。
Should work if you force floating point and then cast back to an integer.
如果您强制浮点数然后转换回整数,则应该可以工作。
int o = (int)(60.0 / ((double) i / 50.0) * 1000.0;
Probably not really necessary to make everything a double.
可能没有必要让所有东西都变成双倍。
回答by Alex Reynolds
By order of precedence, the operation:
按照优先顺序,操作:
60 / (12 * 50)
is performed before multiplying by 1000.
在乘以 之前执行1000。
This value is less than 1 and is cast to an int, which truncates it to 0. And 0times anything is 0.
该值小于 1 并被强制转换为int,从而将其截断为0。和0时代任何事情都是0。
Use a floator first multiply by 1000to ensure you're not ending up with propagating a 0in your calculations.
使用 afloat或 first 乘以1000确保您最终不会0在计算中传播 a 。
回答by Dima
All the operations in your expression are performed in integer arithmetic, meaning that the fractional part of each intermediate result is truncated. This means that if you divide a smaller integer by a larger integer you will always get 0.
表达式中的所有运算均以整数算术执行,这意味着每个中间结果的小数部分都会被截断。这意味着如果你将一个较小的整数除以一个较大的整数,你总是会得到 0。
To get the result you want you must either make sure the operations are performed in a particular order, or you must use floats. For example the result of
要获得您想要的结果,您必须确保以特定顺序执行操作,或者您必须使用浮点数。例如结果
int o = (60.0 / (i * 50.0)) * 1000.0;
should be o = 100.
应该是 o = 100。
回答by Mosib
I think you need to use float here instead of int. It will work the way you want! Will give you answer in decimals as well.
我认为您需要在这里使用 float 而不是 int。它会按照你想要的方式工作!也会给你小数的答案。

