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