为什么轮()和CEIL()不返回一个整数? [英] Why do round() and ceil() not return an integer?

查看:136
本文介绍了为什么轮()和CEIL()不返回一个整数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一段时间后,我发现自己四舍五入一些数字,我总是要的结果转换为整数:

Once in a while, I find myself rounding some numbers, and I always have to cast the result to an integer:

int rounded = (int) floor(value);

为什么所有的舍入函数( CEIL()地板())返回一个浮点数,而不是的整数我觉得这是pretty不直观,而且很想有一些解释!

Why do all rounding functions (ceil(), floor()) return a floating number, and not an integer? I find this pretty non-intuitive, and would love to have some explanations!

推荐答案

这些函数返回的整数值可能太大的整型(int和long等)来存储。为了避免溢出,从而会产生不确定的结果,应用程序应该将其分配给一个整数类型之前执行对返回的值范围检查。

The integral value returned by these functions may be too large to store in an integer type (int, long, etc.). To avoid an overflow, which will produce undefined results, an application should perform a range check on the returned value before assigning it to an integer type.

从所述细胞(3)Linux手册页。

from the ceil(3) Linux man page.

这篇关于为什么轮()和CEIL()不返回一个整数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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