什么是“双”在ceil(双)吗? [英] What does the "double" do in ceil(double)?

查看:168
本文介绍了什么是“双”在ceil(双)吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数字(让我们说,34),我想找到它的十的下一个倍数。我可以这样做:

I have a number (let's say, 34), and I want to find its next multiple of ten. I can do this by:


  1. 将数字除以10


  2. 乘以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).


  1. 使用整数运算,数字/ 10是数字/ 10向下舍入,即3。

  2. ceil(3) / li>
  3. 3 * 10 = 30

如果您有演员, p>

If you have the cast, the following happens:


  1. (double)number = 34.0

  2. 34.0 / 10 = 3.4

  3. ceil(3.4)= 4.0

  4. 4.0 * 10 = 40

  1. (double)number = 34.0
  2. 34.0 / 10 = 3.4
  3. ceil(3.4) = 4.0
  4. 4.0*10 = 40

要实现的是整数除法总是向0舍入。

The important thing to realise is Integer division always rounds towards 0.

这篇关于什么是“双”在ceil(双)吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