-3

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

    for (loopCount = 0; loopCount < 8; loopCount++){
        integerValueA = loopCount + 2;
        integerValueB = integerValueA + 1;
        intergerOfInterest = (integerValueA) * (integerValueA);
    }


    for (loopCount = 0; loopCount < 8; loopCount++){
        integerValueA = loopCount + 2;
        integerValueB = integerValueA + 1;
        intergerOfInterest = (integerValueA) * (integerValueB);
    }

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.

  • But apparently you have no error? (You haven't described one.) – Hot Licks Jan 01 '14 at 01:41
  • (And is there any reason why you don't use the value you assigned into N_ValB in the first loop?) – Hot Licks Jan 01 '14 at 01:43
  • 2
    (BTW, please learn standard Objective-C naming conventions.) – Hot Licks Jan 01 '14 at 01:44
  • For the first loop, I'm seeing `N_Val` take on the following values: 4, 9, 16, 25, 36, 49, 64, 81. What's the problem? – godel9 Jan 01 '14 at 01:45
  • Well, you keep squaring (N_Count * 2). That's the result you'd expect. – Hot Licks Jan 01 '14 at 01:47
  • (Hint: Delete the non-functional statements -- the assignments to N_ID, N_Own, N_ValB, N_Shape. Look at what's left.) – Hot Licks Jan 01 '14 at 01:49
  • (The two would be equivalent if you changed the last line of the first loop to `N_Val = (N_ValA) * (N_ValA + 1);` – Hot Licks Jan 01 '14 at 01:51
  • @Hot Licks: in order - I get no compile error, however both the NSLog and the inspector tell me the value is wrong!; I have adjusted the naming, and the problem still exists, the other bits are only non funcitonal to what you can see; I would expect to see the list you gave it is not the list I get, see above my code, yes it equivelent however there are 6 loops of which I have shown the two simplest, which have very slightly different results to be stored using integerValueA and integerValueB in various ways to create numbers, keeping the format for each loop the same makes checking easier – Bookkeeper Jan 01 '14 at 02:01
  • Now (after your edits) your two loops are identical. And, unless I misread, they both will produce the progression of squares: 4, 9, 16, ... – Hot Licks Jan 01 '14 at 02:03
  • OK, now they're different again. The first will get 4, 9, 16... and the second will get 6, 12, 20... – Hot Licks Jan 01 '14 at 02:11
  • After finding and replacing all instances of my variables for some reason that loop now works but integerOfInterest = integerValueB^2; still gives the wrong answers 1,6,7,4,5,10,11,8 – Bookkeeper Jan 01 '14 at 02:14
  • I can't work out how to show it unless I take screenshots and post a link. integerOfInterest = (integerValueA + integerValueB)^2; also gives errors 7,5,11,9,15,13,19,17 so it seams the the square function may be the problem, I will adjust to multiply only and fully recompile and see. – Bookkeeper Jan 01 '14 at 02:20
  • Clean build with all '^' powers removed has worked, but still baffled at the reason for the problem! Thanks for the help. – Bookkeeper Jan 01 '14 at 02:27
  • 1
    You know that `^` is the bitwise XOR operator in C, not the exponent operator, right? – godel9 Jan 01 '14 at 02:33
  • Thanks I missed that, I know the bitwise shift operators! and the logical AND/OR operators, but I didn't pick up on that one. Thank you for solving my mystery, if you post as an answer I will accept it, and I will use the macro `pow(number,power)` in future. – Bookkeeper Jan 01 '14 at 02:58
  • 1
    So what you're saying is that you never actually showed us your real code. – Hot Licks Jan 01 '14 at 13:07
  • I showed you my actual code at the point I was getting the error in fact the code above is copied from when I was still getting the error. I did point out in my opening statement I had tried both, for some reason my subsequent rebuilds preserved the bit operator, dispite what my code was actually reading... Once I did a clean then a rebuild it worked. – Bookkeeper Jan 02 '14 at 10:53

2 Answers2

0

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
}
godel9
  • 7,340
  • 1
  • 33
  • 53
0

Is it possible you have a spelling error on your variable InteRgerOfInterest instead of IntEgerOfInterest ?

GH

  • It is spelt incorrectly but has no effect as it was done by find replace all, to fix a non-conventional name. – Bookkeeper Jan 01 '14 at 11:02