C ++ Integer Function

I want to implement the largest integer function. [The largest integer function is the standard name for what is also known as the gender function.]

int x = 5/3;

My question with large numbers may be a loss of accuracy, since 5/3 will lead to double?

EDIT: The largest integer function is an 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 to create a double? Because if that is the case, I will have a loss of precision when converting to int.

Hope this makes sense.

+6
source share
5 answers

. , , , 5000/3000.

, 5/3 , . double, static_cast<double>(5)/3.

+6

, 5/3 1 5% 3 2 ( ). . ++ -5/3 -1 ( ), -2 (), -1 . ++ 0B ( ) -1, .

+3

5/3 1 ( ), 5.0/3 5/3.0, .

+2

C ++, ,/ , int. , ... (C ++ ) , 5/3 - , .

-5/3 = > -2, , ...

0

, . , ​​, - (, int64_t , double )

:

#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;
    }
}

, divison, double floor(double). , , . , .

, /, , , floor ....

0

All Articles