780 questions
21
votes
1
answer
655
views
Default equality operator for an empty union
C++ allows us to make union class type without any data members. (cppreference even contain an example with it.)
Can one request the compiler to provide default implementation of equality comparison ...
3
votes
1
answer
112
views
When comparing a value and a reference, should I dereference the reference or create a reference to the value?
I'm trying to learn Rust and I managed to find two ways to solve a question (which was to convert from celsius to fahrenheit).
fn convert_only_if_needed(celsius_temp : &f64) -> f64 {
if !(*...
3
votes
1
answer
177
views
Overloading comparison operators for simple POD `struct`s holding many numerical fields
Say I have got a plain old data (POD) struct containing multiple numeric fields.
I want to overload the comparison operators so that I can compare two instances of my POD.
The problem is that I need ...
4
votes
1
answer
191
views
Comparison operator with explicit object parameter of not class type
Can comparison operators for a class in C++23 have explicit object parameter of a type distinct from the class type?
Consider for example
struct A {
int i;
constexpr bool operator==(this int x,...
4
votes
1
answer
154
views
Redeclaration of explicitly defaulted comparison operator makes it undefined
In the following program, struct A has default friend equality comparison operator, which is redeclared again to get the pointer of the function (&operator==):
struct A {
friend constexpr bool ...
2
votes
1
answer
210
views
(x != x) for default equality operator==
This program
struct A {
int i = 0;
constexpr operator int() const { return i; }
constexpr operator int&() { return ++i; }
};
struct B {
A a;
bool operator==(const B &) ...
2
votes
1
answer
127
views
Defaulting comparison operator with explicit object parameter
In C++23 a class can have explicit object member functions (with the first parameter prefixed by this), including member comparison operators. Which of them a compiler can generate automatically after ...
-5
votes
1
answer
84
views
Comparison Operators result in Python
Why in Python print(bool(7>8)==False==True) results False, logically it should be True?
as bool(7>8) is False, therefore
False==False==True -> True==True -> True (NOT False)
as the ...
3
votes
2
answers
102
views
Impact of comparison function on upper_bound and lower_bound
I would like to understand the impact of the comparison function (< or <=) on both lower_bound and upper_bound functions.
Consider this program:
#include <iostream>
#include <vector>
...
0
votes
1
answer
439
views
Overloaded functions have similar conversions [duplicate]
My program will not compile, and give the error overloaded functions have similar conversions. What does this mean and how do I fix it?
struct DynamicInt {
bool operator==(const DynamicInt&);
}...
18
votes
1
answer
2k
views
C++20 std::vector comparison weird behaviour
Overloading the operator bool() for a custom class T breaks std::vector<T> comparison operators.
The following code tried on the first online compiler google suggest me prints
v1 > v2: 0
v1 &...
0
votes
0
answers
26
views
x86 - compare numbers and push the result onto the stack
I would like to know, how to compare two numbers and push the result (true, false or 1, 0) onto the stack. I know about the cmp instruction, but it only sets the flags. I need something that will ...
1
vote
0
answers
44
views
Comparing numeric values not as expected (VBScript) [duplicate]
I stumbled upon a strange behavior of VBScript when comparing numerical values. I expect it's a rounding issue but I can't figure out how I can improve the code to behave as expected.
Dim angle
angle ...
0
votes
3
answers
70
views
Python comparison operator not giving proper answer in if-else statements
# Program1: Take two numbers as input from User and print which one is greater or are they equal.
num1 = input("Enter the first number: ")
num2 = input("Enter the second number: ")...
3
votes
2
answers
329
views
Do tuples conform to Comparable?
Tuples in Swift seem to conform to Comparable inasmuch as I get these results:
print ( (3,0) < (2,10000) ) // false
print ( (0,0,5) < (0,0,7) ) // true
It seems to do a left-right member-by-...
0
votes
2
answers
194
views
Is grouping values with ( value1 || value2) for comparisons valid in JavaScript or TypeScript and if so, why not?
I am wondering if grouping values with || while comparing a single variable value is considered valid in the latest JavaScript or TypeScript and if not, does anyone know a reason for this to not ...
1
vote
0
answers
27
views
Chained comparisons not always executing functional calls (side-effects) [duplicate]
I was testing out comparison chaining and I noticed that in certain patterns the some of the function calls are not executed.
If I plan on using the function calls to do things what would be the best ...
-3
votes
2
answers
106
views
Why is the expected output not displayed [duplicate]
why there is no output for this?
#include <stdio.h>
int main() {
int i;
int array[4] = {10, 25, 36, 42};
// int size=sizeof(array) / sizeof(int);
for (i = -1; i < sizeof(...
-1
votes
2
answers
172
views
Why is "-1 < strlen(s)" equal to 0?
char* s =(char*) "sss";
int j = -1;
printf("%d", j < strlen(s));
It's mainly about the difference between j < 3 and j < strlen(s).
In the above code, the printf() ...
1
vote
0
answers
34
views
If statement checking for class inputs [duplicate]
I'm currently still learning, but am working on a small and simple script.
I'm attempting to check the user inputs to ensure they match the desired values of the defined variables. The script works ...
-2
votes
1
answer
72
views
How compare if a variable (x) is greater than another variable (y) by a certain value. For example, if x > y by 2, print("Hooray!")
So I'm trying to make a rock, paper, scissors game and I'm trying to make it so that if the user (or computer) either gets 3 points OR the user's (or computer's) score is greater by 2 (like a best 2 ...
2
votes
1
answer
103
views
Different result from std::vector erase when comparing in if than comparing to stored value
For some reason, I get different result when comparing the return value from vector::erase inside a if statement or if I store the value first and then compare. But it seems to only happen with g++.
...
5
votes
1
answer
177
views
Why does pair's comparison operator prefers conversion over user-provided operator<?
After switching to C++20 I found that some of our tests failed.
The output of the following code is different between C++17 and C++20 modes:
https://godbolt.org/z/hx4a98T13
class MyString
{
public:
...
4
votes
2
answers
303
views
What is mathematically unsound about a >= b < c chained comparison?
Herb Sutter implemented only a subset of chained comparisons in his cppfront, stating in his keynote (https://youtu.be/fJvPBHErF2U?si=RqcR661yBzcQ8r5_&t=850) that a >= b < c is "...
3
votes
1
answer
126
views
Can friend comparison operator be defined for a local class?
Since C++20 the compiler can generate default comparison operators for a class, including as friend non-member function, see (2) in cppreference.com.
I came across the code working in MSVC that does ...
0
votes
0
answers
24
views
Mysql: How to compare value in the same column between current day and last 7 day, is the value in current day is increase or degrade in percentage .?
I have table mysql with column date, time, subject, description and value. I want to know the value at current day is increase or degrade from the value at last 7 day in the same time.
Date ...
0
votes
1
answer
45
views
Comparison in c incorrect for integers other than 2? [closed]
I am writing a function to determine the prime factors of a number and return them in a doubly linked list. The code is as below.
(create_list, add_to_tail, and print_DLL perform operations on the DLL ...
-2
votes
1
answer
159
views
Why compare operator <,>,= given wrong output when comparing strings? [duplicate]
I am comparing two string using comparison operators(<,>,=).
The output of "a" < "b" In this case is 0.
#include<iostream>
using namespace std;
int main()
{
cout&...
-3
votes
3
answers
766
views
What's the purpose of & in Java?
So, I was following a pacman tutorial and the coder uses & not &&. He doesn't explain what it does though. I searched it up and it says that unlike &&, & checks both ...
36
votes
2
answers
6k
views
The presence of both operator == and operator != breaks some concepts
After upgrading to latest Visual Studio 2022 version 17.6, one of our custom views stopped to be recognized as std::ranges::range. It turned out that the problem was in the presence of both operator ==...
0
votes
1
answer
188
views
What do the comparison operators in the body of a for loop do?
I've entered my programming class' exam where i've encountered a question in which i was asked to interpret what would the result of the code block written in C would be and the code block is as ...
0
votes
1
answer
53
views
Logical comparison returning different result for same values
I am absolutely lost as to what is happening here. I am simply comparing the values of two numbers and returning the result.
When I'm doing it in a custom property, the result is always False, even ...
4
votes
2
answers
2k
views
Why do IEEE 754 floating-point numbers use a sign bit of 1 for negative numbers?
The typical reason given for using a biased exponent (also known as offset binary) in floating-point numbers is that it makes comparisons easier.
By arranging the fields such that the sign bit takes ...
1
vote
1
answer
191
views
Is it possible for byte arrays, returned from BufferedStream.Read(), to have different lengths?
We have this old code in our repo:
bool BufferByteCompareFiles(string filePath1, string filePath2)
{
int bufferCapacity = 0x400000;
var firstFile = File.OpenRead(filePath1);
...
2
votes
2
answers
955
views
How to lexicographically compare two vectors in reverse order?
If I want to compare two vectors in lexicographical order, I can do as follows:
int main() {
std::vector<int> a{0, 7, 8, 9};
std::vector<int> b{1, 2, 3, 4};
std::cout << ...
-1
votes
2
answers
316
views
Why 3>2>1 returns True whereas (3>2)>1 returns False in Python? [duplicate]
I was not able to find proper documentation about it. Same thing I did in Javascript and I got False for Both Cases. I know that 3>2 should be treated as (3>2) -> True|1, therefore 3>2>...
0
votes
1
answer
750
views
Since When Can We Compare Strings with == in Java? [duplicate]
I recently went back to the String comparisons in Java and tried this:
String s = "hello";
System.out.println(s == "hello");
System.out.println(s.equals("hello"));
When ...
1
vote
1
answer
84
views
A Mandelbrot Comparison Logic issue that refuses to be resolved
I want to see the implementation of the Mandelbrot Set working (script provided by BruXy.regnet.cz), but one line of code is giving me problems to resolve.
In the While-loop, a logic AND comparison is ...
-1
votes
1
answer
160
views
C double comparison not working as intended: -0 not equal to 0 [duplicate]
I have a program in which I am plotting a function in the console, and I am iterating through a value of y which is a float. However, when this value is equal to zero, I would like to draw the axis. ...
1
vote
2
answers
59
views
String[] Array variable comparison
I am writing a code that vaguely simulates a poker game, which at this point should output a "Players Hand", an "Opponents Hand" and a "Flop".
Each card should only ...
4
votes
2
answers
189
views
Should pmr containers with different allocators compare equal?
Say I have a container that uses a hardcoded pmr allocator to store data. What is the consensus among the C++ community: should operator==() compare equal on objects that only differ by their ...
0
votes
2
answers
681
views
A program that prompts the user to enter a number, and then checks whether the number is divisible by 7 and 11, or either 7 or 11
//TASK 5 PART 1
//Request input from user
let x = Number(prompt("Enter a number:"));
// using if...else if
if (x % 7 === 0 &&
x % 11 === 0) {
console.log
...
0
votes
1
answer
140
views
Inputting lengths and angles to determine whether it is a square, rectangle, a rhombus, or parallelogram
I am very new to JavaScript, and so I am here to ask for help on how to make my code more efficient, using less lines of code, improve readability or if anybody spots any errors. I am always keen on ...
9
votes
2
answers
386
views
What happens "behind the scenes" if I call `None == x` in Python?
I am learning and playing around with Python and I came up with the following test code (please be aware that I would not write productive code like that, but when learning new languages I like to ...
2
votes
2
answers
228
views
Comparing '1' and '_' in PowerShell gives unexpected result
On comparing '1' with '_' the answer I'm expecting is '1' < '_' because their Ascii values are 49 and 95 respectively. But the answer is the other way. For that matter, even ':' instead of '_' ...
1
vote
1
answer
175
views
p > nullptr: Undefined behavior?
The following flawed code for a null pointer check compiles with some compilers but not with others (see godbolt):
bool f()
{
char c;
return &c > nullptr;
}
The offensive part is the ...
0
votes
2
answers
816
views
How to make the cancel Button work in Google App Script Browser Input Box
function sendEmails() {
const sheet = getSpreadsheetApp()
const sheetData = getSpreadsheetData(sheet).getValues();
const subjectline = "Weekly Breakdown for the week of - "
var ...
1
vote
1
answer
101
views
C++ Reuse out-of-class comparsion operators of base for derived class
Snippet
struct A {
int a;
};
bool operator==(const A& lhs, const A& rhs) { return lhs.a == rhs.a; }
template <typename T>
bool operator==(const A& lhs, const T& rhs) {
...
1
vote
1
answer
45
views
How to compare two arrays without checking each value for simon game?
This works perfectly but I want to know that here I am checking only the most recent user input value(with their index number) with the randomly generated value and their lengths, but how can I check ...
0
votes
1
answer
158
views
Can a string and int comparison be ever True in python2.7
I know python2.7 allows comparison between different types. So the equality comparison between str and int will result in False. Can the comparison ever be true depending on the values of a and b?
a =...