Ok, maybe I'm dumb, but Why there is no exception being thrown in the following code?
It's clear that the index by which the _cells array is being accessed is negative (posCol + col = -1) as shown in the picture.
Thanks in advance.
Most likely shape[row,col] is equal to 0. If it is, C# will short circuit the rest of the conditional since it can already determine the entire thing will evaluate to false. Since it never evaluates the second one with the negative index, it doesn't matter that posCol is negative.
Related
In C# code dealing with any HashSet we need to know if it would be necessary to use the LongCount() method of IEnumerable in order to get the number of elements in the set, or should one always use the Count property, which is of type int32 and has a maximum value of 2^31 - 1.
If there exists such limitation on the maximum number of elements in a HashSet, then what type to use when we need to deal with a set that has numberOfElements >= 2^31 ?
Update:
The exact answer can be found if one runs as part of their code this:
var set = new HashSet<bool>(Int32.MaxValue -1);
Then this exception is thrown -- note what the message says:
But anyway, one shouldn't be forced to experiment just to find a simple fact, whose place is in the language documentation!
I know that array comparison questions have been asked multiple times but I am curious to know if there is a better method than the ones I have tried so far.
The requirement is to compare two integer arrays of same length to check if one of them has values which are always greater than or equal to the ones in the second one when mapped one to one. So, if you were to sort the arrays (which I did for comparing them), the values at the same indices in the first array should always be greater than or equal to the ones at the same indices in the second one.
I tried the check in two ways so far:
Sort the arrays. Run a for loop and return a false value if any of the values in the second array turns out to be greater than the corresponding value in the first array:
Array.Sort(Array1);
Array.Sort(Array2);
//
for (int i = 0; i < Array1.length; i++)
{
if (Array2[i] > Array1[i])
{
return false;
}
}
return true;
In the alternate method, I tried to be cute and used the chaining of FindIndex and IndexOf methods to get the solution in one go. I think it's actually less efficient since, as far as I can tell and please correct me if I am wrong, it is traversing the array twice instead of once as is the case in the previous method:
Array.Sort(Array1);
Array.Sort(Array2);
//
return Array.FindIndex(Array2, i => i > Array1[Array.IndexOf(Array2, i)]) >= 0
? false
: true;
I think the first method works better but is there a faster/better way to achieve the same result? The number of elements can range from 10 to 10000.
Thanks.
in this particular line of code :
correct = Array.LastIndexOf(turns.ToArray(), false, 4, 0);
I get result correct = -1, well how is this even possible ?
turns[0] up to turns[3] are equal to false turns[4]=true and turns[5]=false is it possible to be caused because the last index i want to be looked up to is 4 and it has value different than the required one ?
The issue is with the last argument (count). This restricts the number of elements searched. You are restricting it to search 0 elements starting at index 4. Thus, it doesn't find anything.
Your count indicates searching 0 elements in the section.
correct = Array.LastIndexOf(turns.ToArray(), false, 4, 2);
Try this out:
correct = Array.LastIndexOf(turns.ToArray(), false, turns.Length, turns.Length);
What were you doing wrong:
never hard code array length (especially in your case, when the array is filled with values)
the first index is actually the starting search index from backwards, and the second index is actually the count, i.e. how many items to search (MSDN constructor clarification)
Update 1:
Made a mistake on the starting index and the count number. Updated the changes, thank you #Steve for pointing it out.
A friend of mine came to me with this strange behavior which i can't explain, any insight view would be appreciated.
Im running VS 2005 (C# 2.0), the following code show the behavior
int rr = "test".IndexOf("");
Console.WriteLine(rr.ToString());
the above code, print "0" which clearly show it should have return -1
This also happen in Java where the following Class show the behavior:
public class Test{
public static void main(String[] args){
System.out.println("Result->"+("test".indexOf("")));
}
}
Im running Java 1.6.0_17
Quote from the C# documentation:
If value is Empty, the return value
is 0.
The behavior that you describe is entirely as expected (at least in C#).
0 is correct. Start at position zero and you can (trivially) match a zero-length string. Likewise, "" contains "".
This is not an exception to the rule, but rather a natural consequence of how indexOf and startsWith are defined.
You’re claiming that "test".indexOf("") should return -1. This is essentially equivalent to the claim that "test".startsWith("") should return false. Why is this? Although this case is specifically addressed in the documentation as returning true, this is not just an arbitrary decision.
How would you decide "test".startsWith("te"), for example? The simplest way is to use recursion. Since both strings start with the character 't', you call "est".startsWith("e") and return the result. Similarly, you will call "st".startsWith("") and return the result. But you already know that the answer should be true, so that is why every string starts with "".
0 is correct. The Javadocs point out that indexOf works as follows:
The integer returned is the smallest
value k such that:
this.startsWith(str, k)
Any string starting with "" is equal to the original string (and every string starts with ""), so the smallest k for str = "" is always 0.
Think of it this way: IndexOf, when looking for a string, will start at position 0, try to match the string, if it doesn't fit, move on to position 1, 2, etc. When you call it with an empty string, it attempts to match the empty string with the string starting at position 0 with length 0. And hooray, nothing equals nothing.
Side note: There's no real reason to use ToString when you're using Console.Write/WriteLine. The function automatically calls the ToString method of the object in question. (Unless overloading ToString)
It should return 0. You are looking for the first occurrence of an empty string, right? :)
More fun php actually does a way better job!
php -r "print strpos('test','');"
PHP Warning: strpos(): Empty delimiter. in Command line code on line 1
Just for the fun of it. It also works like that in python
>>> "test".startswith("")
True
>>> "test".index("")
0
Python throws a ValueError instead of the -1 that is nice.
>>> "test".index('r')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: substring not found
I'm using C# with the XNA library and I'm getting NaNs cropping up in my Vector3 objects. Is there a way to break into the debugger when the offending calculation happens (e.g. a divide by zero)? Currently the program just continues running. I'm using VS2008 Professional. All the exceptions in the Exceptions dialog are selected in the "user-unhandled" column.
Edit: To clarify, I can't work out where the bad calculation is. This is why I want the debugger to break automatically. Setting breakpoints is not a solution.
Firstly dividing a double/float by zero gives Infinity/-Infinity depending upon whether the double is positive or negative. Only a zero double/float divided by zero gives NaN. In either case, no exception will be thrown.
You should be able to use conditional breakpoints to detect when a particular variable gets set to one of these values. Be careful when checking for NaN though, as NaN != NaN.
double a = double.NaN;
Console.Out.WriteLine(a == double.NaN); // false
Console.Out.WriteLine(a == a); // false
Console.Out.WriteLine(double.IsNaN(a)); // true
Sounds like you're handling the exception somehow (like a catching a generic Exception) What you can do is press Ctrl+alt+E to bring up the exceptions dialog -- make sure you check the "when thrown" checkbox for the exception(s) you're interested in
You could set a conditional breakpoint that only breaks when the divisor of an expression is 0.
I know this is an old post but.....
from experience its nearly always Vector3.Normalize that i use by mistake.
When I'm not sure whether it will be zero length I now always do
float L = V.Length();
if(L != 0.0)
V /= L;
divide by zero in Normalize should give an exception but it doesn't. Caused me a lot of head scratching.