简体   繁体   English

C ++整数楼层函数

[英]C++ integer floor function

I want to implement greatest integer function. 我想实现最大的整数函数。 [The "greatest integer function" is a quite standard name for what is also known as the floor function.] [“最大整数函数”是一个非常 标准的名称 ,也称为地板函数。]

int x = 5/3;

My question is with greater numbers could there be a loss of precision as 5/3 would produce a double? 我的问题是,由于5/3会产生双倍的精度,因此可能会有更多的精度损失?

EDIT: Greatest integer function is integer less than or equal to X. Example: 编辑:最大整数函数是小于或等于X的整数。示例:

4.5 = 4
4 = 4
3.2 = 3
3 = 3

What I want to know is 5/3 going to produce a double? 我想知道的是5/3会产生双倍? Because if so I will have loss of precision when converting to int. 因为如果是这样,我将在转换为int时失去精度。

Hope this makes sense. 希望这是有道理的。

You will lose the fractional portion of the quotient. 你将失去商的小数部分。 So yes, with greater numbers you will have more relative precision, such as compared with 5000/3000 . 所以是的,数字越大,你的相对精度就越高,比如5000/3000

However, 5 / 3 will return an integer, not a double. 但是, 5 / 3将返回一个整数,而不是一个整数。 To force it to divide as double, typecast the dividend as static_cast<double>(5) / 3 . 为了强制它除以double,将被除数分为static_cast<double>(5) / 3

Integer division gives integer results, so 5 / 3 is 1 and 5 % 3 is 2 (the remainder operator). 整数除法给出整数结果,因此5/3为1,5%3为2(余数运算符)。 However, this doesn't necessarily hold with negative numbers. 但是,这并不一定与负数相符。 In the original C++ standard, -5 / 3 could be either -1 (rounding towards zero) or -2 (the floor), but -1 was recommended. 在最初的C ++标准中,-5 / 3可以是-1(向零舍入)或-2(向下舍入),但建议使用-1。 In the latest C++0B draft (which is almost certainly very close to the final standard), it is -1, so finding the floor with negative numbers is more involved. 在最新的C ++ 0B草案中(几乎肯定非常接近最终标准),它是-1,因此更多地涉及找到负数的底线。

5/3将始终产生1(整数),如果你做5.0 / 3或5 / 3.0,结果将是双倍。

Since in C and C++, as others have said, / is integer division, it will return an int. 因为在C和C ++中,正如其他人所说,/是整数除法,它将返回一个int。 in particular, it will return the floor of the double answer... (C and C++ always truncate) So, basically 5/3 is exactly what you want. 特别是,它将返回双重答案的底线...(C和C ++总是截断)所以,基本上5/3正是你想要的。

It may get a little weird in negatives as -5/3 => -2 which may or may not be what you want... 它可能会在负数中变得有些奇怪,因为-5/3 => -2可能是也可能不是你想要的......

As far as I know, there is no predefined function for this purpose. 据我所知,没有为此目的预定义的功能。 It might be necessary to use such a function, if for some reason floating-point calculations are out of question (eg int64_t has a higher precision than double can represent without error) 可能有必要使用这样的函数,如果由于某种原因浮点计算是不可能的(例如int64_t的精度高于double可以表示没有错误)

We could define this function as follows: 我们可以定义这个函数如下:

#include <cmath>

inline long
floordiv (long num, long den)
{
  if (0 < (num^den))
    return num/den;
  else
    {
      ldiv_t res = ldiv(num,den);
      return (res.rem)? res.quot-1 
                      : res.quot;
    }
}

The idea is to use the normal integer divison, but adjust for negative results to match the behaviour of the double floor(double) function. 我们的想法是使用正常的整数除法,但调整负面结果以匹配double floor(double)函数的行为。 The point is to truncate always towards the next lower integer, irrespective of the position of the zero point. 不管零点的位置如何,该点总是截断到下一个较低的整数。 This can be very important if the intention is to create even sized intervals. 如果打算创建大小均匀的间隔,这可能非常重要。

Timing measurements show that this function here only creates a small overhead compared with the built-in / operator, but of course the floating point based floor function is significantly faster.... 时序测量表明,与内置/运算符相比,此功能仅产生较小的开销,但当然基于浮点的floor功能明显更快....

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM