Questions about operators that test the relationship between two objects/variables/entities. These can apply to operators in any language.
Questions tagged [relational-operators]
66 questions
1765
votes
16 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.

Vinícius
- 15,498
- 3
- 29
- 53
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
4
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,555
- 2
- 16
- 23
3
votes
4 answers
Comparing unsigned integer with negative literals
I have this simple C program.
#include
#include
#include
bool foo (unsigned int a) {
return (a > -2L);
}
bool bar (unsigned long a) {
return (a > -2L);
}
int main() {
printf("foo returned = %d\n",…

Sourav Ganguly
- 329
- 2
- 14
3
votes
4 answers
How const char* strings are compared?
Firstly, consider this example:
#include
using namespace std;
int main()
{
cout << ("123" == "123");
}
What do I expect: since "123" is a const char*, I expect ADDRESSES (like one of these answers said) of these strings to be…

Learpcs
- 282
- 3
- 10
3
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
- 343
- 3
- 10
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,069
- 15
- 27
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
- 41,637
- 14
- 86
- 162
2
votes
2 answers
comparison between signed and unsigned char
I'm pretty much assuming this is a stupid question... but I can't really find the answer for it. So I'm asking this here.
For the purpose of learning about implicit type casting, I'm running the following code on C.
#include
int main()
{
…

asdfqwerzxcv215
- 35
- 5
2
votes
1 answer
Allow nullable operands in less- or greater-than comparisons in TypeScript
In JavaScript, null operands in a relational expression are treated as 0:
function f() { return /* either a number or null */; }
let b = f() < 0; // false if f() returns null
But in TypeScript, if I provide a type annotation for f and turn on…

Michael Liu
- 52,147
- 13
- 117
- 150
2
votes
2 answers
c++ too few parameters in my overloaded operator ==
I am trying to compare two arrays by making an overloaded operator ==.
My code looks something like this:
//myArray.h
class myArray {
int size, start, end;
int *data;
public:
myArray(int sz);
myArray(int lower, int upper);
int…

NetElvis
- 15
- 1
- 2
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
- 37,899
- 23
- 129
- 288
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
…

user8822312
- 255
- 4
- 14
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,723
- 3
- 18
- 30
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