I have this code:
max(n+1,min(numPages+n-4,currentPage+n-2))
which is equivalent to the expression
$\max\{n+1, \min\{N+n-4, m+n-2\}\}$
$0<m<N, 0\le n\le5$
Is there any neat/weird trick to simplify it? Even only w.r.t. a computational standpoint?
I have this code:
max(n+1,min(numPages+n-4,currentPage+n-2))
which is equivalent to the expression
$\max\{n+1, \min\{N+n-4, m+n-2\}\}$
$0<m<N, 0\le n\le5$
Is there any neat/weird trick to simplify it? Even only w.r.t. a computational standpoint?
though it hardly matters to have 2-3 more or less additions(or any basic arithmetic operations, as at hardware level it's very fast, so definitely other components of a code make the bottleneck for efficiency, but as you have asked:
$\max\{n+1, \min\{N+n-4, m+n-2\}\}$
assuming all are positive numbers (as it shows, they are page numbers etc),
it is equivalent to
// by doing this you are avoiding 2 extra additions during evaluation.
Now in all cases you can write
note that you don't have to check whether $min\{N-5, m-3\}$ > 0 if you constrain N & m accordingly.
any other optimization I could think of, is subjective to the implementation and scenario you are dealing with.
So overall we have saved 1 addition and 1 comparison.