comparison-operators

Why does my test of sign always report “negative”?

空扰寡人 提交于 2019-12-25 08:24:15
问题 The following program should print whether the sum of the elements of the array is positive or negative: #include <stdio.h> #define ARR_SIZE 5 int main() { int array[ARR_SIZE] = {1,-2,3,4,-5}; unsigned sum; int i; for(i=0, sum=0; i < ARR_SIZE; i++) { sum += array[i]; printf("sum %d\n ", sum); } printf("%d\n",sum); if(sum>-1) printf("non negative\n"); else printf("negative\n"); return 0; } The program doesn't do what it is supposed to; it prints 'negative' no matter what array values it

Is std::equal_to guaranteed to call operator== by default?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-22 18:34:44
问题 I had always thought that the standard required the non-specialized template for std::equal_to<T> to call T::operator== , but I noticed the description at cppreference.com almost implies it's the other way around; certainly it doesn't mention it as a requirement. I also checked the C++11 draft standard N3337 and couldn't find any guarantees there either. If you create a class with an operator== you'd hope it would get used in all circumstances. I can't honestly think of a way to implement std

why do I need comparison operators in boost python vector indexing suite?

时光怂恿深爱的人放手 提交于 2019-12-22 05:23:31
问题 I would like to expose C++ code with a std::vector<A> to python. My class A{}; does not have a comparison operator implemented. When I try BOOST_PYTHON_MODULE(libmyvec) { using namespace boost::python; class_<A>("A"); class_<std::vector<A> >("Avec") .def(boost::python::vector_indexing_suite<std::vector<A> >()); } I get an error about comparison operators. If I change the definition of A to class A { public: bool operator==(const A& other) {return false;} bool operator!=(const A& other)

Ordering of boolean values

我们两清 提交于 2019-12-22 02:51:50
问题 Under C++ or <stdbool.h> from C99, how is the less-than operator < defined for boolean values? Alternatively, explain the behaviour of this code: #ifndef __cplusplus #include <stdbool.h> #endif #include <stdio.h> int main() { bool b = -1; if(b < true) { printf("b < true\n"); } if(b < false) { printf("b < false\n"); } if(true < false) { printf("true < false\n"); } if(false < true) { printf("false < true\n"); } } Under MSVC version 10, compiled as C++ code, GCC 4.6.3-ubuntu5 compiled as C code

Combined Comparison / “Spaceship” Operator (<=>) in Javascript?

情到浓时终转凉″ 提交于 2019-12-20 17:33:25
问题 Ruby has something called a Combined Comparison or "Spaceship" Operator, it looks like this: <=> It does the following: a <=> b := if a < b then return -1 if a = b then return 0 if a > b then return 1 Credit Is there a similar Operator in Javascript? If not, how can I end up with the same result? @madox2 suggested using Math.sign(a - b) , which works for number, but not arrays (to compare arrays you need to use array.length ). It also does not work in Internet Explorer, Safari or all Mobile

Why is === faster than == in PHP?

泪湿孤枕 提交于 2019-12-17 21:38:45
问题 Why is === faster than == in PHP? 回答1: Because the equality operator == coerces, or converts, the data type temporarily to see if it’s equal to the other operand, whereas === (the identity operator) doesn’t need to do any converting whatsoever and thus less work is done, which makes it faster. 回答2: === does not perform typecasting, so 0 == '0' evaluates to true , but 0 === '0' - to false . 回答3: First, === checks to see if the two arguments are the same type - so the number 1 and the string '1

Sympy - Comparing expressions

大城市里の小女人 提交于 2019-12-17 19:34:56
问题 Is there a way to check if two expressions are mathematically equal? I expected tg(x)cos(x) == sin(x) to output True , but it outputs False . Is there a way to make such comparisons with sympy? Another example is (a+b)**2 == a**2 + 2*a*b + b**2 which surprisingly also outputs False . I found some similar questions, but none covered this exact problem. 回答1: From the SymPy documentation == represents exact structural equality testing. “Exact” here means that two expressions will compare equal

Java instanceof operator

偶尔善良 提交于 2019-12-17 16:52:58
问题 Is there a valid class Type variable that can be used with the instanceof operator? For Example: String s = "abc"; Class<?> classType = String.class; if (s instanceof classType) { //do something } as an alternative to this: if (s.getClass() == classType) { //do something } Would there be any performance benefit? 回答1: What you're doing is not actually the same. Consider what happens with subclasses (I know you can't subclass String , so in the String case it doesn't matter). class A {} class B

JavaScript - === vs == operators performance

萝らか妹 提交于 2019-12-17 10:44:29
问题 A few weeks ago, I have read this thread Is < faster than <=? about comparison operators in C . It was said that there is no difference in the performance between < and <= as they are interpreted as same/similar machine commands. At the same time, in our company's "best practices", it was said that we should always use "===" to compare things instead of "==". So, I started to wonder if this is always appropriate as I am used to using the "==" and "typeof ... == " and do not want to change my

operator< comparing multiple fields

非 Y 不嫁゛ 提交于 2019-12-17 09:44:11
问题 I have the following operator< that is supposed to sort first by a value, then by another value: inline bool operator < (const obj& a, const obj& b) { if(a.field1< b.field1) return true; else return a.field2 < b.field2; } I have the feeling this is incorrect and that you can't do that without another third comparaison test on the members variables, but I can't find any example where this doesn't work. So whould this really sort as expected? thanks edit : I would have coded it as : inline bool