什么是“双”在ceil(双)吗? [英] What does the "double" do in ceil(double)?
问题描述
我有一个数字(让我们说,34),我想找到它的十的下一个倍数。我可以这样做:
I have a number (let's say, 34), and I want to find its next multiple of ten. I can do this by:
- 将数字除以10
- 乘以10。
经过一番研究,在Objective C中的代码:
After a bit of research, I discovered that this is the code for that in Objective C:
int number = 34;
int roundedNumber = ceil((double)number/10)*10;
我的问题是:(double)
for,为什么删除(double)
导致它舍入而不是向上?
My question is: what is the (double)
for, and why does removing (double)
cause it to round down instead of up?
从googling,将浮动格式更改为双精度,但老实说,这对我来说太复杂了。任何人都可以提供一个简单的解释,它在做什么?
I understand from googling that changes the float format to "double precision" but, to be honest, this is way too complicated for me. Can anyone provide a simple explanation of what it is doing?
推荐答案
如果您没有投放,则会发生以下情况(如果数字为34)。
If you don't have the cast the following happens (if number is 34).
- 使用整数运算,数字/ 10是数字/ 10向下舍入,即3。
- ceil(3) / li>
- 3 * 10 = 30
如果您有演员, p>
If you have the cast, the following happens:
- (double)number = 34.0
- 34.0 / 10 = 3.4
- ceil(3.4)= 4.0
- 4.0 * 10 = 40
- (double)number = 34.0
- 34.0 / 10 = 3.4
- ceil(3.4) = 4.0
- 4.0*10 = 40
要实现的是整数除法总是向0舍入。
The important thing to realise is Integer division always rounds towards 0.
这篇关于什么是“双”在ceil(双)吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!