What does -- do in Excel formulas? - excel

Trying to decipher some Excel formulas and I see some stuff like SUMPRODUCT(--Left(...)...)
What is the -- doing? Naturally seems like decrementing to me but couldn't find any documentation on it.

The double-dash is known as a double unary operator.
Try this link: Why use -- in SUMPRODUCT formulae
Specifically:
SUMPRODUCT() ignores non-numeric entries. A comparison returns a boolean (TRUE/FALSE) value, which is non-numeric. XL automatically coerces boolean values to numeric values (1/0, respectively) in arithmetic operations (e.g., TRUE + 0 = 1).
The most efficient way to coerce the value is first to apply the unary minus operator, coercing TRUE/FALSE to -1/0, then applying it again to negate the value, e.g., +1/0.
A single unary operator (-) coerces true/false values into -1/0. By using the double unary operaor, we coerce the values again to 1/0.

The unary operator (-) is a shorthand method to convert a true/false statement into -1/0.
A single operator will convert -(true) into -1, so a double unary operator is used to convert that back into 1:
-(-(true)) = -(-(1)) = 1
-(-(false)) = -(-(0)) = 0

I've been using SUMPRODUCT for a while and have always used the * symbol instead of the --. I'm sure I asked the same question you've asked, but I can't remember the reason they gave me, but I was told that there wasn't really a need for --as sumproduct managed itself quite well without the it.
Anyway, =sumproduct(()*()*()*()) has always worked for me, and it's less confusing.

Boolean values TRUE and FALSE in excel are treated as 1 and 0, but we need to convert them. To convert them into numbers 1 or 0, do some mathematical operation. The Unary operator negates the boolean (math operation), hence, converts the boolean to number. Same works in TRUE * FALSE = 0

Related

Is TRUE equal to one (TRUE=1?) or greater than one (TRUE>1?) or even sometimes equal to zero (TRUE=0?)?

The title of this question seems weird, let me explain what I mean.
In B) below, it turns out that TRUE>1 because it returns TRUE; however, TRUE+0 returns 1, which means that TRUE=1. Thus, TRUE sometimes is greater than 1 and sometimes equal to 1.
In A) we can see that the same happens with FALSE, it may be FALSE>0 as in the first line, and FALSE=0 as in the second line.
Returning to the calculations, in C) we can see that TRUE=0 since it is not counted by the SUMPRODUCT function.
All this confuses me a great deal, and I constantly have to check if excel is calculating properly or not.
My conclusions so far:
in a simple comparison (+, -, =), TRUE>1 because its type is 4 (type of 0 is 1).
in a simple comparison (+, -, =), FALSE>0 because its type is 4 (type of 0 is 1).
TRUE and FALSE, preceded by a unary +/- remain logical values (TRUE>1 and FALSE>0)
when used in an arithmetic binary operation (+, -, ...) TRUE is coverted to 1 and FALSE is converted to 0 (TRUE=1 and FALSE=0)
for some functions, such as SUMPRODUCT in C), TRUE is not counted (TRUE=0)
FALSE (type 4) > ="" (type 2) > 0 (type 1); also, due to transitive property, FALSE (type 4) > 0 (type 1)
Please see the results in the picture below.
There is more to this subject, please have a look at the following picture:
an empty cell is equal to ="" (double quote), to zero (0) or even to FALSE
however, "double quote", zero and FALSE are not equal among them; it seems to me that the transitive property is not respected
I do not want to make this question too long.
I'd like to know if you agree with my conclusions and if you have other similar cases where the mathematical logic is in conflict with the results that excel yields

When "--" is not needed?

