Questions about operators that test the relationship between two objects/variables/entities. These can apply to operators in any language.
Questions tagged [relational-operators]
42 questions
1645
votes
14 answers
Is < faster than <=?
Is if (a < 901) faster than if (a <= 900)?
Not exactly as in this simple example, but there are slight performance changes on loop complex code. I suppose this has to do something with generated machine code in case it's even true.
snoopy
- 14,122
- 3
- 22
- 49
18
votes
3 answers
What is the difference between directly assigning the result of left shift operation to a variable and the left shift assignment operation in C?
In the following expression, the result of the left shift operation is assigned to the variable i.
int i;
i = 7 << 32;
printf("i = %d\n",i);
In the following expression, the left shift assignment operation is carried.
int x = 7;
x <<= 32;
printf("x…
user5155804
3
votes
1 answer
is it defined behaviour to add the result of logical operation
Is it okay (defined behavior) to add up the result of logical operations (as they should just be 0 or 1)?
Can I do something like this if I want to count the numbers bigger than zero?(or is there a better way?)
int a[3] = {1,-5,3};
int result =…
Kami Kaze
- 2,051
- 11
- 25
3
votes
2 answers
Proper way of overloading binary relational operators in C++
What is the proper/canonical way of overloading binary relational operators in C++?
Is it better to use member functions, or friend free functions?
E.g.:
class X {
public:
...
// Use member function overloads
bool operator==(const X& rhs)…
Mr.C64
- 37,988
- 11
- 76
- 141
2
votes
2 answers
Scoped Enums (enum class) relational operators
I've looked all over the place and I can't believe this question has not been asked before.
Is the ordering of scoped enumerators defined by the standard?
Say if I have the following
#include
enum class Fruits {Apple, Orange,…
GamefanA
- 1,346
- 2
- 13
- 22
2
votes
2 answers
Can I Write Relational Operators in Terms of Arithmetic Operations?
So I have a fairly complex function:
template
void foo(const int param1, const int param2, int& out_param)
Given int bar, const int arg1, and const int arg2 the function will be called with either: foo>(arg1, arg2, bar) or…
Jonathan Mee
- 35,107
- 16
- 95
- 241
2
votes
1 answer
Matlab compare two matrixes with different dimension
I see people take ==, ~=, >, < between matrixes with a different dimension in parentheses following a matrix to get its entries, like this:
b =
1 4 7
2 5 8
3 6 9
>> b == [1 2 3]
ans =
3×3 logical array
…
Yongliang He
- 213
- 3
- 12
2
votes
1 answer
Named Numeric Vector (in ascending order) to Named Logical Vector based on condition
I have a named numeric vector vec, then it was sorted in ascending order and saved in object vec_sort, as shown below.
vec <- c(1,1,1,2,3,1,5)
names(vec) <- letters[1:7]
vec
# a b c d e f g
# 1 1 1 2 3 1 5
str(vec)
# Named num [1:7] 1 1 1 2 3 1…
Sowmya S. Manian
- 3,321
- 2
- 14
- 25
2
votes
2 answers
How does Ruby compare semantic version strings?
I noticed some unexpected behavior when comparing Ruby strings. Which I will write below:
2.3.1 :011 > '5.6' >= '5.5'
=> true
2.3.1 :012 > '5.6' >= '5.7'
=> false
2.3.1 :013 > '5.6' >= '5.6.1'
=> false
2.3.1 :014 > '5.6' <= '5.6.1'
…
Thomas Deranek
- 193
- 1
- 9
2
votes
2 answers
Unexpected output in C program
I run the following C program
#include
int main() {
int x = 5, y = 6, z = 3, i;
i = y > x > z;
printf("%d\n", i);
}
and get the output as 0.
Again, when I run
#include
int main() {
int x = 5, y = 6, z = 3,…
Preetam Datta
- 41
- 3
2
votes
3 answers
c: What does this line do?
I read some code and came over this rather cryptic syntax:
size_t count = 1;
char *s = "hello you";
char *last_word = "there";
count += last_word < (s + strlen(s) - 1); #line of interest
Count is incremented, somehow. But I thought the <…
Koen
- 120
- 1
- 10
1
vote
2 answers
Understanding the operator "less" or "greater" in assigning value with C++
I used greater than and less than signs and it gives ouput! How it is working ?
int x = 2;
x >= 3;
cout << x; // output is 2
And also the output is different like this
int x = 2;
x = x > 3;
cout << x; // output is zero !! HOW ??
Sajawal Bashir
- 11
- 1
1
vote
1 answer
How to use if statement in R?
I am habituated to Python where the following code works without any exceptions. However, I am getting the below error in R when I try to run the command.
a <- readline(prompt="Give a num between 1-10: ")
b <- readline(prompt="Give another between…
Vivek
- 296
- 3
- 12
1
vote
1 answer
What exactly the "LeftFirst" Boolean Flag is in "Abstract Relational Comparison Algorithm" in ECMAScript?
Can someone explain what exactly the LeftFirst Boolean Flag is in Abstract Relational Comparison Algorithm in ECMAScript? I know that there is only one operator < handling all other relational operators like >, >=, <= as mentioned in the ECMAScript…
user13104441
- 69
- 7
1
vote
2 answers
Can someone explain me what is the "LeftFirst" Boolean flag they have defined in the ecmaScript specification
Can someone explain to me what is the LeftFirst Boolean flag? when reading the EcmaScript specification about [relational-operators](https://tc39.es/ecma262/#sec-relational-operators" relational-operators definition in ECMAScript") and Abstract…
Kevin
- 179
- 10