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
I had a strange error in a VB6 app this morning and it all stems from the fact that IsNumeric is not working as I expected. Can someone shed some light on why? To me this seems like a bug.
This code displays 4.15877E+62 in a message box:
Dim strMessage As String
strMessage = "0415877D57"
If IsNumeric(strMessage) Then
MsgBox CDbl(strMessage)
Else
MsgBox "not numeric"
End If
I am guessing that the runtime engine is incorrectly thinking that the D is in fact an E?
I think this is a bug though as the exact same code in VB.NET outputs not numeric
Is this a known issue with IsNumeric?
If you check the VB6 docs:
Note Floating-point values can be expressed as mmmEeee or mmmDeee, in which mmm is the mantissa and eee is the exponent (a power of 10). The highest positive value of a Single data type is 3.402823E+38, or 3.4 times 10 to the 38th power; the highest positive value of a Double data type is 1.79769313486232D+308, or about 1.8 times 10 to the 308th power. Using D to separate the mantissa and exponent in a numeric literal causes the value to be treated as a Double data type. Likewise, using E in the same fashion treats the value as a Single data type.
I've been using my own IsNumber function for a long time exactly because of this situation. IsNumeric can also return true for certain money symbols, like this: IsNumeric("$34.20").
My IsNumber function looks like this:
Public Function IsNumber(ByVal Data As String) As Boolean
If Data = "" Then
IsNumber = False
Exit Function
End If
IsNumber = IsNumeric(Data & "e0")
End Function
The idea here is... if there is already an e or d in the data, adding another will cause the data to NOT be numeric using the IsNumeric check. You can easily change this function to only allow for integers by replacing "e0" with ".0e0". Want just positive integers? then use this: IsNumeric("-" & Data & ".0e0")
The only downside of this method is that an empty string normally is not numeric, but when you append "e0" to it, it becomes numeric so you need to add a check for that, like I did in my code.
I suggest making a custom validator. Do you want to allow 0-9 only? What about negatives? Commas? I never cared for Microsoft's implementation, but I understand it.
I have a boolean formula such as (i_0 | i_1) ^ ((~i_0) & (~i_1)). And I want to check its validity using z3 python.
The main challenge that I'm facing is How to convert this string into a formula in z3 format?
Is there any easy way to do this?
To check validity of a formula, assert its negation and see if it's unsat. If the negation is unsatisfiable, that'd mean that your formula is valid for all assignments, i.e., it's a tautology.
Here's how you do that for your formula in z3py:
from z3 import *
i_0, i_1 = Bools('i_0 i_1')
s = Solver()
s.add(Not(Xor(Or(i_0, i_1), And(Not(i_0), Not(i_1)))))
print(s.check())
Note the outermost Not as we add the formula to the solver, to negate it. Inside the Not is a straightforward encoding of your formula, using And, Or, Xor, and Not functions.
When I run this, I get:
unsat
which means that your original formula is a tautology.
Note If your question is about how to take the input as a "string" and turn it into a z3Py formula automatically, then there's really no "out-of-the-box" way of doing that; as your string can have arbitrary conventions, operators, parentheses etc. But that is the typical parsing problem, i.e., converting text input to the function calls needed to run in z3py; and should be tagged as such.
I would like to ask something about data types in Lua.
I get from serial link some message (command:value) like this:
tmp_string = "BRAKE:1"
then I parse this string to command and value in two different functions (one is for command and other one is for value). This is function for parsing value
function parser(value)
index = string.find(value, ":")
result = value.sub(value, index+1)
return result
end
I would like to now what sort of data type result is? If I use string match it works.
...if string.match(state, "1") then...
However it also works when I do something like this
x = (state*65536)/3.2808)
I thought the result is string, but I don't understand why it works also with numerical operations. Thank you in advance.
Lua 5.3 Reference Manual, §3.4.1 - Arithmetic Operators
With the exception of exponentiation and float division, the arithmetic operators work as follows: If both operands are integers, the operation is performed over integers and the result is an integer. Otherwise, if both operands are numbers or strings that can be converted to numbers (see §3.4.3), then they are converted to floats, the operation is performed following the usual rules for floating-point arithmetic (usually the IEEE 754 standard), and the result is a float.
Emphasis is mine.
When dealing with operations, Lua will attempt to convert string operands to floats, and if it works - it works. If it fails, you get an error.
>| '55' / 2
<| 27.5
>| 'foo' / 2
<| error: [string "return 'foo' / 2"]:1: attempt to perform arithmetic on a string value
If you want to be explicit about this (and safe) use tonumber, and handle the nil-case.
If you need to know the type of a value in Lua, you can pass the variable to type and check the resulting string.
I have an array in MATLAB, and I wanted to display the positive symbol, "+" in front of positive elements, and keep the negative symbol, "-" in already existing negative values. I thought I could do the following:
I was thinking of constructing a sort of cell string or string array, and having an if, else system where if the numbers magnitude was >0, then I should store the value as '+' concatenated with the conversion of the element. If it was 0, just do a straight up char conversion since 0 has no sign, and if it was negative, just convert it. I know what to do, however, logistically, I think my order of commands is whacky.
How can I implement this?
I have the following script for an array x, but it just spews out values, I want an orderly string array I can copy and paste for use outside of MATLAB.
x;
pos = '+';
bound = length(x);
for i=1:bound
if(x(i)==0)
num2str(x(i))
end
if(x(i)>0)
num2str(x(i))
strcat(pos,num2str(x(i)))
end
if(x(i)<0)
num2str(x(i))
strcat(pos,num2str(x(i)))
end
end
I think you are searching for this.
Let's make an example.
First type in your command window :
test = 5;
Then:
sprintf('%+d',test)
You should have in this way what you want.
Of course you need to adapt it to your case. I suggest you to read this.
I hope it helps.
I am doing some classification and needed to convert an integer code to strings for that reason. I wrote something like this:
s(1).class = 1;
s(2).class = 7;
s(3).class = 9;
[s([find([s.class] == 1)]).class] = deal('c1'); %first conversion
[s([find([s.class] > 1)]).class] = deal('c2'); %second conversion
and was surprised to find s being a 1x4 struct array after the second conversion instead of the expected 1x3 struct array with the values.
Now, after some research, I understand that after the first conversion the value of s(1).class is 'c1' and the argument to find in the second conversion is not what I assumed it would be. The [s.class] statement actually returns something like the string 'c1\a\t' with ASCII escape sequences for bell and horizontal tab.
As the comparison does work (returning the matrix [1 1 1 1] and thus expanding my structure) I assume that matlab converts either the operand [s.class] or the operand 1.
Which is it? What actually is compared here numbers or characters?
And on the other hand is there a built in way to make > more restrictive, i. e. to require the operands to be of the same type and if not to throw an error?
When you do the comparison 'ab' > 1, the char array 'ab' gets converted to a double array, namely the ASCII codes of the characters. So 'ab' > 1 is equivalent to double('ab') > 1, which gives [1 1].
To get the behaviour you want (issue an error if one of the arguments is char) you could define a function:
function z = greaterthan(x,y)
if ischar(x) || ischar(y)
error('Invalid comparison: one of the input arguments is of type char')
else
z = x>y;
end
so that
>> greaterthan([0 1 2], 1)
ans =
0 0 1
>> greaterthan('ab', 1)
??? Error using ==> greaterthan at 3
Invalid comparison between char and int
Because you have not provided any expected output yet, I am going with the observations.
You are using a comprehension method (by invoking find) to determine which locations you will be populating for struct s with the results from your method deal (takes the argument c1 and c2). You have already set your type for s{whatever).class in the first snippet you provided. Which means it is number you are comparing, not character.
There is this isa function to see which class your variable belongs to. Use that to see what it is you are actually putting in (should say int32 for your case).