Given a real number , one can find its positive part in multiple ways: by definition:
as a maximum or by using the absolute value: …
This is boring.
You know what else is boring? Spreadsheets.
Google Sheets, in particular. Let’s suppose we have a bunch of numbers in a spreadsheet (in a range like A1:B3), and want a single-cell formula (using arrayformula) that gets the positive parts of all of them.
1 -2 4 0 -1 3
The definition-based approach,
=arrayformula(if(A1:B3>0, A1:B3, 0)) does the job:
1 0 4 0 0 3
But it’s kind of redundant: the range has to be typed twice. This can be annoying if you are not just taking positive part once but computing something nested with them: . Each application of positive part would double the length of the formula.
The absolute value approach,
=arrayformula((A1:B3+abs(A1:B3))/2), has the same issue.
The maximum looks promising, because involves only once. Alas,
=arrayformula(max(A1:B3,0)) returns a single number (4 in this example), because
max just takes the maximum of all entries.
Is there a spreadsheet formula that returns positive part in array-compatible way, and uses the argument only once?
Yes, there is:
Negative arguments throw an error at the evaluation of the square root. This error is handled by
iferror wrapper, which returns the second argument in this case, namely 0. Otherwise, the value of the square root gets squared, bringing back the input.
This can be nested: for example, the formula
=arrayformula(1-iferror(sqrt(1-iferror(sqrt(A1:B3)^2, 0))^2, 0))
clamps the input values between 0 and 1, performing . Yes, it’s long — but if the input is itself a 100-character formula, this is still a simplification over the other approaches.