Skip to main content
added 4 characters in body
Source Link
cubuspl42
  • 8.6k
  • 4
  • 46
  • 67

There's a solution for both positive and negative x but only for positive y with just 1 division and without branches:

int ceildiv_ceil(int x, int y) { return x / y + (x % y > 0); } 

Note, if x is positive then division is towards zero, and we should add 1 if reminder is not zero.

If x is negative then division is towards zero, that's what we need, and we will not add anything because x % y is not positive

There's a solution for both positive and negative x but only for positive y with just 1 division and without branches:

int ceil(int x, int y) { return x / y + (x % y > 0); } 

Note, if x is positive then division is towards zero, and we should add 1 if reminder is not zero.

If x is negative then division is towards zero, that's what we need, and we will not add anything because x % y is not positive

There's a solution for both positive and negative x but only for positive y with just 1 division and without branches:

int div_ceil(int x, int y) { return x / y + (x % y > 0); } 

Note, if x is positive then division is towards zero, and we should add 1 if reminder is not zero.

If x is negative then division is towards zero, that's what we need, and we will not add anything because x % y is not positive

added 18 characters in body
Source Link
RiaD
  • 47.8k
  • 12
  • 85
  • 128

There's a solution for both positive and negative x andbut only for positive y with just 1 division and without branches:

int ceil(int x, int y) { return x / y + (x % y > 0); } 

Note, if x is positive then division is towards zero, and we should add 1 if reminder is not zero.

If x is negative then division is towards zero, that's what we need, and we will not add anything because x % y is not positive

There's a solution for both positive and negative x and y with just 1 division and without branches:

int ceil(int x, int y) { return x / y + (x % y > 0); } 

Note, if x is positive then division is towards zero, and we should add 1 if reminder is not zero.

If x is negative then division is towards zero, that's what we need, and we will not add anything because x % y is not positive

There's a solution for both positive and negative x but only for positive y with just 1 division and without branches:

int ceil(int x, int y) { return x / y + (x % y > 0); } 

Note, if x is positive then division is towards zero, and we should add 1 if reminder is not zero.

If x is negative then division is towards zero, that's what we need, and we will not add anything because x % y is not positive

Source Link
RiaD
  • 47.8k
  • 12
  • 85
  • 128

There's a solution for both positive and negative x and y with just 1 division and without branches:

int ceil(int x, int y) { return x / y + (x % y > 0); } 

Note, if x is positive then division is towards zero, and we should add 1 if reminder is not zero.

If x is negative then division is towards zero, that's what we need, and we will not add anything because x % y is not positive