“双”是什么意思?在天花板上做(双)?
我有一个数字(假设是 34),我想找到它的下一个 10 的倍数。我可以通过以下方法做到这一点:
- 将数字除以 10
- 将其四舍五入为整数
- 乘以 10。
经过一番研究,我发现这是 Objective C 中的代码:
int number = 34;
int roundedNumber = ceil((double)number/10)*10;
我的问题是: (double)
for,为什么删除 (double)
会导致它向下舍入而不是向上舍入?
我从谷歌搜索中了解到,将浮点格式更改为“双精度”,但说实话,这对我来说太复杂了。谁能简单解释一下它在做什么?
I have a number (let's say, 34), and I want to find its next multiple of ten. I can do this by:
- Dividing the number by 10
- Rounding it up to a whole number
- Multiplying by 10.
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;
My question is: what is the (double)
for, and why does removing (double)
cause it to round down instead of up?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您没有演员表,则会发生以下情况(如果数字为 34)。
如果进行强制转换,会发生以下情况:
需要认识到的重要一点是整数除法总是向 0 舍入。
If you don't have the cast the following happens (if number is 34).
If you have the cast, the following happens:
The important thing to realise is Integer division always rounds towards 0.
它将
number
转换为double
,以便执行浮点除法而不是整数除法。比较1/2
与1.0/2
。It casts
number
as adouble
so that float division is performed instead of integer division. Compare1/2
versus1.0/2
.