FAQ Database Discussion Community


issue with equating dictionaries and global issue

python-3.x,equality,defaultdict
Trying to figure best way to union of two dictionaries. Here is the code that I have. Counter is one of the options that I found. def __add__(self,right): mergedbag = Bag() mergedbag.bag_value = copy.copy(self.bag_value) for item in right.bag_value.keys(): mergedbag.bag_value[item] += right.bag_value[item] return mergedbag ...

Java assignment operator

java,variable-assignment,equality,convention
The following blockquote is taken from http://www.oracle.com/technetwork/java/javase/documentation/codeconventions-137265.html Do not use the assignment operator in a place where it can be easily confused with the equality operator. Example: if (c++ = d++) { // AVOID! (Java disallows) ... } should be written as if ((c++ = d++) != 0) { ......

CollectionAssert of an nested List

c#,unit-testing,equality
How to assert a nested list too, without 'unnesting' it? expected.Add(new Customer{ Edition = "Cust", Rarity = "R", ID = 1001, Name = "John Doe", Types = new List<Type_>{ new Type_{ ID = 1, Name = "abc" } }, Here the assert: CollectionAssert.AreEqual(expected, actual); That certainly exclude the nested list....

Does __ne__ use an overridden __eq__?

python,comparison,equality
Suppose I have the following program: class A(object): def __eq__(self, other): return True a0 = A() a1 = A() print a0 != a1 If you run it with Python the output is True. My question is the __ne__ method is not implemented, does Python fall on a default one? if...

In Groovy, why does the behaviour of '==' change for interfaces extending Comparable?

groovy,equality
I'm trying to develop a project in Groovy and I've found some of my tests failing in an odd way: I have an interface Version extends Comparable<Version> with two concrete subclasses. Both override equals(Object) and compareTo(Version) - however, if I try to compare two instances of Version that are of...

Do the 'evil' equalities slow down code?

javascript,performance,operators,equality
For my new JavaScript library, I have a function isBinary which returns true if the passed value is 1, 0, true, or false. I use the following code for easy writing, but I was wondering if I was slowing down the processing, and milliseconds do matter, because the function is...

Can the Java Method object be compared by identity (ie. ==) or do I have to use equals()?

java,reflection,equality,comparison-operators
In Java, can instances of the java.lang.reflect.Method object be compared by identity (ie. ==) or do I have to use equals()?

Take out date values between two dates from matrix variable, Matlab [SOLVED]

matlab,date,equality,between
I'm trying to take out two separate years from a date table. % Date table Datez = [2001 2;2001 5;2001 9;2001 11;2002 3;2002 5;2002 7;2002 9;2002 11;... 2003 2;2003 4;2003 6;2003 8;2003 10;2003 12;2004 3;2004 5;2004 7;... 2004 9;2004 11; 2005 10;2005 12] I want to take out all values...

determine that an instance has GetHashCode and Equals overridden?

c#,.net,equality
Given an instance of an object in C#, how can I determine if that object has value semantics? In other words, I want to guarantee that an object used in my API is suitable to be used as a dictionary key. I was thinking about something like this: var type...

C# generic types equality operator

c#,generics,equality
From https://msdn.microsoft.com/en-us/library/d5x73970.aspx When applying the where T : class constraint, avoid the == and != operators on the type parameter because these operators will test for reference identity only, not for value equality. This is the case even if these operators are overloaded in a type that is used as...

Comparing two virtually identical BigDecimal numbers in Ruby on Rails

ruby,precision,equality,bigdecimal
I got a little problem that I really would like to understand. I am using assert_equal to compare two BigDecimal numbers that are supposed to be identical. They actually are except a very little tiny fraction, see below: -#<BigDecimal:7f4b40e8de78,'0.4021666666 6666666666 666666667E2',36(45)> +#<BigDecimal:7f4b40e85db8,'0.4021666666 6666666666 6666668E2',36(63)> I use assert_in_delta in order to...

Why does comparing Integers by reference (==) work?

java,comparison,operators,equality
In Herbert Schildt's "Java: The Complete Reference, 9th Edition", there's an example that kinda baffles me. Its key point that i can't comprehend could be summed up to the following code: class Test { public static void main (String args[]) { Integer i1 = 6; Integer i2 = 6; Integer...

Find minimum of nonlinear system of equations with nonlinear equality and inequality constraints in MATLAB

matlab,constraints,equality,nonlinear-optimization,inequality
I need to solve this problem better described at the title. The idea is that I have two nonlinear equations in four variables, together with two nonlinear inequality constraints. I have discovered that the function fmincon is probably the best approach, as you can set everything I require in this...

Is string interning done at compile time in Java? [duplicate]

java,string,equality
This question already has an answer here: When are Java Strings interned? 2 answers I am really confused with how string interning works in Java. When I write: String a = "ABC"; String b = "ABC"; if (a==b) System.out.println("Equal"); Does the compiler store the string literal "ABC" into the...

How can I compare value-types acquired from Reflection's “GetValue”?

c#,reflection,equality
I have this code which gets the value from the Test class and then converts it to the type it is. It prints correctly as "Int32" but when I test the equality with another variable with the same value, it prints "false". I suspect it is because it is testing...

equality operator overloading in swift enums with associated values

swift,enums,operator-overloading,equality
I know an almost similar question was asked earlier, but I am not able to comment on it, because I am to new here. That is the reason why I am posting a separat question. Also my question is an extension to the previous question asked and is aimed at...

Making and comparing Sets in Coq

set,equality,coq
I'm having trouble understanding whether it is possible to prove that two sets (in this case regular languages) are identical and thus interchangeable. From what I understand, sets can be equivalent even if they are not constructively equal. Regular languages are sets of strings, but I don't see how to...

Coq: substitution and dependent types

equality,coq,dependent-type
I'm at an odd place trying to prove an equation: 1 subgoals A : Type s : set A x : A s0 : s x x0 : A s1 : s x0 H : x0 = x ______________________________________(1/1) stv s x0 s1 = stv s x s0 What I...

Is there any way of breaking up an Expression> and get the right hand side of the equality comparison?

c#,linq,lambda,expression,equality
This may be a longshot, but I will try it anyway. Let's say that I have an expression in these manners: public class Foo { public int Id { get; set; } } Expression<Func<Foo, bool>> expr = p => p.Id == 2; Is there any way of breaking up the...

Python 2 : How do I check type equality with created classes ? (not between objects)

python,class,python-2.7,types,equality
I've got a defined class (what's inside is useless to the problem): class AllyInstance(dict): def __init__(self,name,pyset,number,gender='none'): for dicopoke_loop_enemy in dicopoke[name]: self[dicopoke_loop_enemy]=dicopoke[name][dicopoke_loop_enemy] self['set']=pyset self['status']='Normal' self['hp']=pyset['stats']['hp'] self['boosts']={'atk':0,'def':0,'spa':0,'spd':0,'spe':0} self['secondarystatuses']=[] #leech seed, confusion ... self['gender']=gender self['name']=name self['number']=number I create an instance of the class :...

c++ == operator double

c++,floating-point,equality
I know it is incorrect to compare double (equality) and the best is to use an epsilon factor as described into the Knuth book (Art of programming). Nevertheless, I am working on a legacy code (C++), where there are a lot of devision like: // b,c double from previous computation...

Putting data into Java arrays

java,arrays,equality
I have a fundamental question about how arrays store data and how to properly put data into an array. THIS PART ANSWERED In this code the method spinWheel() is just calling an integer from 0-36. for(cntr=0; cntr<99; cntr++) { spunNum=spinWheel(); all99Spun[0]=spunNum; } How do I adjust the array all99Spun[] so...

What is faster: equal check or sign check

java,performance,comparison,equality,cpu-architecture
I wonder which operation works faster: int c = version1.compareTo(version2); This one if (c == 1) or this if (c > 0) Does sign comparasion use just a one bit check and equality comparasion use substraction, or it is not true? For certainty, let's say we work on x86. P.S....

Haskell: `==' is not a (visible) method of class

haskell,compiler-errors,instance,equality,typeclass
So, when I compile the following piece of code edited: instance (Eq a) => PartOrd a where [] == [] = True (x:xs) == (y:ys) = x==y && xs==ys _==_ = False xs /= ys = not (xs == ys) I get: `==' is not a (visible) method of class...

How to override isEqual: for CLBeacon?

ios,objective-c,core-location,equality
Background I have a method, provided by a 3rd party library, that returns an array of CLBeacons. - (void)beaconManager:(ESTBeaconManager *)manager didRangeBeacons:(NSArray *)beacons inRegion:(CLBeaconRegion *)region This method is called at regular intervals and the array contains a list of the beacons that are in range. Objects in the array are not...

Using an inequality operator to compare objects

ios,equality
Using != here seems wrong to me. We are comparing objects so isEqual should be the correct approach ? Unless NSNull null returns a singleton with a consistent memory address ? if ([super actionForLayer:layer forKey:@"backgroundColor"] != [NSNull null]) { // whatever } Nick Lockwood is using this in his gist...

Why does the Java equals(Object O) method not have a variant which can take a specific object type (e.g. String, Integer, etc) as input?

java,generics,equality,string-comparison,method-signature
I come across problems where I need to compare two strings (or any other object ) for equality/non-equality using Java language. There are two methods on String Object very useful for this purpose viz. compareTo(Object O), which returns a integer result of comparison while other equals(Object o), which returns a...

Annotating a Java class as safe for reference comparison

java,intellij-idea,equals,equality,multiton
I have a class that's a multiton, so I know that given a particular key, there will never be two instances of the same class that exist. This means that, instead of: if (someObject.equals(anotherObject)) ...it's safe for me to do this: if (someObject == anotherObject) The class is also final,...

Should I override the Object.equals(Object) method?

java,equals,equality,object-comparison
I have two instances of the same class. public class Gilda { private String nome; public Gilda(String nome) { this.nome = nome; } // More stuff } When trying to compare them via Object.equals(Object) method, it returns false. This is strange because nome has the same value on both of...

`Is` operator does not return true for two range variables that point to the same cell

excel,vba,excel-vba,reference,equality
I have a custom class named imera in which I include a range property named date_cell. When creating a collection of imera's, every imera's date_cell is set to reference to a specific cell in excel. While attempting to search within the collection by date_cell: Option Explicit Public imeraCol as Collection...

How to compare two Strings when both can be null? [duplicate]

java,equals,equality
This question already has an answer here: Compare two strings in Java with possible null values 8 answers I am aware that it is better to call the equals method over using the == operator (see this question). I want two strings to compare as equal if they are...

Julia's dictionary method `haskey` returning false when key is present

dictionary,equality,julia-lang
I am new to Julia and I am not sure why the last line evaluates to false: type Point{T} x::T y::T end D = [Point(1.,2.) => 42] haskey(D, Point(1., 2.)) #False! Clearly the key exists so what's going on here!? Edit. If I don't use a class Point, it works...

The order of evaluation of equality operator in C++

c++,equality,order-of-evaluation
Recently I have read some chapters C++ Primer, there is one things which made me confused. The book says: There are four operators that do guarantee the order in which operands are evaluated. the logical AND (&&) operator, the logical OR (||) operator, the conditional (? :) operator, and the...

why “==” working differently for integer and strings reference? [duplicate]

java,equality
This question already has an answer here: How do I compare strings in Java? 23 answers May I know how == works here? public class App { public static void main(String[] args) { String s1 = new String("str"); String s2 = new String("str"); System.err.println("why it,s "+String.valueOf(s1==s2)); int i1 =...

C# - byte array - byte[] - Is there a simple comparer? [duplicate]

c#,arrays,equality
This question already has an answer here: Comparing two byte arrays in .NET 16 answers Have I just forgotten the obvious, or is the "manual" comparer the best way to go? Basically, I just want to compare the contents of type (small) byte-arrays. If all bytes match, the result...

To check objects inside std::array has identical member data

c++,std,equality
Cards.h class Card { public: // Card suits struct Suit { // Suits in order enum Enum { Clubs, Diamonds, Hearts, Spades, }; }; // Card rank struct Rank { // Ranks with aces low enum Enum { Ace, Two, King, .... ... }; }; // constructors //get & set...

Swift: Overriding == in subclass results invocation of == in superclass only

swift,override,equality
I've got a class A, which conforms to Equatable protocol and implements == function. In subclass B I override == with more checks. However, when I do comparison between two arrays of instances of B (which both have type Array<A>), == for A is invoked. Of course if I change...

Unexpected effect of smt.arith.nl.gb on reasoning with (syntactic) equality - bug?

z3,equality,groebner-basis
Consider the following SMTLIB program (on rise4fun here): (set-option :auto_config false) (set-option :smt.mbqi false) (set-option :smt.arith.nl.gb false) (declare-const n Int) (declare-const i Int) (declare-const r Int) (assert (= i n)) (assert (= r (* i n))) (push) (assert (not (= r (* n n)))) (check-sat) ; unknown (pop) Although it...

Proper way to compare two objects of different types

c#,equality
I'm using a lib containing a service that notifies when an object is created or deleted in another application. Those object are of type A which contains a lot of information that I don't need in my app. So in my app I created a type B which is a...

Check if class property has been set

c#,equality,setter,getter
Lets assume I have a User class public Class User { public string Name { get; set; } public string Surname { get; set; } public int Level {get;set;} } User user1 = new User(); user1.Name = "name"; user1.Surname = "Surname"; user1.Level = 0; User user2 = new User(); user2.Name...

how to check “reference equality” between two range variables

excel,vba,reference,equality
I am not very experienced in vba, so there seems to be something I'm missing, starting from the basics. I have a custom class named "imera" in which I include a range property named "date_cell". When creating a collection of imera's, every imera's date_cell takes a reference to a specific...