I've been testing this for about an hour and I don't understand what's going on.
In imageJ, if I say:
i = 3.5;
print(round(i));
I get 4
.
However, If I say:
print(65535/(27037-4777)*(26295-4777));
print(round(65535/(27037-4777)*(26295-4777)));
For some reason, I am getting:
63350.5
63350
Shouldnt it be rounding up to 63351?
Taking a look at your comments, the number that was generated through that calculation is actually 63350.499999...
, and so when you try and round, the number gets rounded down and you get 63350
. One thing that I can suggest is to add a small constant that may seem innocuous in hindsight, but it will resolve situations like this. You want to make it small enough so that it'll push the fractional part of your number over to the 0.5
range so it'll round successfully, but it won't interfere how round
works for the other fractional parts.
The Java API has a function called Math.ulp
that will compute the next possible fractional component that is after a particular floating point number that you specify. However, because ImageJ
doesn't have this functionality, consider adding something small like 1e-5
. This may seem like a foolish hack, but this will certainly avoid the situation like what you're experiencing now. This constant that you're adding should also not affect how round
works in ImageJ
.
tl;dr
: Add a small constant to your number (like 1e-5
) then round. This shouldn't affect how round
works overall, and it'll simply push those numbers with a fractional component that are hovering at the 0.5
mark to be truly over 0.5
.
Good luck!