My goal is to prevent index out of bounds conditions for the lower bound when using a variable as a subscript to an array. In other words, I'd like to limit the integer variable values to >= 0. Sort of similar to an absolute value, except instead of making a negative number positive, it would make a negative number zero.
Is there any better method of doing this than using a macro such as:
#define gte0(value) (value < 0) ? 0 : value
and then wrapping my variables representing an index with this macro when I access an array element? Is there a standard practice bounds checking other than doing it manually in every place in your code before you access an array element with a variable representing the index?
I'm looking for any solutions in C or Objective-C.
Thanks!
Require unsigned int or NSUInteger primitives for indices. You'll then be guaranteed a value greater than or equal to zero, up to UINT_MAX or whatever limits.h defines, and you just need to check the upper bound.
There is no way of doing this automatically in C.
Related
I am trying to go over over some variables during some branching and I want to know if the current solution has any variables that are integers.
I am doing this:
SCIPgetSolVal(scip, sol, (*transport_vars)[i][j])) > 0.999
But I want to know if there exists a method that can tell me that (*transport_vars)[i][j] is achieving its upper limit (binary 1.0) instead of comparing it to > 0.999.
In case it matters, I am comparing it with 0.999 in a constraint handler. I think I am facing numerical issues if I use 0.999
You can just call SCIPisIntegral(SCIP* scip, SCIP_Real val) if you just want to know if your value is integer (within tolerances).
If you really want to know if it is 1 or not, you can use either SCIPisEq or SCIPisFeasEq. Both check equality with a tolerance. In the first case the tolerance is scips num_epsilon parameter and in the second it is the feasibility tolerance.
I'm trying to create a Minesweeper game.
I have a 4x4 set of buttons equally spaced in main.Storyboard.
My plan is to create a random array which places a 0 or * in the 1st/2nd/3rd/4th arrays. I would do this by using the arc4Random method.
With the remaining blank cells, I then have to check how many mines there could be for the 8 (potential) squares around the cell/button. This would be governed by the boundary conditions (0,0 to 3,3).
Once this is set up, I would then set the background and number label to the same colour. I could then write an if or else statement to change the colour after each button is pressed.
I'm quite struggling how to start this off and actually write this. Can anyone please give me some advice please?
Well,
you can get a boolean like this.
bool hasMine = arc4random() % 2;
this will give you 50% chance to get a bomb... if you want less bomb, increase the value (3 will give you 2 bomb free square, for one with a bomb, etc..)
then a "" or a "*" like this;
NSSString * value = hasMine ? #"*" : #"" ;
then it's just a matter of a for loop to populate your arrays.
for the sake of performance, I wouldn't use a n x n nested array but a single arrray of nxn size (in your case a array with 16 value). Then I will set a tag for 0 to (nxn -1) to each button based on its position, and on click I'll get the tag of the pressed button and retrive the value of the object at this position in the array
I have a label that I want to set its text as an object from and array peopleQuestionArray. I have generated an random int with values ranging from 1-4. My array currently has 4 objects in it. I expect that either the first, second, third, or fourth objects in my array will be displayed, but instead xcode gave me and exception. Does anyone know why. Here's the code I used to set the labels text :
int random = (arc4random() % 4) +1;
[quesetionLabel setText:[peopleQuestionArray objectAtIndex:random]];
Your index may go out of range. NSArray indices start from 0. So you need to generate a random number ranging from 0-3, not 1-4. Remove the +1 while calculating the random number.
Why is the value of NSUInteger 2^32 - 1 instead of 2^32? Is there a relationship between this fact and the need of a nan value? This is so confusing.
Count to 10 on your fingers. Really :)
The standard way to count to 10 is 1,2,3,..10 (the ordinality of each finger is counted). However, what about "0 fingers"?
Normally that might represent that by putting your hands behind our back, but that adds another piece of information to the system: are your hands in front (present) or behind (missing)?
In this case, putting hands behind your back would equivalent to assigning nil to an NSNumber variable. However, NSUInteger represents a native integer type which does not have this extra state and must still encode 0 to be useful.
The key to encode the value 0 on your fingers is to simply count 0,1,2..9 instead. The same number of fingers (or bits of information) are available, but now the useful 0 can be accounted for .. at the expense of not having a 10 value (there are still 10 fingers, but the 10th finger only represents the value 9). This is the same reason why unsigned integers have a maximum value of 2^n-1 and not 2^n: it allows 0 to be encoded with maximum efficiency.
Now, NaN is not a typical integer value, but rather comes from floating point encodings - think of float or CGFloat. One such common encoding is IEEE 754:
In computing, NaN, standing for not a number, is a numeric data type value representing an undefined or unrepresentable value, especially in floating-point calculations ..
2^32-1 because counting starts from 0 for bits. If it's easier think of it as 2^32 - 2^0.
It is the largest value a 32-bit unsigned integer variable can hold. Add one to that, and it will wrap around to zero.
The reason for that is that the smallest unsigned number is zero, not one. Think of it: the largest number you can fit into four decimal places is 9999, not 10000. That's 10^4-1.
You cannot store 2^32 in 4 bytes, but if you subtract one then it fits (result is 0xffffffff)
Exactly the same reason why the odometer in your car shows a maximum of 999999 mi/km (assuming 6 digits) - while there are 10^6 possible values it can't show 10^6 itself but 0 through 10^6-1.
I have a method that needs to do a different thing when given an unset float than a float with the value of 0. Basically, I need to check whether or not a variable has been, counting it as set if it has a value of 0.
So, what placeholder should I use as an unset value (nil, NULL, NO, etc) and how can test to see if a variable is unset without returning true for a value of 0?
You can initialize your floats to NaN (e.g. by calling nan() or nanf()) and then test with isnan() if they have been changed to hold a number. (Note that testing myvalue == nan() will not work.)
This is both rather simple (you will probably include math.h in any case) and conceptually sensible: Any value that is not set to a number is "not a number"...
Using a constant value to indicate the unset state often leads to errors when the variable legitimately obtains the value of that constant.
Consider using NSNumber to store your float. That way it can not only be nil, it will default to that state.
This assumes that you only need a small number of floats. If you need millions of them, NSNumber may be too slow and memory-intensive.
Instead of overloading these float properties (let's call them X and Y), create a separate isValid flag for each property. Initialize the flags to indicate that the floats haven't been set, and provide your own setters to manage the flags appropriately. So your code might look something like:
if (self.isXValid == YES) {
self.Y = ... // assigning to Y sets isYValid to YES
}
else if (self.isYValid == YES) {
self.X = ... // assigning to Y sets isXValid to YES
}
You could actually go a step further and have the setter for X also assign Y and vice versa. Or, if X and Y are so closely linked that you can calculate one based on the value of the other, you really only need one variable for both properties.