Search Results

Search found 251 results on 11 pages for 'equality'.

Page 3/11 | < Previous Page | 1 2 3 4 5 6 7 8 9 10 11  | Next Page >

  • How to hash and check for equality of objects with circular references

    - by mfya
    I have a cyclic graph-like structure that is represented by Node objects. A Node is either a scalar value (leaf) or a list of n=1 Nodes (inner node). Because of the possible circular references, I cannot simply use a recursive HashCode() function, that combines the HashCode() of all child nodes: It would end up in an infinite recursion. While the HashCode() part seems at least to be doable by flagging and ignoring already visited nodes, I'm having some troubles to think of a working and efficient algorithm for Equals(). To my surprise I did not find any useful information about this, but I'm sure many smart people have thought about good ways to solve these problems...right? Example (python): A = [ 1, 2, None ]; A[2] = A B = [ 1, 2, None ]; B[2] = B A is equal to B, because it represents exactly the same graph. BTW. This question is not targeted to any specific language, but implementing hashCode() and equals() for the described Node object in Java would be a good practical example.

    Read the article

  • Check for element equality in an animation function.

    - by Zardoz
    I have the the below code and would expect that in the first pass of the fadeTo function "yes" would be printed as those first two console logs tell me it is the same element. But it doesn't recognize them as equal. What do I miss here? var tds = self.element.find("td:nth-child(" + (columnIndex + 1) + ")"); tds.fadeTo(options.columnFadeOutTime, 0, function() { window.console.log(tds.first()); window.console.log($(this)); if ($(this) == tds.first()) { window.console.log("yes"); } else { window.console.log("no"); } }

    Read the article

  • Help with string equality in Java

    - by annayena
    The following function accepts 2 strings, the 2nd (not 1st) possibly containing *'s (asterisks). An * is a replacement for a string (empty, 1 char or more), it can appear appear (only in s2) once, twice, more or not at all, it cannot be adjacent to another * (ab**c), no need to check that. public static boolean samePattern(String s1, String s2) It returns true if strings are of the same pattern. It must be recursive, not use any loops, static or global variables. Also it's prohibited to use the method equals in the String class. Can use local variables and method overloading. Can use only these methods: charAt(i), substring(i), substring(i, j), length(). Examples: 1: TheExamIsEasy; 2: "The*xamIs*y" ---> true 1: TheExamIsEasy; 2: "Th*mIsEasy*" ---> true 1: TheExamIsEasy; 2: "*" ---> true 1: TheExamIsEasy; 2: "TheExamIsEasy" ---> true 1: TheExamIsEasy; 2: "The*IsHard" ---> FALSE I am stucked on this question for many hours now! I need the solution in Java please kindly help me.

    Read the article

  • <hash_set> equality operator doesn't work in VS2010

    - by ProgramWriter
    Sample code: std::hash_set<int> hs1; // also i try std::unorded_set<int> - same effect std::hash_set<int> hs2; hs1.insert(15); hs1.insert(20); hs2.insert(20); hs2.insert(15); assert(hs1 == hs2); hash_set doesn't stores elements in some order defined by hash function... why? Please note that this code works in VS2008 using stdext::hash_set.

    Read the article

  • Why hashCode() returns the same value for a object in all consecutive executions?

    - by Vijay Shanker
    Hi, I am trying some code around object equality in java. As I have read somewhere hashCode() is a number which is generated by applying the hash function. Hash Function can be different for each object but can also be same. At the object level, it returns the memory address of the object. Now, I have sample program, which I run 10 times, consecutively. Every time i run the program I get the same value as hash code. If hashCode() function returns the memory location for the object, how come the java(JVM) store the object at same memory address in the consecutive runs? Can you please give me some insight and your view over this issue?

    Read the article

  • Java: Comparing a class with another within that class using a my own .equals

    - by user1670252
    I am making a method .equals replacing the equals method used. It accepts a object. I want it to check if that object equals the class that runs the .equals class. I know I want to compare all the private methods I have to that object. Is there a way to do this without making another private class to get the private variables from the object? How do I do this to compare equality not identity? I am stuck on this. Do i have to use == to compare? Also looking online i see others use recursion. If this is the way i have to do it can you show and explain it to me? so an example i have public boolean equals(Object o) { this is in a class we will call bobtheBuilder (first thing to pop in my head) I want to check if the object o is equal to the class he has private object array and a private int. I assume I want to check if the array and int of this class equal the array and int of the object.

    Read the article

  • GetHashCode on null fields?

    - by Shimmy
    How do I deal with null fields in GetHashCode function? Module Module1 Sub Main() Dim c As New Contact Dim hash = c.GetHashCode End Sub Public Class Contact : Implements IEquatable(Of Contact) Public Name As String Public Address As String Public Overloads Function Equals(ByVal other As Contact) As Boolean _ Implements System.IEquatable(Of Contact).Equals Return Name = other.Name AndAlso Address = other.Address End Function Public Overrides Function Equals(ByVal obj As Object) As Boolean If ReferenceEquals(Me, obj) Then Return True If TypeOf obj Is Contact Then Return Equals(DirectCast(obj, Contact)) Else Return False End If End Function Public Overrides Function GetHashCode() As Integer Return Name.GetHashCode Xor Address.GetHashCode End Function End Class End Module

    Read the article

  • problem with if statement used to determine function return

    - by Patrick
    Im using an if statement to determine what to return in a function, but it seems to be not working the way i want it to. function DoThis($dogs, $cats){ // do something with dogs, pet them perhaps. $reg = $dogs[0]; $nate = $dogs[1]; if($cats = "dave"){return $reg;} if($cats = "tom"){return $nate;} } $cats is a string (if that helps), and when entered it doesn't yield any return. If i manually set a return, that works, but the above doesnt for some reason.

    Read the article

  • Does MS Test provide a default value equals comparison?

    - by fearofawhackplanet
    I want to test for example int orderId = myRepository.SubmitOrder(orderA); orderB = myRepository.GetOrder(orderId); Assert.AreEqual(orderA, orderB); // fail Obviously I need a value comparison here, but I don't want to have to provide an overridden Equals implementation for all of my classes purely for the sake of testing (it wouldn't be of any use in the rest of the app). Is there a provided generic method that just checks every field using reflection? Or if not, it is possible to write my own?

    Read the article

  • Different results when applying function to equal values

    - by Johannes Stiehler
    I'm just digging a bit into Haskell and I started by trying to compute the Phi-Coefficient of two words in a text. However, I ran into some very strange behaviour that I cannot explain. After stripping everything down, I ended up with this code to reproduce the problem: let sumTup = (sumTuples°concat) frequencyLists let sumFixTup = (138, 136, 17, 204) putStrLn (show ((138, 136, 17, 204) == sumTup)) putStrLn (show (phi sumTup)) putStrLn (show (phi sumFixTup)) This outputs: True NaN 0.4574206676616167 So although the sumTupand sumFixTup show as equal, they behave differently when passed to phi. The definition of phi is: phi (a, b, c, d) = let dividend = fromIntegral(a * d - b * c) divisor = sqrt(fromIntegral((a + b) * (c + d) * (a + c) * (b + d))) in dividend / divisor

    Read the article

  • == Operator and operands

    - by rahul
    I want to check whether a value is equal to 1. Is there any difference in the following lines of code Evaluated value == 1 1 == evaluated value in terms of the compiler execution

    Read the article

  • How do I ignore the UTF-8 Byte Order Marker in String comparisons?

    - by Skrud
    I'm having a problem comparing strings in a Unit Test in C# 4.0 using Visual Studio 2010. This same test case works properly in Visual Studio 2008 (with C# 3.5). Here's the relevant code snippet: byte[] rawData = GetData(); string data = Encoding.UTF8.GetString(rawData); Assert.AreEqual("Constant", data, false, CultureInfo.InvariantCulture); While debugging this test, the data string appears to the naked eye to contain exactly the same string as the literal. When I called data.ToCharArray(), I noticed that the first byte of the string data is the value 65279 which is the UTF-8 Byte Order Marker. What I don't understand is why Encoding.UTF8.GetString() keeps this byte around. How do I get Encoding.UTF8.GetString() to not put the Byte Order Marker in the resulting string?

    Read the article

  • String comparison in Python: is vs. ==

    - by Coquelicot
    I noticed a Python script I was writing was acting squirrelly, and traced it to an infinite loop, where the loop condition was "while line is not ''". Running through it in the debugger, it turned out that line was in fact ''. When I changed it to != rather than 'is not', it worked fine. I did some searching, and found this question, the top answer to which seemed to be just what I needed. Except the answer it gave was counter to my experience. Specifically, the answerer wrote: For all built-in Python objects (like strings, lists, dicts, functions, etc.), if x is y, then x==y is also True. I double-checked the type of the variable, and it was in fact of type str (not unicode or something). Is his answer just wrong, or is there something else afoot? Also, is it generally considered better to just use '==' by default, even when comparing int or Boolean values? I've always liked to use 'is' because I find it more aesthetically pleasing and pythonic (which is how I fell into this trap...), but I wonder if it's intended to just be reserved for when you care about finding two objects with the same id.

    Read the article

  • C# == operator in Immediate window behaves differently than at run-time

    - by Damiano
    Try the following in the Immediate window: object a1 = "a"; object a2 = "a"; a1==a2 // outputs false and you'll see that a1 == a2 outputs false. However, at runtime in either a window app or console, you'll get true: object t1 = "a"; object t2 = "a"; MessageBox.Show((t1 == t2).ToString()); // outputs true The runtime behavior is consistent with the definition for the == operator and strings. Does anybody know if this a bug in the Immediate window?

    Read the article

  • why assert_equal() in Ruby on Rails sometimes seem to compare by Identity and sometimes by value?

    - by Jian Lin
    it was very weird that yesterday, I was do an integration test in Rails and assert_equal array_of_obj1, array_of_obj2 # obj1 from db, obj2 created in test and it failed. The values shown inside the array and objects were identical. If I change the test to assert array_of_obj1 == array_of_obj2 Then it will pass. But today, the first test actually passed. What reason could it be? Is assert_equal always using == or .equal? in Rails 2.2 or 2.3.5?

    Read the article

  • C++ enforce conditions on inherited classes

    - by user231536
    I would like to define an abstract base class X and enforce the following: a) every concrete class Y that inherits from X define a constructor Y(int x) b) it should be possible to test whether two Y objects are equal. For a, one not very good solution is to put a pure virtual fromInt method in X which concrete class will have to define. But I cannot enforce construction. For b), I cannot seem to use a pure virtual method in X bool operator == (const X& other) const =0; because in overridden classes this remains undefined. It is not enough to define bool operator == (const Y& other) const { //stuff} because the types don't match. How do I solve these problems?

    Read the article

  • Javascript === vs == : Does it matter which "equal" operator I use?

    - by bcasp
    I'm using JSLint to go through some horrific JavaScript at work and it's returning a huge number of suggestions to replace == with === when doing things like comparing 'idSele_UNVEHtype.value.length == 0' inside of an if statement. I'm basically wondering if there is a performance benefit to replacing == with ===. Any performance improvement would probably be welcomed as there are hundreds (if not thousands) of these comparison operators being used throughout the file. I tried searching for relevant information to this question, but trying to search for something like '=== vs ==' doesn't seem to work so well with search engines...

    Read the article

  • php validate integer [updated]

    - by George Garchagudashvili
    Read B A: I'll give quick example: $a = "\n \t 34 3"; // string(9) $aint = intval($a); // int(34) var_dump($a == $aint); result: bool(true) call me noob but can you tell me why/how does these variables do pass equalization test? What I want to achieve is to check if '1989' equals 1989 would be true, but not any other case. ex: '1989 ' should not pass the test. Also I don't want to use regex. B: I need to validate if variable is integer, I've tried all the available built-in functions or helpful tips, but the only best solution would be regex which I don't want to use this time. also filter_var is not best because it also filters data, but I want to only validate it. 123 -123 '123' '-123' these inputs to be only true, false otherwise I've tried many different options: ctype_digit("-123"); // false - doesn't work is_int('123'); // false filter_var(' 123 ', FILTER_VALIDATE_INT) !== false; // true - doesn't work

    Read the article

  • PHP: What's the best way to check equality of $_SERVER['HTTP_REFERER'] ?

    - by Hank
    I have a PHP script that checks the HTTP Referer. if ($_SERVER['HTTP_REFERER'] == 'http://www.example.com/') {...} However, this seems inherintly unsafe ... because what happens if the user goes to 'http://example.com/' or 'http://www.ExaMple.com' (both of which don't match the equality test). Question: what's a better equality test to ensure that the HTTP Referer is coming from 'example.com' ?

    Read the article

  • Two '==' equality operators in same 'if' condition are not working as intended.

    - by Manav MN
    I am trying to establish equality of three equal variables, but the following code is not printing the obvious true answer which it should print. Can someone explain, how the compiler is parsing the given if condition internally? #include<stdio.h> int main() { int i = 123, j = 123, k = 123; if ( i == j == k) printf("Equal\n"); else printf("NOT Equal\n"); return 0; } Output: manav@workstation:~$ gcc -Wall -pedantic calc.c calc.c: In function ‘main’: calc.c:5: warning: suggest parentheses around comparison in operand of ‘==’ manav@workstation:~$ ./a.out NOT Equal manav@workstation:~$ EDIT: Going by the answers given below, is the following statement okay to check above equality? if ( (i==j) == (j==k))

    Read the article

  • Practice of checking 'trueness' or 'equality' in conditional statements - does it really make sense?

    - by Senthil
    I remember many years back, when I was in school, one of my computer science teachers taught us that it was better to check for 'trueness' or 'equality' of a condition and not the negative stuff like 'inequality'. Let me elaborate - If a piece of conditional code can be written by checking whether an expression is true or false, we should check the 'trueness'. Example: Finding out whether a number is odd - it can be done in two ways: if ( num % 2 != 0 ) { // Number is odd } or if ( num % 2 == 1 ) { // Number is odd } When I was beginning to code, I knew that num % 2 == 0 implies the number is even, so I just put a ! there to check if it is odd. But he was like 'Don't check NOT conditions. Have the practice of checking the 'trueness' or 'equality' of conditions whenever possible.' And he recommended that I use the second piece of code. I am not for or against either but I just wanted to know - what difference does it make? Please don't reply 'Technically the output will be the same' - we ALL know that. Is it a general programming practice or is it his own programming practice that he is preaching to others?

    Read the article

  • Practice of checking 'trueness' or 'equality' of conditional statements - does it really make sense?

    - by senthilkumar1033
    I remember many years back, when I was in school, one of my computer science teachers taught us that it was better to check for 'trueness' or 'equality' of a condition and not the negative stuff like 'inequality'. Let me elaborate - If a piece of conditional code can be written by checking whether an expression is true or false, we should check the 'trueness'. Example: Finding out whether a number is odd - it can be done in two ways: if ( num % 2 != 0 ) { // Number is odd } or if ( num % 2 == 1 ) { // Number is odd } When I was beginning to code, I knew that num % 2 == 0 implies the number is even, so I just put a ! there to check if it is odd. But he was like 'Don't check NOT conditions. Have the practice of checking the 'trueness' or 'equality' of conditions whenever possible.' And he recommended that I use the second piece of code. I am not for or against either but I just wanted to know - what difference does it make? Please don't reply 'Technically the output will be the same' - we ALL know that. Is it a general programming practice or is it his own programming practice that he is preaching to others?

    Read the article

< Previous Page | 1 2 3 4 5 6 7 8 9 10 11  | Next Page >