"--" converts TRUE/FALSE to 1/0. I have found that sometimes "--" is not needed . Math can be done directly on TRUE/FALSE. The following is an example:
=AGGREGATE(14,6,(LEFT(C5:C400,1)="T")*(LEN(C5:C400)=6)*RIGHT(C5:C400,5),1)
Both "(LEFT(C5:C400,1)="T")" and "(LEN(C5:C400)=6)" give an array of TRUE/FALSE. However, this array can be used directly in multiplication without first being converted to 1/0.
When it is not necessary to convert TRUE/FALSE to 1/0 and math would still work?
-- is the same as -1 * -1.
Any time Math is done against a string or boolean that can be converted to a number Excel will make the conversion to the number.
-- is just a shorter method when only one Boolean or number stored as a string is used.
VALUE() can be used in place, or + 0, or * 1 Any math will do it.
If multiple numbers stored as string or multiple Booleans are added or Multiplied or Subtracted or Divided, then the conversion is done and the -- is not needed.

MIN Function not ignoring FALSE

I have a formula inside a MIN function that evaluates to FALSE. MIN should ignore logical values, but it evaluates to 0 if the value is evaluated inside the MIN function:
=MIN(FALSE,2) >>> 0
But
A1=FALSE
=MIN(A1,2) >>> 2
Is this a bug in Excel? I'm using new latest version (190811) on a mac.
This is not a bug but by design:
From the MIN documentation:
Logical values and text representations of numbers that you type directly into the list of arguments are counted.
Or from this link:
The MIN function ignores TRUE and FALSE values, and numbers entered as text, unless they are typed directly into the list of arguments.
In other words, if an argument is or evaluates to TRUE or FALSE, it is not ignored.
There is most likely an alternative, but you would need to share more detail - what is the function that resolves to TRUE/FALSE - to propose an adequate solution.

If statement returning not equal to 0 when value equals 0

I have a variable, Diff as a double. (values are a sample from my Workbook)
Dim Diff As Double
Sheets(Sheet1).Activate
Diff = 4382.98-4117.34-265.64
Values for calculating Diff come from cells formatted as numbers.
I then use Diff as an argument in an if statement.
If Diff <> 0 Then
ActiveSheet.Range(A2).Value = 265.64 + Diff
End If
Diff should equal 0, but the if statement is proceeding as if the condition is true. I have similar if statements that do not have problems like this. Do I need to format my values differently?
Edit: Had 43892.98 instead of 4382.98
Edit2: Had 256.64 instead of 265.64
Never, NEVER, NE-VER compare doubles on equity! (vars of floating point type)
Let even just in the mind only - never...
And you do right - your compare is "<>" - and you got the right result.
Currency is INTEGER type, i.e. not-floating point var.
values, exceeded 4 digits after point, are rounding internally;
compare on equity is correct.
.
I stored Diff As Currency instead of Double and the issue cleared up.

Excel matching IF statement not evaluating correctly

I have two columns in the following structure
A B
1 49 4922039670
I have been able to evaluate =LEN(A1) as 2, =LEFT(B1,2) as 49, and =LEFT(B1,LEN(A1)) as 49. However when I try to evaluate =IF(LEFT(B1,LEN(A1))=A1,TRUE,FALSE) it evaluates FALSE. I have no idea why. I have tried to change both columns A and B to different matching formats but it still just evaluates FALSE.
Not entirely sure what Im doing wrong.
maybe it is depending on the Version but in my Excel it you cast implicit your number 4922... to a string and 49 <> '49'
Either you make both strings or both numbers, f.e.:
=IF(LEFT(B1,LEN(A1))=""&A1,TRUE,FALSE)
""&<a number> concates an empty string to a number, the result will be a string
you could use function "TEXT" or - if you are sure, both are numbers - use 'VALUE'
=IF(VALUE(LEFT(B1,LEN(A1)))=A1,TRUE,FALSE)
to make a text should be more stable
Comparing a string (what LEFT gives you) with an integer is going to be FALSE
=LEFT(B1,LEN(A1))=49 //FALSE
You want
=LEFT(B1,LEN(A1))="49" //TRUE
So:
=LEFT(B1,LEN(A1))=TEXT(A1, "0")
Or:
=EXACT(LEFT(B1,LEN(A1)),A1)

Resources