I understand that Round
give the nearest even integer for cases where the number is between two integers, i.e. Round[2.5] = 2
and Round[3.5] = 4
(see this question). This carries over to rounding to non-integers that are not orders of magnitude as well:
Round[1.3, 0.2] = 1.2
Round[1.5, 0.2] = 1.6
Round[Range[1, 2, 0.1], 0.2] = {1., 1.2, 1.2, 1.2, 1.4, 1.6, 1.6, 1.6, 1.8, 1.8, 2.}
However, the Floor
function seems to behave oddly in a similar situation:
Floor[Range[1, 2, 0.1], 0.2] = {1., 1., 1., 1.2, 1.2, 1.4, 1.6, 1.6, 1.8, 1.8, 2.}
but Ceiling
does what I would expect it to do:
Ceiling[Range[1, 2, 0.1], 0.2] = {1., 1.2, 1.2, 1.4, 1.4, 1.6, 1.6, 1.8, 1.8, 2., 2.}
My question therefore is why does Floor[1.2, 0.2] = 1.
? I understand the behavior of Round
for this test set, but I do not understand Floor
. Then, with Floor
giving something I don't expect, I am very surprised that Ceiling
gives exactly what I would expect.
Background for my Problem
I have a regular array of 2D data ({x, y, probability}
) that I want to bin by summing, so Round
doesn't work because different numbers of elements are included in consecutive bins because it rounds to evens, but I expected Floor
to work.
gathered = GatherBy[predictedProbabilityDist,
{ Round[#[[1]], newGridSpacing],
Round[#[[2]], newGridSpacing] } &];
versus
gathered = GatherBy[predictedProbabilityDist,
{ Floor[#[[1]], newGridSpacing],
Floor[#[[2]], newGridSpacing] } &];
or the nearly equivalent expression with Ceiling
, followed by something like
predictedProbDist = Map[
{ Min[#[[All, 1]]],
Min[#[[All, 2]]],
Total[#[[All, 3]]] } &,
gathered];
Based on the testing with the simple Range
above, it looks like I need to use Ceiling
, but I don't know why for sure that is the case.