unary-operator

Using precedence in Bison for unary minus doesn't solve shift/reduce conflict

断了今生、忘了曾经 提交于 2019-12-21 06:00:45
问题 I'm devising a very simple grammar, where I use the unary minus operand. However, I get a shift/reduce conflict. In the Bison manual, and everywhere else I look, it says that I should define a new token and give it higher precedence than the binary minus operand, and then use "%prec TOKEN" in the rule. I've done that, but I still get the warning. Why? I'm using bison (GNU Bison) 2.4.1. The grammar is shown below: %{ #include <string> extern "C" int yylex(void); %} %union { std::string token;

What does a plus sign do in front of a variable in Python?

本秂侑毒 提交于 2019-12-19 12:29:21
问题 There's the following bit of Python code in a project I have to maintain: # If the `factor` decimal is given, compute new price and a delta factor = +factor.quantize(TWOPLACES) new_price = +Decimal(old_price * factor).quantize(TWOPLACES) delta = new_price - old_price The question here is the purpose of + in front of a variable. Python docs call it unary plus operator , which “yields its numeric argument unchanged”. Can it be safely removed then? (Incidentally, the code was written by me some

What does a plus sign do in front of a variable in Python?

怎甘沉沦 提交于 2019-12-19 12:29:00
问题 There's the following bit of Python code in a project I have to maintain: # If the `factor` decimal is given, compute new price and a delta factor = +factor.quantize(TWOPLACES) new_price = +Decimal(old_price * factor).quantize(TWOPLACES) delta = new_price - old_price The question here is the purpose of + in front of a variable. Python docs call it unary plus operator , which “yields its numeric argument unchanged”. Can it be safely removed then? (Incidentally, the code was written by me some

encounter “unary operator expected” in bash script

纵然是瞬间 提交于 2019-12-17 18:38:38
问题 in my bash script, I have a function to return 0 or 1(true or false) for the later main function's condition. function1 () { if [[ "${1}" =~ "^ ...some regexp... $" ]] ; then return 1 else return 0 fi } then in my main function: main () { for arg in ${@} ; do if [ function1 ${arg} ] ; then ... elif [ ... ] ; then ... fi done } however, when i ran this script it always gave me an error msg "[: function1: unary operator expected" can anyone help me please? 回答1: You are making the common mistake

How to printf a memory address in C

大憨熊 提交于 2019-12-17 07:39:45
问题 My code is: #include <stdio.h> #include <string.h> void main() { char string[10]; int A = -73; unsigned int B = 31337; strcpy(string, "sample"); // printing with different formats printf("[A] Dec: %d, Hex: %x, Unsigned: %u\n", A,A,A); printf("[B] Dec: %d, Hex: %x, Unsigned: %u\n", B,B,B); printf("[field width on B] 3: '%3u', 10: '%10u', '%08u'\n", B,B,B); // Example of unary address operator (dereferencing) and a %x // format string printf("variable A is at address: %08x\n", &A); I am using

Unary Operator-() on zero values - c++

此生再无相见时 提交于 2019-12-13 13:27:15
问题 I wrote this code to overload the unary operator- on a matrix class: const RegMatrix RegMatrix::operator-()const{ RegMatrix result(numRow,numCol); int i,j; for(i=0;i<numRow;++i) for(j=0;j<numCol;++j){ result.setElement(i,j,(-_matrix[i][j])); } return result; } When i ran my program with debugger in visual studio, it showed me that when the operation is done on a double equals zero, it inserts the result matrix the number -0.00000. Is it some weird VS-display feature, or is it something i

combination of unary minus and float conversion

随声附和 提交于 2019-12-12 09:36:10
问题 Consider the following C statements: unsigned long x = 1; float a = -x; double b = -x; I would expect the unary minus term to yield an unsigned long value equal to ULONG_MAX and a and b to be set to single and double precision representations of ULONG_MAX, respectively. This is the result I obtain with gcc 4.4.7 on 32-bit Linux and with the Intel and PGI compilers on 64-bit Linux. With gcc (tested versions 4.4.7, 4.7.2 and 4.8.0, both with -O0 and -O2) on 64-bit Linux, however, the double

How post Increment ++ Operator works while initialization

穿精又带淫゛_ 提交于 2019-12-12 05:14:47
问题 I have written following code to reverse the given String: String str = "abcdef"; char[] strToChar = str.toCharArray(); char[] outString = new char[strToChar.length]; for(int j = 0; j < strToChar.length; ) { int len = strToChar.length-j; outString[j++] = strToChar[strToChar.length-j]; } As per my understanding initialization happens from Right to Left . Hence, strToChar[strToChar.length-j] here should throw ArrayIndexOutOfBoundException . But it runs fine. How is that happening? Shouldn't it

C# bitwise AND operator '&' logic

大城市里の小女人 提交于 2019-12-12 02:56:38
问题 I'm finding difficult to understand how the ´&´ operator works in relation to this code: for(int cnt = 0; cnt < (1 << somelist.Count()); ++cnt) { for (int i = 0; i < somelist.Count(); ++i) { // 1 << 0 -- 1 -> 1 // 1 << 1 -- 10 -> 2 // 1 << 2 -- 100 -> 4 // 1 << 3 -- 1000 -> 8 // 1 << 4 -- 10000 -> 16 // 1 << 5 -- 100000 -> 32 var l = (1 << i); if ((cnt & l) == 0) { // when is it getting here? // some code to execute } } } which ones are the cases when it enters the if condition and those

Unary Operations fused with assignment

余生长醉 提交于 2019-12-10 20:46:43
问题 Doubtful result in the following code: public static void main (String[] args) { int i = 2; i = i+=2 + i++; System.out.println(i); } Was expecting 8 as output, as 'i+=2' should update i, but its not behaving so. Output: 6 I infer that the short-hand assignment operator is returning 4 as expected but not updating the same in variable i. Any explanation will be appreciated. 回答1: i++ is a postfix increment - it increments i, then essentially returns the old value of i. The equivalent prefix