Operator¶
you can watch parallel the video for C/C++ (german): https://www.youtube.com/watch?v=5iL-onZ6M5U&list=PLEWVM-KBUSpmWSfyoFdD_hLWAY_9tTgi5&index=13
Be aware of differences in operator precedence between c/c++ javascript on the one side and python on the other side especially with regard to the bitwise operators and equality and comparison operators. https://stackoverflow.com/questions/8649738/cs-heritage-bitwise-operators-vs-equality-operators-precedence https://www.sololearn.com/en/Discuss/1823439/difference-of-operator-precedence-between-c-and-python
we will see that in detail in the next chapter about bitwise operators
import ROOT
import fortranmagic
%load_ext fortranmagic
import os
import sys
import numpy as np
if sys.platform.startswith("win"):
# Depends of system, python builds, and compilers compatibility.
# See below.
f_config = "--fcompiler=gnu95 --compiler=mingw32"
else:
# For Unix, compilers are usually more compatible.
f_config = ""
# Disable only deprecated NumPy API warning without disable any APIs.
f_config += " --extra '-DNPY_NO_DEPRECATED_API=0'"
%fortran_config {f_config}
%load_ext rpy2.ipython
The fortranmagic extension is already loaded. To reload it, use: %reload_ext fortranmagic New default arguments for %fortran: --extra '-DNPY_NO_DEPRECATED_API=0' The rpy2.ipython extension is already loaded. To reload it, use: %reload_ext rpy2.ipython
C++/C and Javascript¶
https://discuss.codechef.com/t/operator-precedence-table/14545
C++¶
| Level | Operators | Description | Associativity |
|---|---|---|---|
| 16 | :: |
Scope Resolution | - |
| 15 | ()[]->++ --static_cast, dynamic_cast etc |
Function Call Array Subscript Member Selectors Postfix Increment/Decrement Type Conversion |
Left to Right |
| 14 | ++ --+ -! ~(type)*&sizeofnew, delete |
Prefix Increment / Decrement Unary plus / minus Logical negation / bitwise complement C-style typecasting Dereferencing Address of Find size in bytes Dynamic Memory Allocation / Deallocation |
Right to Left |
| 13 | */% |
Multiplication Division Modulo |
Left to Right |
| 12 | + - |
Addition / Subtraction | Left to Right |
| 11 | >><< |
Bitwise Right Shift Bitwise Left Shift |
Left to Right |
| 10 | < <=> >= |
Relational Less Than / Less than Equal To Relational Greater / Greater than Equal To |
Left to Right |
| 9 | ==!= |
Equality Inequality |
Left to Right |
| 8 | & |
Bitwise AND | Left to Right |
| 7 | ^ |
Bitwise XOR | Left to Right |
| 6 | \| |
Bitwise OR | Left to Right |
| 5 | && |
Logical AND | Left to Right |
| 4 | \|\| |
Logical OR | Left to Right |
| 3 | ?: |
Conditional Operator | Right to Left |
| 2 | =+= -=*= /= %=&= ^= \|=<<= >>= |
Assignment Operators | Right to Left |
| 1 | , |
Comma Operator | Left to Right |
C¶
| Level | Operators | Description | Associativity |
|---|---|---|---|
| 15 | ()[]-> .++ -- |
Function Call Array Subscript Member Selectors Postfix Increment/Decrement |
Left to Right |
| 14 | ++ --+ -! ~(type)* &sizeof |
Prefix Increment / Decrement Unary plus / minus Logical negation / bitwise complement Casting Dereferencing Address of Find size in bytes |
Right to Left |
| 13 | * / % |
Multiplication Division Modulo |
Left to Right |
| 12 | + - |
Addition / Subtraction | Left to Right |
| 11 | >><< |
Bitwise Right Shift Bitwise Left Shift |
Left to Right |
| 10 | < <= > >= |
Relational Less Than / Less than Equal To Relational Greater / Greater than Equal To |
Left to Right |
| 9 | == != |
Equality Inequality |
Left to Right |
| 8 | & |
Bitwise AND | Left to Right |
| 7 | ^ |
Bitwise XOR | Left to Right |
| 6 | \| |
Bitwise OR | Left to Right |
| 5 | && |
Logical AND | Left to Right |
| 4 | \|\| |
Logical OR | Left to Right |
| 3 | ?: |
Conditional Operator | Right to Left |
| 2 | = += -= *= /= %= &= ^= \|=<<= >>= |
Assignment Operators | Right to Left |
| 1 | , |
Comma Operator | Left to Right |
javascript¶
| Level | Operators | Description | Associativity |
|---|---|---|---|
| 15 | ()[].new |
Function Call Array Subscript Object Property Access Memory Allocation |
Left to Right |
| 14 | +++ -!deletetypeofvoid |
Increment / Decrement Unary plus / minus Logical negation / bitwise complement Deallocation Find type of variable void |
Right to Left |
| 13 | */% |
Multiplication Division Modulo |
Left to Right |
| 12 | + - |
Addition / Subtraction | Left to Right |
| 11 | >><< |
Bitwise Right Shift Bitwise Left Shift |
Left to Right |
| 10 | < <=> >= |
Relational Less Than / Less than Equal To Relational Greater / Greater than Equal To |
Left to Right |
| 9 | ==!====!== |
Equality Inequality Identity Operator Non Identity Operator |
Left to Right |
| 8 | & |
Bitwise AND | Left to Right |
| 7 | ^ |
Bitwise XOR | Left to Right |
| 6 | \| |
Bitwise OR | Left to Right |
| 5 | && |
Logical AND | Left to Right |
| 4 | \|\| |
Logical OR | Left to Right |
| 3 | ?: |
Conditional Operator | Right to Left |
| 2 | =+= -=*= /= %=&= ^= \|=<<= >>= |
Assignment Operators | Right to Left |
| 1 | , |
Comma Operator | Left to Right |
%%cpp
cout <<3+4*5<<endl; //Precedence
23
%%cpp
int c=1;
++c;
cout <<c<<endl;
2
%%cpp
int c=1;
cout <<++c<<endl;
2
%%cpp
int c=1;
cout <<c++<<endl;
cout <<c<<endl;
1 2
%%cpp
int c=1;
cout <<--c<<endl;
cout <<c--<<endl;
cout <<c<<endl;
0 0 -1
%%cpp
int c=1;
c-=1;
cout <<c<<endl;
c+=1;
cout <<c<<endl;
c*=2;
cout <<c<<endl;
c/=2;
cout <<c<<endl;
c=11;
c%=10; //Modulo operation
cout <<c<<endl;
0 1 2 1 1
There is no power operator in c/c++. You need to use a function from cmath. Attention ^ is the binary xor operator.
%%cpp
#include <cmath>
cout <<pow(2,3);
8
Javascript has the power operator ** wich is equivalent to Math.pow(a,b) with the highest precedence right between 14 and 15 and is not mentioned in the above table.
%%js //the next line is only necessary in jupyter notebooks
element.setAttribute('style', 'white-space: pre;');console.log=function(text){element.textContent+=text+"\n"}
console.log(2**5)
console.log(2**(3**4))
console.log(2**3**4)
Python¶
Be aware of a sutle difference in the operator precedence of the Comparison operators as compared to c/c++ where the equality and unequality operator have less precedence. Also Python does allow for 1<2<3 which is evaluated as (1<2) And (2<3)
| Operators | Description | Associativity |
|---|---|---|
() Highest precedence |
Grouping / parentheses | Left - Right |
** |
Exponentiation | Right - Left |
+x, -x, ~x |
Unary plus, unary minus, bitwise NOT | Left - Right |
*, /, //, % |
Multiplication, division, floor division, modulo | Left - Right |
+, - |
Addition, subtraction | Left - Right |
<<, >> |
Bitwise left shift, bitwise right shift | Left - Right |
& |
Bitwise AND | Left - Right |
^ |
Bitwise XOR | Left - Right |
\| |
Bitwise OR | Left - Right |
Is, is not, in, not in,<, <=, >, >=, ==, != |
Identity, membership, and comparison operators | Left - Right |
Not x |
Logical NOT | Left - Right |
And |
Logical AND | Left - Right |
Or |
Logical OR | Left - Right |
If else |
Conditional expression | Left - Right |
Lambda |
Anonymous function definition | Left - Right |
=, +=, -=, *=, /= LowestPrecedence |
Assignment operators | Right - Left |
print(2**4)
16
Remark¶
Python behaves different with regard to the precedence comparison operator has the same precedence to equality or inequality operator:
print(1<2==1)
#should according to table be evaluated as
print((1<2)==1) #oops but that is not the case
#the comparison operators are actually a special case as the table above is actually wrong here
#because python allows for
print(1<2<3)
#and is to be read as
print(1<2 and 2<3)
#lets check
print(1<2 and 2==1) #yep
False True True True False
%%cpp
cout << (1<2==1) <<endl;
//this is evaluated as
cout << ( (1<2)==1) <<endl;
1 1
%%js //the next line is only necessary in jupyter notebooks
element.setAttribute('style', 'white-space: pre;');console.log=function(text){element.textContent+=text+"\n"}
console.log((1<2==1))
2**3**4==2**(3**4) # the ** operator is Right to left associative
True
R¶
| Operator | Description | Associativity |
|---|---|---|
^ |
Exponent | Right to Left |
-x, +x |
Unary minus, Unary plus | Left to Right |
%% |
Modulus | Left to Right |
*, /, %/% |
Multiplication, Division, Integer Division | Left to Right |
+, - |
Addition, Subtraction | Left to Right |
<, >, <=, >=, ==, != |
Comparisons | Left to Right |
! |
Logical NOT | Left to Right |
&, && |
Element-wise logical AND, short-circuit logical AND | Left to Right |
\|, \|\| |
Element-wise logical OR, short-circuit logical OR | Left to Right |
->, ->> |
Rightward assignment | Left to Right |
<-, <<- |
Leftward assignment | Right to Left |
= |
Leftward assignment | Right to Left |
Attention: Power and Remainder are different in Python and R. Also Precedence of Modulus operation is higher than multiplicative.
| Python | R | |
|---|---|---|
| Arithmetic Operators | ||
| Assignment : Defining a number | a = 10 ; b = 25 |
a <- 10 ; b <- 25 |
| Addition | a + b |
a + b |
| Subtraction | a - b |
a - b |
| Multiplication | a * b |
a * b |
| Division | a / b |
a / b |
| Power : a^b | a ** b |
a ^ b |
| Remainder (Modulus) | a % b |
a %% b |
| Integer Division | a // b |
a %/% b |
| Logical Operators | ||
| Short-Circuit logical AND | a and b |
a && b |
| Short-Circuit logical OR | a or b |
a \|\| b |
| Element-wise logical AND | a and b |
a & b |
| Element-wise logical OR | a or b |
a \| b |
| Logical NOT | !a |
!a |
| Relational Operators | ||
| Equal | a == b |
a == b |
| Less than | a < b |
a < b |
| Greater than | a > b |
a > b |
| Less than or equal | a <= b |
a <= b |
| Greater than or equal | a >= b |
a >= b |
| Not Equal | a != b |
a != b |
%%R
print(2**3**4==2**(3**4)) # the ** operator is Right to left associative
print(5*2%%6)
[1] TRUE [1] 10
print(5*2%6)
4
Fortran¶
| Category | Operator | Associativity |
|---|---|---|
| Logical NOT and negative sign | .not. (-) |
Left to right |
| Exponentiation | ** |
Left to right |
| Multiplicative | * / |
Left to right |
| Additive | + - |
Left to right |
| Relational | < <= > >= |
Left to right |
| Equality | == /= |
Left to right |
| Logical AND | .and. |
Left to right |
| Logical OR | .or. |
Left to right |
| Assignment | = |
Right to left |