I am trying to figure out what this statement means and what the variable larg
contains:
int larg;
larg = ((larg % 8 != 0) ? (int) (Math.floor(larg / 8.0) + 1) * 8 : larg);
I am trying to figure out what this statement means and what the variable larg
contains:
int larg;
larg = ((larg % 8 != 0) ? (int) (Math.floor(larg / 8.0) + 1) * 8 : larg);
The part
(larg % 8 != 0)
asks if larg does not divide without a remainder by 8. If so
(int) (Math.floor(larg / 8.0) + 1) * 8
is executed which divides larg by 8, rounds down to discard the remainder and adds one, and then multiplies back by 8. This means find the next multiplum of 8 larger than larg
.
This is put in a ternary operator ...? ... : ...
which is an if-statement. So
larg =((larg % 8 != 0) ? (int) (Math.floor(larg / 8.0) + 1) * 8 : larg);
means: "If larg is not a multiple of 8, round up to the next multiple of 8, otherwise set it to itself". Another way to put it (as division of an integer by an integer discards the fraction)
if (larg % 8 != 0) {
larg = ((larg / 8) + 1) * 8;
}
This code was most likely written by a semi-experienced programmer, who preferred a one-liner instead of the three-line if
-statement. A more experienced programmer would know that readability is more important than keeping it on one line, so a future reader like you would understand it instead of having to ask here.