I am stumped! I am using NSinteger but as all my inputs are integers and the results are therefore integers I can't see the problem that people doing division are having due to rounding.
I've looked in the developer guide for NSInteger and cannot find a warning.
And my searching of this site and google hasn't yielded results.
As I state at the end I can work around it, but I feel there is something simple I am missing.
All the variables are direct, no pointers yet in the first for loop, intergerOfInterest is 0, 1, 6, 7, 4, 5, 10, 11 which is very random, I have tried swapping integerValueA^2 for the calculation but with no effect. The second for loop however gives the correct answers, 6,12,20,30,2,56,72,90
There are several loops in the structure and the common factor between the correct number and the incorrect number is if the NSInteger is used more than once in a calculation. I figure this can't be right so does anyone have an idea how I am getting this wrong. Note: if I add an extra variable to store the second instance of the number (so in the first loop I use "(integerValueA) * (integerValueB - 1 )" it works fine.
Note: Edited to use naming conventions.
Solution
It sounds from the discussion in the comments that your original code looked something like this:
for(int i = 0; i < 8; i++) {
nVal = i ^ 2; // Supposed to be equivalent to nVal = i * i;
// Do something with nVal
}
The ^ operator in C is actually the bit-wise XOR operator, not the exponent operator. The above code takes i, flips bit 1, and assigns the result to nVal.
You want to use either of the following:
// Option 1
for(int i = 0; i < 8; i++) {
nVal = i * i;
// Do something with nVal
}
// Option 2
for(int i = 0; i < 8; i++) {
nVal = pow(i, 2);
// Do something with nVal
}
OTHER TIPS
Is it possible you have a spelling error on your variable InteRgerOfInterest instead of IntEgerOfInterest ?