Quick alternatives for floor and ceiling for a positive system. Double values

double d = 0; // random decimal value with it integral part within the range of Int32 and always positive. int floored = (int) Math.Floor(d); // Less than or equal to. int ceiled = (int) Math.Ceiling(d); // Greater than or equal to. int lessThan = ? // Less than. int moreThan = ? // Greater than. 

The Floor and Ceiling functions include the largest / smallest integer that is less than / greater than or equal to d respectively. I want to find out the largest / smallest integer that is less / greater , but NOT equal to d respectively.

Of course, this can be achieved with a few if and but's , but I'm looking for a method that either does not include branching, or at least very quickly, since this operation will be performed billions of times in the algorithm.

Is binary manipulation possible? If not, what would be the best alternative?

The obvious solution would be something like:

 int lessThan = (d - floored) > double.Epsilon ? floored : (floored-1); int moreThan = (ceiled - d) > double.Epsilon ? ceiled : (ceiled+1); 

NOTE The challenge is to find out if d closer to lessThan or moreThan .

+4
source share
5 answers

Since d always positive, you can use this cast for whole truncations (i.e., this is the floor for positive input and the ceiling for negative input).

floor(d + 1) same as ceil(d) + 1 if integer, ceil(d) otherwise , and ceil(d - 1) same as floor(d) - 1 if integer, floor(d) otherwise

 int moreThan = (int)(d + 1); // floor(d + 1) int lessThan = int.MaxValue + (int)((d - int.MaxValue) - 1) // ceil(d - 1) 

lessThan little confusing, I won’t be surprised if anyone else thinks better.

But since you want this:

The goal is to find out if d comes closer to lessThan or moreThan

This should be even simpler:

 double x = d % 1; if (x == 0 || x == 0.5) // d is equally far from either one, either by a difference of 1 or of 0.5 else if (x < 0.5) // d is closer to lessThan else // d is closer to moreThan 
+3
source

I can skip the question, but find the nearest integer value of d:

 int floored = (int)d; int ceiled = (int)(d + 1); int mathRounded = (int)(d + 0.5) 
0
source

I do not know what results you want to achieve at the end.

If you want ceil , floor and what is the closest :

 int lessThan = (int) Math.Floor(d); // Less than or equal to. int moreThan = (int) Math.Ceiling(d); // Greater than or equal to. // 1 comparison instead of 2 that is made on your question. if (lessThan == moreThan) { lessThan--; moreThan++; } bool isCloserToFloor = (d - .5) < lessThan; bool isCloserToCeil = !isCloserToFloor; 
0
source

We did not transfer this testing, but you can check if your double is already integer, and then perform the corresponding operation:

 double d = 0; int lessThan; int moreThan; if (d % 1 == 0) { lessThan = d - 1; moreThan = d + 1; } else { lessThan = (int) Math.Floor(d); moreThan = (int) Math.Ceiling(d); } 
0
source

You can cheat a little: use Convert.ToInt32 (bool):

 double d = 0; // random decimal value with it integral part within the range of Int32 and always positive. int floored = (int) Math.Floor(d); // Less than or equal to. int ceiled = (int) Math.Ceiling(d); // Greater than or equal to. int lessThan = floored - Convert.ToInt32(Math.Abs(d-floored) < epsilon); int moreThan = ceiled + Convert.ToInt32(Math.Abs(d-ceiled) < epsilon); 
0
source

All Articles