Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ integer floor function

Tags:

c++

math

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?

EDIT: Greatest integer function is integer less than or equal to X. Example:

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

What I want to know is 5/3 going to produce a double? Because if so I will have loss of precision when converting to int.

Hope this makes sense.

like image 912
Thomas Avatar asked Apr 12 '10 13:04

Thomas


People also ask

Does C have floor division?

(Floor) In the C Programming Language, the floor function returns the largest integer that is smaller than or equal to x (ie: rounds downs the nearest integer).

Is integer division the same as floor in C?

the answer is yes.

What is the difference between Ceil () and floor () with examples?

The ceil function and the floor function have different definitions. The ceil function returns the smallest integer value which is greater than or equal to the specified number, whereas the floor function returns the largest integer value which is less than or equal to the specified number.

What is floor function?

The Math.floor() function always rounds down and returns the largest integer less than or equal to a given number.


2 Answers

Integer division gives integer results, so 5 / 3 is 1 and 5 % 3 is 2 (the remainder operator). 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. 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.

like image 50
David Thornley Avatar answered Oct 26 '22 13:10

David Thornley


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 (e.g. int64_t has a higher precision than double can represent without error)

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. 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....

like image 29
Ichthyo Avatar answered Oct 26 '22 14:10

Ichthyo